|
| 1 | +const fs = require("fs"); |
| 2 | +const filePath = |
| 3 | + process.platform === "linux" ? "/dev/stdin" : "./서정우/input.txt"; |
| 4 | +const input = fs.readFileSync(filePath).toString().trim().split("\n"); |
| 5 | + |
| 6 | +const [N, M] = input[0].split(" ").map(Number); |
| 7 | +const map = input.slice(1).map((row) => row.split(" ").map(Number)); |
| 8 | + |
| 9 | +const isOutOfMap = (x, y, N, M) => { |
| 10 | + if (x >= M || x < 0 || y >= N || y < 0) { |
| 11 | + return true; |
| 12 | + } else { |
| 13 | + return false; |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +const q = [[0, 0, 0]]; |
| 18 | +const visited = Array.from(Array(N), () => Array(M).fill(false)); |
| 19 | +const result = []; |
| 20 | + |
| 21 | +while (q.length > 0) { |
| 22 | + // console.log(q); |
| 23 | + const node = q.shift(); |
| 24 | + const [x, y, count] = node; |
| 25 | + |
| 26 | + const boosters = map[y][x]; |
| 27 | + |
| 28 | + for (let b = 1; b <= boosters; b++) { |
| 29 | + if (!isOutOfMap(x + b, y, N, M)) { |
| 30 | + if (!visited[y][x + b]) { |
| 31 | + q.push([x + b, y, count + 1]); |
| 32 | + visited[y][x + b] = true; |
| 33 | + } |
| 34 | + if (x + b === M - 1 && y === N - 1) result.push(count + 1); |
| 35 | + } |
| 36 | + if (!isOutOfMap(x, y + b, N, M)) { |
| 37 | + if (!visited[y + b][x]) { |
| 38 | + q.push([x, y + b, count + 1]); |
| 39 | + visited[y + b][x] = true; |
| 40 | + } |
| 41 | + if (x === M - 1 && y + b === N - 1) result.push(count + 1); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +console.log(Math.min(...result)); |
0 commit comments