hwanil

Untitled

Jan 12th, 2018
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. import java.util.*;
  2. public class N2146 {
  3.  
  4.    static int input;
  5.    static int[] dx = {-1,1,0,0};
  6.    static int[] dy = {0,0,1,-1};
  7.    static int answer=0;
  8.    static int answer2=Integer.MAX_VALUE;
  9.    static int flag;
  10.    public static final ArrayList<Integer> list = new ArrayList<>();
  11.    
  12.    public static void main(String[] args) {
  13.       // TODO Auto-generated method stub
  14.       Scanner sc = new Scanner(System.in);
  15.      
  16.       input = sc.nextInt();
  17.       int[][] array = new int[input][input];
  18.       boolean[][] check = new boolean[input][input];
  19.       boolean[][] check2 = new boolean[input][input];
  20.       int[][] dist = new int[input][input];
  21.       int[][] dist2 = new int[input][input];
  22.      
  23.       Queue<int[]> q = new LinkedList<int[]>();
  24.      
  25.       int count=0;
  26.       //int answer =0;
  27.      
  28.       for(int i=0; i<input; i++) {
  29.          for(int t=0; t<input; t++) {
  30.             array[i][t] = sc.nextInt();
  31.          }
  32.       }
  33.      
  34.       //섬 영역 구하기
  35.       for(int i=0; i<input; i++) {
  36.          for(int k=0; k<input ; k++) {
  37.             if(array[i][k]==1 && check[i][k]==false ) {
  38.                bfs(array, count+=1, check, dist, i, k);      
  39.             }
  40.          }
  41.       }  
  42.      
  43.      
  44.       for(int i=0; i<input; i++) {
  45.          for(int k=0; k<input ; k++) {
  46.             if(dist[i][k]!=0) {
  47.                q.offer(new int[] {i,k});
  48.             }  
  49.          }
  50.       }
  51.       test( dist2, answer2, check2, dist,  q);
  52.    
  53.       for(int v : list) {
  54.          if (answer2 > v) {
  55.             answer2 = v;
  56.          }
  57.       }
  58. //      System.out.println(list);
  59. //      
  60. //      for(int i=0; i<input; i++) {
  61. //         for(int k=0; k<input ; k++) {
  62. //            System.out.print(dist2[i][k]);
  63. //            if(k==input-1) System.out.println("");
  64. //         }
  65. //      }
  66.      
  67.       System.out.println(answer2);
  68.      
  69.      
  70.    }
  71.    
  72.    static void bfs(int[][] array, int count, boolean[][] check, int[][] dist, int x, int y) {
  73.       Queue<int[]> q = new LinkedList<int[]>();
  74.       q.offer(new int[] {x,y});
  75.       check[x][y] = true;
  76.       dist[x][y] = count;
  77.      
  78.       while(!q.isEmpty()) {
  79.          int[] head = q.poll();
  80.          for(int i=0; i<4; i++) {
  81.             int nx = head[0] + dx[i];
  82.             int ny = head[1] + dy[i];
  83.          
  84.             if(nx>=0 && ny>=0 && nx<input && ny<input) {
  85.                if(array[nx][ny]==1 && check[nx][ny] == false) {
  86.                   bfs(array,count,check,dist,nx,ny);
  87.                }
  88.             }
  89.          }
  90.       }
  91.    }
  92.    
  93.    static void test(int[][] array, int answer, boolean[][] check2, int[][] dist, Queue<int[]> q) {
  94.      
  95.  
  96.       Loop : while(!q.isEmpty()) {
  97.          int[] head = q.poll();
  98.          for(int i=0; i<4; i++) {
  99.             int nx = head[0] + dx[i];
  100.             int ny = head[1] + dy[i];
  101.          
  102.             if(nx>=0 && ny>=0 && nx<input && ny<input) {
  103.                if(dist[nx][ny]!=0 && dist[nx][ny] != dist[head[0]][head[1]]) {
  104.                   list.add(array[nx][ny] + array[head[0]][head[1]]);
  105. //                  System.out.println("nx : "+nx + "  ny" + ny + "  head  " + head[0] + "  hhh" + head[1]);
  106. //                  System.out.println(array[nx][ny] +"    " + array[head[0]][head[1]]);
  107.                }
  108.                if(dist[nx][ny]==0 ) {            
  109.                   q.offer(new int[] {nx,ny});
  110.                   dist[nx][ny]  = dist[head[0]][head[1]];
  111.                   array[nx][ny] = array[head[0]][head[1]]+1;
  112.                   //test(array,answer++,check2,dist,nx,ny);
  113.                }
  114.                
  115.  
  116.             }
  117.          }
  118.       }
  119.    }  
  120.  
  121. }
Add Comment
Please, Sign In to add comment