erictomi   1년 전

1. 물의 끝 포인트를 구해서 한 칸씩 먼저 이동시킨다

2. 고슴도치가 그 이후에 이동한다

3. 만약 visited의 비버굴의 타입이 숫자라면 해당 숫자를 출력한다

4. 비버굴의 타입이 숫자가 아니라면 KAKTUS를 출력한다

어떤 실수가 존재할까요 ?

siyamaki   1년 전

21번째 줄에 오타가 있습니다

erictomi   1년 전

시작점을 1로 초기화 후 visited에 방문한 곳의 거리수를 계산하기 위한 초기화인데 어떤 오타일까요..?

siyamaki   1년 전

visited[i][j] ===1 는 비교연산자 아닌가요? 해당 줄을 실행하면 아무 일도 일어나지 않는걸로 아는데요

erictomi   1년 전

아 그러네요 ㅋㅋ... 죄송합니다 시도해보겠습니다!

erictomi   1년 전

```
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");
}
```

해당 코드로 통과했습니다 제가 "매분"마다 물이 퍼진다는 조건을 무시하고 있었더라구요

댓글을 작성하려면 로그인해야 합니다.