๐ก ์ ๊ทผ๋ฐฉ์
- ๊ฐ์ฅ ๋นจ๋ฆฌ ํ์ถ ํ ์ ์๋ ์๊ฐ
- ๋ถ์ ์ธ์ ์์ญ์ผ๋ก ์ด๋
-> BFS!!
1. ๋ถ์ด ๋ฒ์ง๋ ์๊ฐ์ ๋จผ์ bfs๋ก ๊ตฌํ๊ณ
2. ์งํ์ด๊ฐ ์ด๋ํ๋ ์๊ฐ์ด ๋ถ์ด ๋ฒ์ง๋ ์๊ฐ๋ณด๋ค ์งฆ์ผ๋ฉด ์ด๋ ๊ฐ๋ฅ
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue {
constructor() {
this.first = null;
this.last = null;
this.size = 0;
}
enqueue(val) {
const newNode = new Node(val);
if (!this.first) {
this.first = newNode;
this.last = newNode;
} else {
this.last.next = newNode;
this.last = newNode;
}
return ++this.size;
}
dequeue() {
if (!this.first) return null;
const tmp = this.first;
if (this.first === this.last) {
this.last = null;
}
this.first = this.first.next;
this.size--;
return tmp.value;
}
}
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
//์
๋ ฅ๊ฐ 1๊ฐ(1์ค)
// const input = require('fs').readFileSync(filePath).toString().trim();
//์
๋ ฅ๊ฐ ์ฌ๋ฌ๊ฐ (1์ค)
// let input = require('fs').readFileSync(filePath).toString().trim().split(' ');
//์
๋ ฅ๊ฐ ์ฌ๋ฌ ์ค
// let input = require('fs').readFileSync(filePath).toString().trim().split('\n');
// ์
๋ ฅ๊ฐ์ด ์ฒซ ๋ฒ์งธ ์ค์๋ ์
๋ ฅ ๊ฐ์ ๊ธธ์ด(n), n๊ฐ์ ์ค์ ๊ฑธ์ณ์ ํ ์ค์ ํ๋์ ์
๋ ฅ๊ฐ์ด ์ฃผ์ด์ง ๋
const [n, ...input] = require('fs')
.readFileSync(filePath)
.toString()
.trim()
.split('\n');
function solution(row, col, maze) {
const queue = new Queue();
const fireQueue = new Queue();
const dr = [1, -1, 0, 0];
const dc = [0, 0, 1, -1];
let min = Infinity;
const fireMaze = [];
maze.forEach((row) => {
fireMaze.push([...row]);
});
let start = null;
let fire = null;
for (let i = 0; i < row; i++) {
for (let j = 0; j < col; j++) {
if (maze[i][j] === 'J') {
start = { r: i, c: j, cnt: 0 };
} else if (maze[i][j] === 'F') {
fire = { r: i, c: j, cnt: 0 };
fireQueue.enqueue({ r: i, c: j, cnt: 0 });
}
}
}
//๋ฐ๋ก ํ์ถ ๊ฐ๋ฅํ ์กฐ๊ฑด
if (
start.r === 0 ||
start.r === row - 1 ||
start.c === 0 ||
start.c === col - 1
) {
return 1;
}
//๋ถ์ด ๋ฒ์ง๋ ์๋ bfs๋ก ๋จผ์ ๊ตฌํ๊ธฐ
while (fireQueue.size) {
const { r, c, cnt } = fireQueue.dequeue();
for (let i = 0; i < 4; i++) {
if (
r + dr[i] >= 0 &&
r + dr[i] < row &&
c + dc[i] >= 0 &&
c + dc[i] < col &&
fireMaze[r + dr[i]][c + dc[i]] === '.'
) {
const tmp = { r: r + dr[i], c: c + dc[i], cnt: (cnt || 0) + 1 };
fireMaze[r + dr[i]][c + dc[i]] = tmp.cnt;
fireQueue.enqueue({ ...tmp });
}
}
}
//์งํ์ด ํ์ถ bfs ๊ตฌํ๊ธฐ
queue.enqueue(start);
while (queue.size) {
const { r, c, cnt } = queue.dequeue();
for (let i = 0; i < 4; i++) {
if (
r + dr[i] >= 0 &&
r + dr[i] < row &&
c + dc[i] >= 0 &&
c + dc[i] < col &&
maze[r + dr[i]][c + dc[i]] === '.'
) {
const tmp = { r: r + dr[i], c: c + dc[i], cnt: cnt + 1 };
//๋ถ์ด ์๋ ๊ฒฝ์ฐ
if (!fire) {
maze[r + dr[i]][c + dc[i]] = { ...tmp };
queue.enqueue({ ...tmp });
}
//๋ถ์ด ์๋๋ฐ ๋ฒฝ์ ๋งํ์ ์ํฅ์ ๋ฏธ์น์ง ์๋ ๊ฒฝ์ฐ, ๋ถ์ด ์ํฅ์ ๋ฏธ์น๋ ๊ฒฝ์ฐ
else if (
fireMaze[tmp.r][tmp.c] === '.' ||
tmp.cnt < fireMaze[tmp.r][tmp.c]
) {
maze[r + dr[i]][c + dc[i]] = { ...tmp };
queue.enqueue({ ...tmp });
}
}
}
}
for (let i = 0; i < row; i++) {
for (let j = 0; j < col; j++) {
if (
typeof maze[i][j] === 'object' &&
(i === 0 || i === row - 1 || j === 0 || j === col - 1)
) {
min = Math.min(min, maze[i][j]['cnt']);
}
}
}
return min !== Infinity ? min + 1 : 'IMPOSSIBLE';
}
const nArr = n.split(' ').map(Number);
const answer = solution(
nArr[0],
nArr[1],
input.map((row) => row.split('')),
);
console.log(answer);
'๐ง ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 2573 ๋น์ฐ js ํ์ด (1) | 2023.12.28 |
---|---|
[๋ฐฑ์ค] 7569 ํ ๋งํ js ํ์ด (1) | 2023.12.28 |
๋ฐฑ์ค JS ์ ๋ ฅ๊ฐ ํ ํ๋ฆฟ(VSCode ์ค๋ํซ์ผ๋ก ์ฝ๊ฒ ์ฌ์ฉํ๊ธฐ) (0) | 2023.12.27 |
[ํ๋ก๊ทธ๋๋จธ์ค]๊ด๋ฌผ ์บ๊ธฐ JS (0) | 2023.12.27 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์์์ฐพ๊ธฐ JS (0) | 2023.12.27 |