티스토리 뷰
https://www.acmicpc.net/problem/2636
2636번: 치즈
아래 <그림 1>과 같이 정사각형 칸들로 이루어진 사각형 모양의 판이 있고, 그 위에 얇은 치즈(회색으로 표시된 부분)가 놓여 있다. 판의 가장자리(<그림 1>에서 네모 칸에 X친 부분)에는 치즈가 놓
www.acmicpc.net
const fs = require('fs');
let [N, ...map] = fs.readFileSync("./dev/stdin").toString().trim().split("\n").map(v => v.split(' ').map(Number))
const [H, W] = N;
let MAX = Math.max(H, W)
const cnt = [];
let cheese = 0;
for (let i = 0; i < H; i++) {
for (let j = 0; j < W; j++) {
if (map[i][j] == 1) {
cheese++;
}
}
}
cnt.push(cheese)
const dx = [1, -1, 0, 0];
const dy = [0, 0, 1, -1];
const out = [[0, 0]];
while (out.length > 0) {
const [x, y] = out.shift();
for (let i = 0; i < 4; i++) {
const nx = x + dx[i];
const ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < H && ny < W && map[nx][ny] == 0) {
map[nx][ny] = MAX;
out.push([nx, ny])
}
}
}
while (cheese > 0) {
const melting = [];
for (let i = 0; i < H; i++) {
for (let j = 0; j < W; j++) {
if (map[i][j] == MAX)
for (let k = 0; k < 4; k++) {
const nx = i + dx[k];
const ny = j + dy[k];
if (nx >= 0 && ny >= 0 && nx < H && ny < W && map[nx][ny] == 1) {
melting.push([nx, ny])
}
}
}
}
melting.forEach(v => {
const [x, y] = v;
if (map[x][y] == 1) {
map[x][y] = MAX - 1;
out.push([x, y])
cheese--;
}
})
MAX--;
cnt.push(cheese)
while (out.length > 0) {
const [x, y] = out.shift();
for (let i = 0; i < 4; i++) {
const nx = x + dx[i];
const ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < H && ny < W && map[nx][ny] == 0) {
map[nx][ny] = MAX;
out.push([nx, ny])
}
}
}
}
const time = cnt.length - 1;
const last = cnt[time - 1];
console.log(time + '\n' + last)
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 1189번: 컴백홈 (0) | 2022.03.12 |
---|---|
Node.js) 백준 1068번: 트리 (0) | 2022.03.11 |
Node.js) 백준 14502번: 연구소 (1) | 2022.03.10 |
Node.js) 백준 2852번: NBA 농구 (0) | 2022.03.09 |
Node.js) 백준 3474번: 교수가 된 현우 (0) | 2022.03.09 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 서버개발
- 은둔청년체험
- BFS
- node.js
- 최소공통조상
- create databases;
- 서버점검
- MOD
- MySQL
- 그래프
- 동적프로그래밍
- DB 생성
- 로드나인
- 면접질문
- 다이나믹프로그래밍
- create db
- 다이나밍프로그래밍
- 면접비
- 투포인터 연습
- 개발자면접
- 투포인터
- KMP
- 롱베케이션
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
29 | 30 |
글 보관함