4485. ๋…น์ƒ‰ ์˜ท ์ž…์€ ์• ๊ฐ€ ์ ค๋‹ค์ง€?

2025. 1. 19. 16:09ยท์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ

Problem ๐Ÿ’ป

4485. ๋…น์ƒ‰ ์˜ท ์ž…์€ ์• ๊ฐ€ ์ ค๋‹ค์ง€?

 

๋งํฌ๊ฐ€ ‘๋„๋‘‘๋ฃจํ”ผ’๋กœ ๊ฐ€๋“์ฐฌ ๋™๊ตด์—์„œ (0, 0) → (n-1, n-1)๊นŒ์ง€ ์ตœ์†Œํ•œ์˜ ๋ฃจํ”ผ๋ฅผ ์žƒ์œผ๋ฉด์„œ ๋„์ฐฉํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋™๊ตด์˜ ๊ฐ ์นธ๋งˆ๋‹ค ๋„๋‘‘๋ฃจํ”ผ์˜ ๊ฐ€์น˜๊ฐ€ ์ฃผ์–ด์ง€๊ณ  ํ•ด๋‹น ๊ฐ€์น˜๊ฐ€ ํด์ˆ˜๋ก ์žƒ๋Š” ๋ˆ์ด ๋งŽ์•„์ง€๋Š” ๊ฒƒ์ด๋‹ค.


 

Approach 1 โญ•

  • ๋‹ค์ต์ŠคํŠธ๋ผ (00:13:12)

 

์ด ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์€ ‘๋„๋‘‘๋ฃจํ”ผ’๋ฅผ ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ ์ƒ๊ฐํ•ด ํ•ด๋‹น ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ‘๊ฐ€์ค‘์น˜’๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ•ด๋‹น ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋ชจ๋“  ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๋„๋‘‘๋ฃจํ”ผ์˜ ๊ฐ€์น˜๋กœ ๊ณ ์ •๋˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿผ, ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ‘๋‹ค์ต์ŠคํŠธ๋ผ’๋ฅผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค.


 

Approach 2 โญ•

  • BFS (00:10:27)

 

๋‘ ๋ฒˆ์งธ๋กœ๋Š” BFS๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ๊ฐ ์นธ์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ ๋„๋‘‘๋ฃจํ”ผ์˜ ํ•ฉ์ด ํ˜„์žฌ ์นธ์— ์ €์žฅ๋œ ํ•ฉ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ํ•ด๋‹น ํ•ฉ์œผ๋กœ ๊ฐฑ์‹ ํ•˜๋ฉด์„œ ์ตœ์†Œ ๋„๋‘‘๋ฃจํ”ผ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

2
5 5
3 9

์ด๋Ÿฌํ•œ ๋™๊ตด์ด ์žˆ์„ ๋•Œ ์ฒ˜์Œ์— (0, 0) → (0, 1) → (1, 1)๋กœ ๊ฐ€๋ฉด 24์˜ ๋ฃจํ”ผ๋ฅผ ์žƒ๊ฒŒ๋˜์ง€๋งŒ (0, 0) → (1, 0) → (1, 1) ๊ฒฝ๋กœ๋กœ ๊ฐ€๊ฒŒ ๋˜๋ฉด 17๋งŒํผ์˜ ๋ฃจํ”ผ๋ฅผ ์žƒ๊ฒŒ๋˜๋ฏ€๋กœ ์ด๋ฅผ ๊ฐฑ์‹ ์‹œ์ผœ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

๋งŒ์•ฝ ๋ฐ˜๋Œ€๋กœ (0, 0) → (1, 0) → (1, 1) ๊ฒฝ๋กœ๋กœ ๋จผ์ € ๊ฐ”๋‹ค๊ฐ€ (0, 0) → (0, 1) → (1, 1) ๊ฒฝ๋กœ๋กœ ์˜จ๋‹ค๋ฉด ๊ฐฑ์‹ ํ•˜์ง€ ์•Š๊ณ  ์ฐจ๋ก€๋ฅผ ๋„˜๊ธด๋‹ค.

 

์ด์ฒ˜๋Ÿผ visited ๋ฐฐ์—ด์„ ์›๋ž˜ bfs์ฒ˜๋Ÿผ ๊ทธ ์นธ์„ ๊ฐ€๊ธฐ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋กœ ๋ณด์ง€์•Š๊ณ  ๊ทธ ์นธ์„ ๊ฐ€๊ธฐ๊นŒ์ง€์˜ ์ตœ์†Œ ๋„๋‘‘๋ฃจํ”ผ ๊ฐ€์น˜๋กœ ๋ณด๊ณ  ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์ด๋‹ค.


 

Solution ๐Ÿ’ก

// ๋‹ค์ต์ŠคํŠธ๋ผ
public class BOJ_4485 {

    private static int[] dy = {-1, 1, 0, 0};
    private static int[] dx = {0, 0, -1, 1};

    private static int[][] dungeon;
    private static int n;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int t = 1;
        while ((n = Integer.parseInt(br.readLine())) != 0) {
            dungeon = new int[n][n];

            for (int y = 0; y < n; y++) {
                StringTokenizer st = new StringTokenizer(br.readLine());
                for (int x = 0; x < n; x++) dungeon[y][x] = Integer.parseInt(st.nextToken());
            }

            int[][] shortest = dijkstra(0, 0);
            System.out.println("Problem " + t++ + ": " + shortest[n-1][n-1]);
        }
    }

    public static int[][] dijkstra(int y, int x) {
        int[][] shortest = new int[n][n];
        for (int i = 0; i < n; i++) Arrays.fill(shortest[i], 987654321);
        shortest[y][x] = dungeon[y][x];

        boolean[][] visited = new boolean[n][n];

        while (true) {
            int smallY = 0;
            int smallX = 0;
            int value = Integer.MAX_VALUE;
            for (int yy = 0; yy < n; yy++) {
                for (int xx = 0; xx < n; xx++) {
                    if (!visited[yy][xx] && value > shortest[yy][xx]) {
                        value = shortest[yy][xx];
                        smallY = yy;
                        smallX = xx;
                    }
                }
            }

            if (smallY == n-1 && smallX == n-1) break;
            visited[smallY][smallX] = true;

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

                if (ny < 0 || ny >= n || nx < 0 || nx >= n) continue;
                if (visited[ny][nx]) continue;

                shortest[ny][nx] = Math.min(shortest[smallY][smallX] + dungeon[ny][nx], shortest[ny][nx]);
            }
        }

        return shortest;
    }
}
// BFS
public class BOJ_4485_2 {

    private static int[] dy = {-1, 1, 0, 0};
    private static int[] dx = {0, 0, -1, 1};

    private static int[][] dungeon;
    private static int[][] visited;
    private static int n;

    private static class Point {
        int y, x;

        public Point(int y, int x) {
            this.y = y;
            this.x = x;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int t = 1;
        while ((n = Integer.parseInt(br.readLine())) != 0) {
            dungeon = new int[n][n];
            visited = new int[n][n];
            for (int i = 0; i < n; i++) Arrays.fill(visited[i], 987654321);

            for (int y = 0; y < n; y++) {
                StringTokenizer st = new StringTokenizer(br.readLine());
                for (int x = 0; x < n; x++) dungeon[y][x] = Integer.parseInt(st.nextToken());
            }

            bfs(0, 0);
            System.out.printf("Problem %d: %d\\n", t++, visited[n-1][n-1]);
        }
    }

    private static void bfs(int y, int x) {
        ArrayDeque<Point> q = new ArrayDeque<>();
        q.add(new Point(y, x));

        visited[y][x] = dungeon[y][x];
        while (!q.isEmpty()) {
            Point p = q.poll();

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

                if (ny < 0 || ny >= n || nx < 0 || nx >= n) continue;
                if (visited[ny][nx] <= visited[p.y][p.x] + dungeon[ny][nx]) continue;

                visited[ny][nx] = visited[p.y][p.x] + dungeon[ny][nx];
                q.add(new Point(ny, nx));
            }
        }
    }
}

 

์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

14658. ํ•˜๋Š˜์—์„œ ๋ณ„๋˜ฅ๋ณ„์ด ๋น—๋ฐœ์นœ๋‹ค  (1) 2025.01.29
1863. ์Šค์นด์ด๋ผ์ธ ์‰ฌ์šด๊ฑฐ  (0) 2025.01.22
1238. ํŒŒํ‹ฐ  (0) 2025.01.17
ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜  (0) 2025.01.17
ํŽœ์œ…ํŠธ๋ฆฌ  (3) 2024.10.03
'์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • 14658. ํ•˜๋Š˜์—์„œ ๋ณ„๋˜ฅ๋ณ„์ด ๋น—๋ฐœ์นœ๋‹ค
  • 1863. ์Šค์นด์ด๋ผ์ธ ์‰ฌ์šด๊ฑฐ
  • 1238. ํŒŒํ‹ฐ
  • ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜
์œจ๋ฌด;
์œจ๋ฌด;
  • ์œจ๋ฌด;
    ๐ŸฅŠ
    ์œจ๋ฌด;
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (65)
      • ๊ฐœ๋ฐœ (30)
        • ์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค (13)
        • ์šด์˜์ฒด์ œ (12)
      • ๊ฐœ๋ฐœ์„œ์  (2)
        • ์ž๋ฐ”-์Šคํ”„๋ง ์‹ค์šฉ์ฃผ์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ (2)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ (28)
      • ๊ฒŒ์ž„๊ฐœ๋ฐœ (5)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    ๋ฐฑ์ค€
    ์…€๋ฃฐ๋Ÿฌ์˜คํ† ๋งˆํƒ€
    dfs
    ๋กœ๊ทธ
    ๊ฐœ๋ฐœ
    bsp
    ์ด๋™์ƒ์„ฑ์ž
    2048(Hard)
    ์ธ๋””๊ฒŒ์ž„
    ์ ˆ์ฐจ์ ์ƒ์„ฑ
    python
    BFS
    C++
    ์šฐํ…Œ์ฝ”
    ์ฝ”๋”ฉ
    2048
    ๊ฒŒ์ž„๊ฐœ๋ฐœ
    ์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค
    ๊ฒŒ์ž„
    ์ž๋ฐ”
    ์ด๊ฒƒ์ดC++์ด๋‹ค
    ๋กœ๊ทธ๋ผ์ดํฌ
    ํŒŒ์ด์ฌ
    ์•Œ๊ณ ๋ฆฌ์ฆ˜
    ๊ฐœ๋ฐœ์—ฐ์Šต
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
์œจ๋ฌด;
4485. ๋…น์ƒ‰ ์˜ท ์ž…์€ ์• ๊ฐ€ ์ ค๋‹ค์ง€?
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”