작성
·
170
0
아래와 같이 작성했는데, 답이 0이 나옵니다.
잘못된 부분이 있을까요??
import java.util.*;
import java.io.*;
class Main {
public static int n,m,L;
public static int[][] map ,dist;
public static boolean[][] visit;
public static int[] dx = {0,0,1,-1};
public static int[] dy = {1,-1,0,0};
public int solution(int[][] board){
int answer = 0;
n=board.length;
m=board[0].length;
map = new int[n][m];
dist = new int[n][m];
visit = new boolean[n][m];
L=0;
for(int i=0; i<n;i++) {
for(int j=0; j<m; j++) {
map[i][j] = board[i][j];
}
}
bfs(0,0);
return answer;
}
public static int bfs(int r,int c) {
Queue<int[]> q =new LinkedList<>();
q.offer(new int[] {r,c}); //0,0대입
visit[r][c] = true;
while(!q.isEmpty()) {
L++;
int len = q.size();
for(int i=0; i<len; i++) {
int[] tmp = q.poll();
int nowx = tmp[0];
int nowy = tmp[1];
for(int j=0;j<4;j++) { //상하좌우 탐색
int nx = nowx+dx[j];
int ny = nowy+dy[j];
if(nx>=0 && ny>=0 && nx<n && ny<m) { //맵 범위안이고
if(!visit[nx][ny] && map[nx][ny]==0) { //다음 노드가 방문안햇고, 방문할 수 있다
dist[nx][ny]=L;
q.offer(new int[] {nx,ny});
visit[nx][ny] = true;
}
}
}
}
}
return dist[n-1][m-1];
}
public static void main(String[] args){
Main T = new Main();
int[][] arr={{0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 1, 0, 0, 0},
{1, 1, 0, 1, 0, 1, 1},
{1, 1, 0, 1, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 0},
{1, 0, 1, 0, 0, 0, 0}};
System.out.println(T.solution(arr));
}
}