Publish:

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

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

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png

์„ค๋ช…

์žฅ์• ๋ฌผ(์œ„ ์‚ฌ์ง„์—์„œ ๊ฒ€์€ ์˜์—ญ) ์„ ํ”ผํ•ด ๊ทธ๋ž˜ํ”„ ์ขŒ์ธก ์ตœ์ƒ๋‹จ(0,0) ์—์„œ ์šฐ์ธก ์ตœํ•˜๋‹จ(n, m) ๊นŒ์ง€ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ.

ํ’€์ด

์ตœ๋‹จ ๊ฒฝ๋กœ ํƒ์ƒ‰์„ ์œ„ํ•ด bfs ๋กœ ํ’€์—ˆ๋‹ค. ๋ฌธ์ œ์—์„œ ์ž…๋ ฅ์œผ๋กœ ๊ทธ๋ž˜ํ”„ 2์ฐจ์› ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง„๋‹ค. bfs ํ•จ์ˆ˜ ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ํ•ด์•ผ ํ•œ๋‹ค.

  • ํ˜„์žฌ ์ขŒํ‘œ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
  • ๊ฒฝ๊ณ„ ์กฐ๊ฑด ์ฒ˜๋ฆฌ(isInRange)
  • ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ขŒํ‘œ๋กœ ์ด๋™ ์‹œ ์นด์šดํŠธ ์ฆ๊ฐ€ (depth + 1)
  • ์ข…๋ฃŒ ์กฐ๊ฑด:
    • ๋ฌธ์ œ์—์„œ ์šฐ์ธก ์ตœํ•˜๋‹จ(n, m) ์— ๋„๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ
    • ๊ทธ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ์š”๊ตฌํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ํ•ด๋‹น ์ขŒํ‘œ๊ฐ’์— ๋„๋‹ฌํ•˜๋ฉด ์ขŒํ‘œ ์ด๋™ ๊ฐ„ ๋ˆ„์ ๋œ depth ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด ์ค€๋‹ค.

์ฐธ๊ณ ๋กœ, ๋งค๋ฒˆ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ๊ณ„ ํ™•์ธ ๋กœ์ง์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ž‘์„ฑํ•˜๊ธฐ ๊ท€์ฐฎ๋‹ค๋ฉด Boundary Padding ๋ฐฉ๋ฒ•์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

1
2
3
 private static boolean isInRange(int x, int y) {
    return x >= 0 && x < graph.length && y >= 0 && y < graph[0].length;
  }

์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ, ๊ทธ๋ž˜ํ”„ ์ขŒํ‘œ ๊ฐ’ ์„ธํŒ… ์‹œ ๊ฒฝ๊ณ„์— ๋ถ€๋”ชํž ๊ฒƒ์„ ๋ฏธ๋ฆฌ ๊ณ ๋ คํ•ด index 1 ๋ถ€ํ„ฐ ๊ฐ’์„ ์„ธํŒ…ํ•˜๊ณ , ๋ ์ธ๋ฑ์Šค๋„ ๋„‰๋„‰ํ•˜๊ฒŒ 1์ด์ƒ ๋” ํฌ๊ฒŒ ์„ ์–ธํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ graph ๋ฅผ ์„ ์–ธํ•˜๊ณ , ์„ธํŒ…ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
// graph ํฌ๊ธฐ๋ฅผ ์• ์ดˆ์— ๋„‰๋„‰ํ•˜๊ฒŒ ์žก๋Š”๋‹ค.
graph = new int[n + 2][n + 2];

// index 1 ๋ถ€ํ„ฐ ๋„ฃ์–ด์ค€๋‹ค.
for (int i = 1; i <= N; i++) {
  String[] split = br.readLine().split("");
  for (int j = 1; j <= M; j++) {
    graph[i][j] = Integer.parseInt(split[j - 1]);
  }
}

์ตœ๋Œ€ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๊ธธ์ด๊ฐ€ 1000 ์ดํ•˜๋ฉด ๊ทธ๋ƒฅ 1000 + 10 ์ •๋„๋กœ ์„ ์–ธํ•ด๋„ ํฌ๊ฒŒ ๋ฌธ์ œ์—†๋‹ค. ๋‹จ, ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๊ธธ์ด๊ฐ€ 1000 ์ด์ƒ์„ ํ›จ์”ฌ ๋„˜์–ด๊ฐ„๋‹ค๋ฉด int[][] ๊ฐ™์€ 2์ฐจ์› ๋ฐฐ์—ด ๋ณด๋‹จ ArrayList<Integer>[] ๋กœ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ์„ ์–ธํ•˜๋ฉด ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰ ์‹œ ๊ฒฝ๊ณ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ๋ž€ ์žˆ์„ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ isInRange ๊ฐ™์€ ์กฐ๊ฑด์„ ๋งค๋ฒˆ ํ™•์ธํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

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
class Solution {
  static int[][] graph;
  static boolean[][] visited;
  static int[] dx = {-1, 1, 0, 0};
  static int[] dy = {0, 0, -1, 1};

  public int solution(int[][] maps) {
    graph = maps;
    visited = new boolean[graph.length][graph[0].length];

    return bfs();
  }

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

    while (!queue.isEmpty()) {
      int[] current = queue.poll();
      int x = current[0];
      int y = current[1];
      int depth = current[2];

      if (x == graph.length - 1 && y == graph[0].length - 1) {
        return depth;
      }

      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];

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

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

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