Publish:

ํƒœ๊ทธ: , , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png

์„ค๋ช…

img

์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ โ€œRโ€ ์—์„œ ์ถœ๋ฐœํ•ด ์ƒํ•˜์ขŒ์šฐ๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด์ „๊นŒ์ง€ ํ’€์–ด๋ดค๋˜ BFS/DFS ๋ฌธ์ œ์™€ ๋‹ค๋ฅธ์ ์€ ํ•œ ์นธ์”ฉ ์›€์ง์ด๋ฉด์„œ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํƒ์ƒ‰ํ•ด๊ฐ€๋Š” ๋ฐฉ์‹์ด ์•„๋‹ˆ๋ผ

  • โ€œDโ€ ๋ฅผ ๋งŒ๋‚ ๋•Œ ๊นŒ์ง€ ์›€์ง์ด๊ฑฐ๋‚˜
  • ๊ฒฉ์ž์˜ ๋ ๋ถ€๋ถ„์„ ๋งŒ๋‚ ๋•Œ ๊นŒ์ง€ ์›€์ง์ธ๋‹ค.

์˜ˆ์‹œ์˜ ๊ฒฝ์šฐ ๋นจ๊ฐ„ ํ™”์‚ดํ‘œ ๋Œ€๋กœ ์ด 7๋ฒˆ ์›€์ง์ด๋ฉด โ€œRโ€ ์—์„œ โ€œGโ€ ๊นŒ์ง€ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ๊ฐ€ ๋œ๋‹ค.

ํ’€์ด

์ด์ „๊นŒ์ง€์˜ ๋ฌธ์ œ๋Š” ์ฃผ๋กœ DFS ๋กœ ํ’€์—ˆ๋Š”๋ฐ ์ด๋ฒˆ ๋ฌธ์ œ๋Š” BFS ๋กœ ํ‘ธ๋Š” ๊ฒƒ์ด ๋” ์ ์ ˆํ•˜๋‹ค. DFS ๋กœ ํ‘ธ๋Š” ๊ฒฝ์šฐ ์ตœ๋‹จ ๊ฒฝ๋กœ์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋กœ์ง์ด ์ถ”๊ฐ€๋กœ ํ•„์š”ํ•˜๋‹ค.

ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•  โ€œRโ€ ์˜ ์œ„์น˜๋ฅผ ์ฐพ์€ ํ›„ BFS ๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค. bfs ํ•จ์ˆ˜์—์„œ๋Š” โ€œRโ€ ์—์„œ ์ถœ๋ฐœํ•ด โ€œGโ€ ์— ๋„๋‹ฌํ•  ๋•Œ ๊นŒ์ง€์˜ ์ •์  ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ์ •์  ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•  ๋• dist ๋ณ€์ˆ˜๋ฅผ dfs ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ bfs ์˜ ๊ฒฝ์šฐ queue ์— ๋„ฃ์–ด์ฃผ๋Š” ์‹์œผ๋กœ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค.

์žฅ์• ๋ฌผ์„ ๋งŒ๋‚˜๊ฑฐ๋‚˜ ๋ฒฝ์— ๋„๋‹ฌํ• ๋•Œ ๊นŒ์ง€ ์ญ‰ ์ด๋™ํ•œ ํ›„, ๊ทธ ๋…ธ๋“œ๊ฐ€ ์•„์ง ๋ฐฉ๋ฌธ ์ „์ด๋ผ๋ฉด ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๊ณ  ๋‹ค์Œ ํƒ์ƒ‰์„ ์œ„ํ•ด queue ์— ์ขŒํ‘œ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค. ์ด๋•Œ, dist + 1 ์„ ๋„˜๊ฒจ์ค˜์„œ ๋…ธ๋“œ ๊ฐ„ ์ด๋™ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•ด ์ค€๋‹ค.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Solution {
  static String[][] graph;
  static boolean[][] visited;
  static int[] dx = {-1, 1, 0, 0};
  static int[] dy = {0, 0, -1, 1};
  static int n;

  public int solution(String[] board) {
    n = board.length;
    graph = new String[n][];
    visited = new boolean[n][];

    int x = 0, y = 0;
    for (int i = 0; i < n; i++) {
      graph[i] = new String[board[i].length()];
      visited[i] = new boolean[board[i].length()];
      char[] charArray = board[i].toCharArray();
      for (int j = 0; j < charArray.length; j++) {
        String s = String.valueOf(charArray[j]);
        graph[i][j] = s;
        if (s.equals("R")) {
          x = i;
          y = j;
        }
      }
    }
    return bfs(x, y);
  }

  private int bfs(int x, int y) {
    Queue<int[]> queue = new LinkedList<>();
    queue.add(new int[] {x, y, 0});
    visited[x][y] = true;

    while (!queue.isEmpty()) {
      int[] current = queue.poll();
      int cx = current[0];
      int cy = current[1];
      int dist = current[2];

      if (graph[cx][cy].equals("G")) {
        return dist;
      }

      for (int i = 0; i < 4; i++) {
        int nx = cx;
        int ny = cy;

        while (isInRange(nx + dx[i], ny + dy[i]) && !graph[nx + dx[i]][ny + dy[i]].equals("D")) {
          nx += dx[i];
          ny += dy[i];
        }

        if (!visited[nx][ny]) {
          visited[nx][ny] = true;
          queue.add(new int[] {nx, ny, dist + 1});
        }
      }
    }
    return -1;
  }

  private boolean isInRange(int x, int y) {
    return x >= 0 && x < n && y >= 0 && y < graph[x].length;
  }
}
๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ