1. ๋ฌธ์ : https://www.acmicpc.net/problem/2178
2. ํ์ด
์ต์ ์นธ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ฏ๋ก bfs๋ก ํ์ดํ๋ค
3. ์ฝ๋
import java.io.IOException;
import java.util.*;
public class Main {
static int N,M;
static int[][] maps;
static int[][] value;
static boolean[][] visited;
static int[] dx = {0,-1,0,1};
static int[] dy = {1,0,-1,0};
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
maps = new int[N][M];
visited = new boolean[N][M];
value = new int[N][M];
String temp = "";
for(int i=0; i<N; i++){
temp = sc.next();
for(int j=0; j<M; j++){
maps[i][j] = temp.charAt(j)-'0';
}
}
bfs();
System.out.println(value[N-1][M-1]+1);
}
public static void bfs(){
int[] xy = new int[2];
xy[0] = 0;
xy[1] = 0;
visited[0][0] = true;
Queue<int[]> queue = new LinkedList<>();
queue.add(xy);
while(!queue.isEmpty()){
int cur[] = queue.poll();
int x = cur[0];
int y = cur[1];
int curVal = value[x][y];
for(int i=0; i<4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
if(!visited[nx][ny] && maps[nx][ny] != 0){
visited[nx][ny] = true;
value[nx][ny] = curVal + 1;
queue.add(new int[]{nx,ny});
}
}
}
}
}
'๐ป Coding Problems Solving > DFS | BFS | Backtracking' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ 12851] ์จ๋ฐ๊ผญ์ง2 (java) (0) | 2023.04.18 |
---|---|
[BOJ 1743] ์์๋ฌผ ํผํ๊ธฐ (java) (0) | 2023.04.11 |
[BOJ 1062] ๊ฐ๋ฅด์นจ (java) (0) | 2023.04.04 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๊ฒ์ ๋งต ์ต๋จ๊ฑฐ๋ฆฌ(java) (0) | 2023.03.31 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ํ๋ ์ฆ4๋ธ๋ก (0) | 2023.03.13 |
์ต๊ทผ๋๊ธ