λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
μ•Œκ³ λ¦¬μ¦˜

[Java] BOJ 2178 λ―Έλ‘œνƒμƒ‰

by 🍊귀🍊 2024. 8. 22.

문제 

문제 링크 https://www.acmicpc.net/problem/2178

  • N×M크기의 λ°°μ—΄λ‘œ ν‘œν˜„λ˜λŠ” λ―Έλ‘œκ°€ μžˆλ‹€.
    1 0 1 1 1 1
    1 0 1 0 1 0
    1 0 1 0 1 1
    1 1 1 0 1 1

  • λ―Έλ‘œμ—μ„œ 1은 이동할 수 μžˆλŠ” 칸을 λ‚˜νƒ€λ‚΄κ³ , 0은 이동할 수 μ—†λŠ” 칸을 λ‚˜νƒ€λ‚Έλ‹€.
  • μ΄λŸ¬ν•œ λ―Έλ‘œκ°€ μ£Όμ–΄μ‘Œμ„ λ•Œ, (1, 1)μ—μ„œ μΆœλ°œν•˜μ—¬ (N, M)의 μœ„μΉ˜λ‘œ 이동할 λ•Œ μ§€λ‚˜μ•Ό ν•˜λŠ” μ΅œμ†Œμ˜ μΉΈ 수λ₯Ό κ΅¬ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€. ν•œ μΉΈμ—μ„œ λ‹€λ₯Έ 칸으둜 이동할 λ•Œ, μ„œλ‘œ μΈμ ‘ν•œ 칸으둜만 이동할 수 μžˆλ‹€.
  • μœ„μ˜ μ˜ˆμ—μ„œλŠ” 15칸을 μ§€λ‚˜μ•Ό (N, M)의 μœ„μΉ˜λ‘œ 이동할 수 μžˆλ‹€. 칸을 μ…€ λ•Œμ—λŠ” μ‹œμž‘ μœ„μΉ˜μ™€ 도착 μœ„μΉ˜λ„ ν¬ν•¨ν•œλ‹€.
  • 첫째 쀄에 두 μ •μˆ˜ N, M(2 ≤ N, M ≤ 100)이 주어진닀. λ‹€μŒ N개의 μ€„μ—λŠ” M개의 μ •μˆ˜λ‘œ λ―Έλ‘œκ°€ 주어진닀. 각각의 μˆ˜λ“€μ€ λΆ™μ–΄μ„œ μž…λ ₯으둜 주어진닀.
  • 첫째 쀄에 μ§€λ‚˜μ•Ό ν•˜λŠ” μ΅œμ†Œμ˜ μΉΈ 수λ₯Ό 좜λ ₯ν•œλ‹€. 항상 λ„μ°©μœ„μΉ˜λ‘œ 이동할 수 μžˆλŠ” 경우만 μž…λ ₯으둜 주어진닀.

아이디어

  • μ΅œλ‹¨ 거리λ₯Ό κ΅¬ν•˜λŠ” λ¬Έμ œμ΄λ―€λ‘œ BFSλ₯Ό μ΄μš©ν•˜μ—¬ μƒν•˜μ’Œμš° 탐색을 톡해 λ°°μ—΄ λ²”μœ„κ°€ λ„˜μ–΄κ°€μ§€ μ•ŠλŠ”λ‹€λ©΄ 방문여뢀와 갈 수 μžˆλŠ” 길인지 νŒλ‹¨ ν›„, κΈ°μ€€ λ…Έλ“œλ₯Ό κ°€λŠ”λ° μ΄λ™ν•œ νšŸμˆ˜μ— 1을 λ”ν•œλ§ŒνΌμ„ μ΄λ™ν•œ μ’Œν‘œκ°’μ˜ μ΄λ™νšŸμˆ˜μ— μ €μž₯ν•œλ‹€.
  • 도착지에 λ„λ‹¬ν• λ•ŒκΉŒμ§€ Queue에 λ„£μœΌλ©΄μ„œ λ°˜λ³΅ν•œλ‹€. 

κ²ͺ은 μ‹œν–‰μ°©μ˜€

  • X

λ¬΄λ‚œν•œ BFS λ¬Έμ œμ˜€λ‹€..!

μ½”λ“œ

import java.io.*;
import java.util.*;
import java.awt.*;

public class BOJ2178 {

    static int N;
    static int M;
    static int map[][];
    static boolean visited[][];
    static int road[][];
    static int dx[] = {-1, 1, 0, 0};
    static int dy[] = {0, 0, -1, 1};

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

        StringTokenizer st = new StringTokenizer(br.readLine());

        // ν–‰
        N = Integer.parseInt(st.nextToken());
        // μ—΄
        M = Integer.parseInt(st.nextToken());
        // 지도
        map = new int[N][M];
        // 방문 처리
        visited = new boolean[N][M];
        // μ΄λ™ν•œ 횟수λ₯Ό μ €μž₯ν•  λ°°μ—΄
        road = new int[N][M];

        // 지도에 λ²½κ³Ό 길을 μž…λ ₯
        for(int i = 0; i < N; i++){
            String s = br.readLine();

            for(int j = 0; j < M; j++){
                int n = Integer.parseInt(s.split("")[j]);
                map[i][j] = n;
            }
        }

        // μ‹œμž‘μ μΈ (0, 0)λΆ€ν„° BFSλ₯Ό 돌림
        BFS(0, 0);

        System.out.println(road[N - 1][M - 1]);
    }

    public static void BFS(int x, int y){
        Queue<Point> q = new LinkedList<>();

        // μ‹œμž‘μ§€μ  Queue에 λ„£μŒ
        q.offer(new Point(x, y));

        // ν•΄λ‹Ή μ’Œν‘œλ₯Ό λ°©λ¬Έμ²˜λ¦¬ν•˜κ³ 
        visited[x][y] = true;

        // ν•΄λ‹Ή μ’Œν‘œκ°’μ˜ 이동 횟수λ₯Ό 1둜 μ €μž₯
        road[x][y] = 1;

        // Queueκ°€ λΉŒλ•ŒκΉŒμ§€ 반볡
        while(!q.isEmpty()){
            int xx = q.peek().x;
            int yy = q.peek().y;

            q.poll();

            // Queueμ—μ„œ κΊΌλ‚Έ μ’Œν‘œκ°€ 도착지라면 μ’…λ£Œ
            if((xx == (N - 1)) && (yy == (M - 1))){
                break;
            }

            for(int i = 0; i < 4; i++){
                // μƒν•˜μ’Œμš°λ‘œ μ΄λ™ν•œ μ’Œν‘œκ°’
                int nx = xx + dx[i];
                int ny = yy + dy[i];

                // λ§Œμ•½ λ°°μ—΄ λ²”μœ„λ₯Ό λ„˜μ–΄κ°„λ‹€λ©΄ continue
                if(nx >= N || nx < 0 || ny >= M || ny < 0){
                    continue;
                }

                // λ§Œμ•½ ν•΄λ‹Ή μ’Œν‘œκ°’μ„ λ°©λ¬Έν•˜μ§€ μ•Šμ•˜κ³  값이 1이라면
                if(!visited[nx][ny] && (map[nx][ny] == 1)){
                    // μƒν•˜μ’Œμš°λ‘œ μ΄λ™ν•œ μ’Œν‘œκ°’μ˜ 이동 횟수λ₯Ό
                    // μ΄λ™ν•˜κΈ° μ „ μ’Œν‘œκ°’μ˜ 이동 νšŸμˆ˜μ—μ„œ 1 λ”ν•œ κ°’μœΌλ‘œ μ €μž₯
                    road[nx][ny] = road[xx][yy] + 1;
                    // μ΄λ™ν•œ μ’Œν‘œκ°’μ„ 방문처리 ν›„
                    visited[nx][ny] = true;
                    // Queue에 μ΄λ™ν•œ μ’Œν‘œκ°’μ„ λ„£μŒ
                    q.offer(new Point(nx, ny));
                }
            }
        }
    }
}