[BOJ 2206] ๋ฒฝ๋ถ€์ˆ˜๊ณ ์ด๋™ํ•˜๊ธฐ

 

1. ๋ฌธ์ œ : https://www.acmicpc.net/problem/2206

 

2. ํ’€์ด

๋ฒฝ์„ ๋ถ€์ˆ˜๊ณ  ๊ฐ€๋Š” ๊ฒฝ์šฐ์™€ ๋ถ€์ˆ˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด visited[][][] 3์ฐจ์›๋ฐฐ์—ด๋กœ ๊ตฌ์„ฑ

 

3. ์ฝ”๋“œ

package baekjoon;

import java.util.*;

public class ๋ฒฝ๋ถ€์ˆ˜๊ณ ์ด๋™ํ•˜๊ธฐ {

    static int[] dx = { 1, 0, -1, 0 };
    static int[] dy = { 0, 1, 0, -1 };
    static int n, m;
    static int[][] board;
    static boolean[][][] visited;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        n = scanner.nextInt();
        m = scanner.nextInt();
        scanner.nextLine();

        board = new int[n][m];
        for (int i = 0; i < n ; i++) {
            String str = scanner.nextLine();
            for (int j = 0; j < m; j++) {
                board[i][j] = Integer.valueOf(str.charAt(j)) - '0';
            }
        }

        visited = new boolean[n][m][2];
        System.out.println(bfs(0, 0));
    }

    private static int bfs(int x, int y) {
        Queue<Node> q = new LinkedList<>();
        q.add(new Node(x, y, 1, 0));
        visited[x][y][0] = true; //0์€ ๋ฒฝ์„ ๋ถ€์‹œ์ง€ ์•Š๊ณ  ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์˜ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
        visited[x][y][1] = true; //1์€ ๋ฒฝ์„ ๋ถ€์‹œ๋ฉด์„œ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์˜ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€

        while (!q.isEmpty()) {
            Node current = q.poll();

            if (current.x == n - 1 && current.y == m - 1) return current.count;

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

                if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
                    if(board[nx][ny] == 0) { //๋ฒฝ์ด ์•„๋‹ ๋•Œ
                        if (visited[nx][ny][current.wall] == false) { //ํ˜„์žฌ๊นŒ์ง€ ์˜จ ๋ฐฉ๋ฒ•(๋ฒฝ์„ ๋ถ€์‰ˆ๋Š”์ง€ ์•„๋‹Œ์ง€)์œผ๋กœ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋‹ค๋ฉด ๋ฐฉ๋ฌธํ•œ๋‹ค.
                            q.add(new Node(nx, ny, current.count + 1, current.wall));
                            visited[nx][ny][current.wall] = true;
                        }
                    }
                    else if (board[nx][ny] == 1) { //๋ฒฝ์ผ๋•Œ
                        if (current.wall == 0 && visited[nx][ny][1] == false) { //ํ˜„์žฌ๊นŒ์ง€ ๋ฒฝ์„ ๋ถ€์ˆœ์ ์ด ์—†๊ณ , ๋ฒฝ์„ ๋ถ€์ˆด์„œ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋‹ค๋ฉด ๋ฐฉ๋ฌธํ•œ๋‹ค.
                            q.add(new Node(nx, ny, current.count + 1, 1));
                            visited[nx][ny][1] = true;
                        }
                    }
                }
            }
        }

        return -1;
    }

    private static class Node {
        private int x;
        private int y;
        private int count;
        private int wall; //๋ฒฝ์„ ๋ถ€์‹œ๋ฉด์„œ ์™”๋Š”์ง€ ์•„๋‹Œ์ง€. 0์ด๋ฉด ์•„๋‹ˆ๊ณ  1์ด๋ฉด ๋ฒฝ์„ ๋ถ€์‹œ๋ฉด์„œ ์™”๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

        public Node(int x, int y, int count, int wall) {
            this.x = x;
            this.y = y;
            this.count = count;
            this.wall = wall;
        }
    }
}

'๐Ÿ’ป Coding Problems Solving > DFS | BFS | Backtracking' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[BOJ 5427] ํƒˆ์ถœ  (0) 2023.08.27
[BOJ 5427] ๋ถˆ  (0) 2023.08.24
[BOJ 16234] ์ธ๊ตฌ์ด๋™  (0) 2023.08.17
[BOJ 1103] ์Šคํƒ€ํŠธ๋งํฌ  (0) 2023.08.11
[BOJ 9466] ํ…€ ํ”„๋กœ์ ํŠธ  (0) 2023.08.01
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ