티스토리 뷰
https://www.acmicpc.net/submit/11559/63166057
로그인
www.acmicpc.net
class Node {
constructor(item) {
this.item = item;
this.next = null;
}
}
class Queue {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(item) {
const node = new Node(item);
if (this.head == null) {
this.head = node;
} else {
this.tail.next = node;
}
this.tail = node;
this.length += 1;
}
pop() {
const popItem = this.head;
this.head = this.head.next;
this.length -= 1;
return popItem.item;
}
}
const input = require('fs')
.readFileSync('./dev/stdin')
.toString()
.trim()
.split('\n')
.map((v) => v.trim().split(''));
const dx = [0, 0, -1, 1];
const dy = [1, -1, 0, 0];
let board = Array.from(Array(6), () => Array(12).fill(null));
for (let i = 0; i < 12; i++) {
for (let j = 0; j < 6; j++) {
board[j][i] = input[i][j];
}
}
// 뿌요가 터진다.
function puyo() {
let flag = false;
let visited = Array.from(Array(6), () => Array(12).fill(false));
for (let i = 0; i < 6; i++) {
for (let j = 0; j < 12; j++) {
if (board[i][j] != '.' && !visited[i][j]) {
const alpha = board[i][j];
const cnt = [];
const q = new Queue();
visited[i][j] = true;
cnt.push([i, j]);
q.push([i, j]);
while (q.length > 0) {
const [x, y] = q.pop();
for (let k = 0; k < 4; k++) {
const nx = x + dx[k];
const ny = y + dy[k];
if (nx < 0 || nx >= 6 || ny < 0 || ny >= 12 || visited[nx][ny])
continue;
if (board[nx][ny] == alpha) {
visited[nx][ny] = true;
cnt.push([nx, ny]);
q.push([nx, ny]);
}
}
}
if (cnt.length >= 4) {
flag = true;
cnt.forEach((v) => {
const [x, y] = v;
board[x][y] = '.';
});
}
}
}
}
return flag;
}
// 뿌요가 내려온다.
function next() {
for (let i = 0; i < 6; i++) {
board[i] = board[i].filter((v) => v != '.');
while (board[i].length < 12) {
board[i].unshift('.');
}
}
}
let answer = 0;
while (true) {
const result = puyo();
if (result) {
answer++;
next();
} else {
break;
}
}
console.log(answer);
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 16987번: 계란으로 계란치기 (0) | 2023.07.09 |
---|---|
Node.js) 백준 1941번: 소문난 칠공주 (0) | 2023.07.09 |
Node.js) 백준 17219번: 비밀번호 찾기 (0) | 2023.07.08 |
Node.js) 백준 2935번: 소음 (0) | 2023.07.08 |
Node.js) 백준 15688번: 수 정렬하기 5 (0) | 2023.07.07 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- node.js
- MOD
- 최소공통조상
- 다이나믹프로그래밍
- create db
- 투포인터 연습
- DB 생성
- 개발자면접
- 면접질문
- BFS
- 롱베케이션
- 면접비
- 서버점검
- 그래프
- 동적프로그래밍
- 투포인터
- 로드나인
- 은둔청년체험
- 서버개발
- MySQL
- create databases;
- 다이나밍프로그래밍
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함