hermosa65   3년 전

어디가 틀린걸까요 정답코드랑 거의 유사한데 왜틀렸죠 >ㅜㅜ

[정답코드]

import java.awt.Point;
import java.util.*;

public class Main{
    public static int[] dx = {1,0,0,-1};
    public static int[] dy = {0,1,-1,0};
    public static int width,height, start_x,start_y;
    public static int count; 
    public static boolean visit[][];
    public static int arr[][];
    public static ArrayList list = new ArrayList<>();
    public static Queue queue = new LinkedList<>();
    public static void main(String[] args){
          Scanner scan = new Scanner(System.in); 
          height = scan.nextInt();
          width = scan.nextInt(); 
          arr = new int[width][height];
          count = 0;
          for(int i = 0; i < width; i++) {
                 for(int j = 0; j < height; j++) {
                      arr[i][j] = scan.nextInt();
                      if(arr[i][j] == 1) {
                              queue.offer(new Point(i,j));
                       }
                  }
           }
           System.out.println(bfs());
    }

     public static int bfs() {
           while(!queue.isEmpty()) {
                 Point now = queue.poll();
                 int x = now.x;
                 int y = now.y;
                 for(int i = 0; i < 4; i++) {
                       int nx = x + dx[i];
                       int ny = y + dy[i];
                       if(nx >= 0 && ny >= 0 && nx < width && ny < height) {
                                if(arr[nx][ny] == 0) {
                                          queue.add(new Point(nx,ny));
                                          arr[nx][ny] = arr[x][y] + 1;
                                 }
                        }
                   }
    }
     for(int i = 0; i < width; i++) {
          for(int j = 0; j < height; j++) {
                 if(arr[i][j] == 0) {
                      return -1;
                 }
                 count = Math.max(count, arr[i][j]);
           }
     }
     if(count == 1)
           return 0;
     else
          return count - 1;
     }

}

siyamaki   3년 전

질문할 코드를 올려주실 땐 틀렸던 코드나 정상적으로 실행된 코드를 올려주세요

또한 Point에 값을 넣을때 N, M쪽의 값을 넣고 있지만 bfs를 수행할 땐 ny nx로 돌고 있어 값이 반대가 되고 있습니다

댓글을 작성하려면 로그인해야 합니다.