티스토리 뷰

https://www.acmicpc.net/problem/4179

 

4179번: 불!

입력의 첫째 줄에는 공백으로 구분된 두 정수 R과 C가 주어진다. 단, 1 ≤ R, C ≤ 1000 이다. R은 미로 행의 개수, C는 열의 개수이다. 다음 입력으로 R줄동안 각각의 미로 행이 주어진다.  각각의 문

www.acmicpc.net

 

억지로 풀었다.  나중에 다시 풀어봐야지.

 

class Node {
  constructor(item) {
    this.item = item;
    this.next = null;
  }
}

class Queue {
  constructor() {
    this.head = null;
    this.tail = null;
    this.length = 0;
  }

  push(item) {
    const node = new Node(item)
    if (this.head == null) {
      this.head = node;
    } else {
      this.tail.next = node;
    }

    this.tail = node;
    this.length += 1;
  }

  pop() {
    const popItem = this.head;
    this.head = this.head.next;
    this.length -= 1;
    return popItem.item;
  }
}
const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n");
const [N, M] = input.shift().trim().split(' ').map(Number);
let board = input.map(v => v.trim().split(''))

const dx = [0, 0, -1, 1]
const dy = [-1, 1, 0, 0]
let answer = Infinity
let visited = Array.from(Array(N), () => Array(M).fill(false))

function findJH() {
  for (let i = 0; i < N; i++) {
    for (let j = 0; j < M; j++) {
      if (board[i][j] == 'J') {
        return [i, j]
      }
    }
  }
}

function findFire() {
  const fire = [];
  for (let i = 0; i < N; i++) {
    for (let j = 0; j < M; j++) {
      if (board[i][j] == 'F') {
        fire.push([i, j])
      }
    }
  }
  return fire
}


function isExit(i, j) {
  if (i == 0 || i == N - 1 || j == 0 || j == M - 1) {
    return true;
  }
  return false;
}





const [jx, jy] = findJH();
board[jx][jy] = '.'
let fire = findFire();
let f = new Queue();
let j = new Queue();




let jh = [[jx, jy, 1]];
visited[jx][jy] = true;

let flag = true;
while (jh.length > 0 && flag) {
  while (jh.length > 0) {
    const [jx, jy, cnt] = jh.shift();
    if (board[jx][jy] == 'F') continue;
    if (isExit(jx, jy)) {
      answer = cnt;
      flag = false;
    } else {
      j.push([jx, jy, cnt])
    }
  }
  if (!flag) break;



  while (fire.length > 0) {
    f.push(fire.shift())
  }
  while (f.length > 0) {
    const [fx, fy] = f.pop();
    for (let i = 0; i < 4; i++) {
      const nfx = fx + dx[i];
      const nfy = fy + dy[i];
      if (nfx >= 0 && nfy >= 0 && nfx < N && nfy < M && board[nfx][nfy] == '.') {
        board[nfx][nfy] = 'F';
        fire.push([nfx, nfy]);
      }
    }
  }


  while (j.length > 0) {

    const [jx, jy, cnt] = j.pop();
    for (let i = 0; i < 4; i++) {
      const njx = jx + dx[i];
      const njy = jy + dy[i];
      if (njx >= 0 && njy >= 0 && njx < N && njy < M && !visited[njx][njy] && board[njx][njy] == '.') {
        jh.push([njx, njy, cnt + 1]);
        visited[njx][njy] = true;
      }
    }
  }
}

if (answer == Infinity) console.log("IMPOSSIBLE")
else console.log(answer)

 

728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
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
글 보관함