๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿง  ์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฆฌ์ฝ”์ณ‡ ๋กœ๋ด‡ JS

by megan07 2024. 1. 3.



๋ฌธ์ œ๋งํฌ 

 

ํ’€์ด ๋ฐฉ๋ฒ•

bfs๋กœ ์ ‘๊ทผํ•˜๊ธด ํ–ˆ๋Š”๋ฐ
ํ์— ๋„ฃ์–ด์•ผ ํ•˜๋Š” ๋…ธ๋“œ๋“ค๊ณผ ๋ฐฉ๋ฌธ ์ฒดํฌํ•ด์•ผ ํ•˜๋Š” ๋…ธ๋“œ๋“ค์„ ์ž˜๋ชป ์ƒ๊ฐํ•ด์„œ ๊ณ„์† ์˜ค๋‹ต์ฒ˜๋ฆฌ๊ฐ€ ๋‚˜๋”๋ผ๊ตฌ์š”

์Šคํ„ฐ๋””์›๋‹˜์˜ ํ’€์ด ๋ณด๊ณ  ๊ฐ์„ ์žก์•„์„œ
๋‹ค์‹œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!

์ด์ „์— ํ’€์—ˆ๋˜ bfs์™€ ๋‹ค๋ฅด๊ฒŒ ์ง€๋‚˜์นœ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ
๋์— ๋‹ค๋‹ค๋ฅธ ๋…ธ๋“œ๋งŒ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์„œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค

1. ์ถœ๋ฐœ์ ๊ณผ ๋ชฉํ‘œ์ ์˜ ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
2. ์‹œ์ž‘์ ์„ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌํ•˜๊ณ  ํ์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.
3. ํ์—์„œ ๊บผ๋‚ธ ์ง€์ ๋ถ€ํ„ฐ ์ƒํ•˜์ขŒ์šฐ๋กœ ๋Œ๋ฉด์„œ ํ•œ ๋ฑกํ–ฅ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋งŒํผ ๋๊นŒ์ง€ ๊ฐˆ ๊ฒƒ
4. ๋„์ฐฉํ•œ ๋์ง€์ ์„ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ƒํƒœ๋ฉด cnt+1ํ•˜๊ณ  ํ์— ๋„ฃ์–ด์คŒ
   (์ง€๋‚˜๊ฐ€๋Š” ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ ํ•˜๋ฉด ์•ˆ๋จ!! ์™œ๋ƒํ•˜๋ฉด ์ง€๋‚˜๊ฐ”๋˜ ๋…ธ๋“œ๋“ค์„ ๋‹ค์‹œ ์ง€๋‚˜๊ฐ€์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌ, ํ•˜์ง€๋งŒ ์ด๋ฏธ ํ•œ ๋ฒˆ ๋ฐฉ๋ฌธํ•œ ๋์ง€์ ์€ ๊ฐˆ ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ)

 

 

1. ๋‚ด ํ’€์ด

function solution(board) {
  const dr = [-1, 1, 0, 0];
  const dc = [0, 0, -1, 1];

  let start;
  let goal;

  const r_length = board.length;
  const c_length = board[0].length;
  const visited = Array.from(new Array(r_length), () =>
    new Array(c_length).fill(false),
  );

  const q = [];

  //1. ์ถœ๋ฐœ์ ๊ณผ ๋ชฉํ‘œ์ ์˜ ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  for (let r = 0; r < r_length; r++) {
    for (let c = 0; c < c_length; c++) {
      if (board[r][c] === 'R') {
        start = [r, c];
      }
      if (board[r][c] === 'G') {
        goal = [r, c];
      }
    }
  }

  //์‹œ์ž‘์ ์„ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌํ•˜๊ณ  ํ์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.
  visited[start[0]][start[1]] = true;
  q.push({ current_r: start[0], current_c: start[1], cnt: 0 });

  //ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
  while (q.length) {
    const { current_r, current_c, cnt } = q.shift();

    //๋ชฉํ‘œ์ ์— ๋„์ฐฉํ•˜๋ฉด ๋ฆฌํ„ด
    if (current_r === goal[0] && current_c === goal[1]) {
      return cnt;
    }

    //์ƒํ•˜์ขŒ์šฐ๋กœ ๋Œ๋ฉด์„œ ํ•œ ๋ฑกํ–ฅ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋งŒํผ ๋๊นŒ์ง€ ๊ฐˆ ๊ฒƒ
    for (let i = 0; i < 4; i++) {
      let next_r = current_r;
      let next_c = current_c;

      //๋ฐฐ์—ด์˜ ๋์— ๋‹ค๋‹ค๋ฅด๊ฑฐ๋‚˜ ๋ฐฉํ•ด๋ฌผ์ด ์กด์žฌํ•˜๋ฉด ๋ฉˆ์ถค
      while (
        next_r + dr[i] >= 0 &&
        next_r + dr[i] < r_length &&
        next_c + dc[i] >= 0 &&
        next_c + dc[i] < c_length &&
        board[next_r + dr[i]][next_c + dc[i]] !== 'D'
      ) {
        next_r += dr[i];
        next_c += dc[i];
      }

      //๋„์ฐฉํ•œ ๋์ง€์ ์„ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ƒํƒœ๋ฉด cnt+1ํ•˜๊ณ  ํ์— ๋„ฃ์–ด์คŒ
      if (!visited[next_r][next_c]) {
        visited[next_r][next_c] = true;
        q.push({ current_r: next_r, current_c: next_c, cnt: cnt + 1 });
      }
    }
  }

  return -1;
}

 


bfs์—์„œ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ์–ด๋–ค ์‹์œผ๋กœ ํ•ด์•ผ ํ•˜๋Š”์ง€
์ƒˆ๋กœ์šด ์œ ํ˜•์„ ํ•˜๋‚˜ ๋” ์•Œ๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™์•„์š”