21번째 줄에 오타가 있습니다
3055번 - 탈출
```
const [RC, ...stringGraph] = require("fs")
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "./input.txt")
.toString()
.trim()
.split("\n");
const [R, C] = RC.split(" ").map(Number);
let graph = stringGraph.map((el) => el.trim().split(""));
const visited = Array.from(Array(R), () => Array(C).fill(false));
let needVisited = [];
let water = [];
let end = [];
const dx = [1, -1, 0, 0];
const dy = [0, 0, 1, -1];
for (let i = 0; i < R; i++) {
for (let j = 0; j < C; j++) {
if (graph[i][j] === "S") {
needVisited = [[i, j]];
visited[i][j] = 1;
}
if (graph[i][j] === "*") {
water.push([i, j]);
}
if (graph[i][j] === "D") {
end = [i, j];
}
}
}
function waterBfs() {
const turnToWater = [...water];
water = [];
while (turnToWater.length > 0) {
const [x, y] = turnToWater.shift();
for (let i = 0; i < dx.length; i++) {
const nx = x + dx[i];
const ny = y + dy[i];
if (
nx >= 0 &&
ny >= 0 &&
R > nx &&
C > ny &&
(graph[nx][ny] === "." || graph[nx][ny] === "S")
) {
water.push([nx, ny]);
graph[nx][ny] = "*";
}
}
}
}
while (needVisited.length > 0) {
waterBfs();
const len = needVisited.length;
for (let i = 0; i < len; i++) {
const [x, y] = needVisited.shift();
for (let i = 0; i < dx.length; i++) {
const nx = x + dx[i];
const ny = y + dy[i];
if (nx >= 0 && ny >= 0 && R > nx && C > ny && !visited[nx][ny]) {
if (graph[nx][ny] === "." || graph[nx][ny] === "D") {
needVisited.push([nx, ny]);
visited[nx][ny] = visited[x][y] + 1;
}
}
}
}
}
const [endX, endY] = end;
if (typeof visited[endX][endY] === "number") {
console.log(visited[endX][endY] - 1);
} else {
console.log("KAKTUS");
}
```
해당 코드로 통과했습니다 제가 "매분"마다 물이 퍼진다는 조건을 무시하고 있었더라구요
댓글을 작성하려면 로그인해야 합니다.
erictomi 1년 전
1. 물의 끝 포인트를 구해서 한 칸씩 먼저 이동시킨다
2. 고슴도치가 그 이후에 이동한다
3. 만약 visited의 비버굴의 타입이 숫자라면 해당 숫자를 출력한다
4. 비버굴의 타입이 숫자가 아니라면 KAKTUS를 출력한다
어떤 실수가 존재할까요 ?