[JAVA]백준 2178 미로탐색
import java.util.*;
public class Main {
static int N;
static int M;
static int [][] maze = new int[100][100];
static boolean [][] visit = new boolean[100][100];
static int[] dx = {0,1,0,-1};
static int[] dy = {1,0,-1,0};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
for(int i=0;i<N;i++){
String str = sc.next();
for(int j=0;j<str.length();j++){
maze[i][j] = str.charAt(j)-'0';
}
}
maze[0][0]=2;
bfs(0,0);
System.out.print(maze[N-1][M-1]-1);
}
public static void bfs(int n, int m){
Queue<Integer> qx = new LinkedList<Integer>();
Queue<Integer> qy = new LinkedList<Integer>();
qx.add(n);
qy.add(m);
while(!qx.isEmpty() && !qy.isEmpty()){
n = qx.poll();
m = qy.poll();
visit[n][m]=true;
for(int i=0;i<4;i++){
int nx = n + dx[i];
int ny = m + dy[i];
if(nx>=0 && ny>=0 && nx<N && ny<M){
if(maze[nx][ny]==1 && visit[nx][ny]==false){
qx.add(nx);
qy.add(ny);
visit[nx][ny]=true;
maze[nx][ny]= maze[n][m]+1;
}
}
}
}
}
}