fork download
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.*;
  4.  
  5. public class Main {
  6. static int[] dx = {-1,1,0,0};
  7. static int[] dy = {0,0,-1,1};
  8. static int n;
  9. static int[][] map;
  10. static boolean[][] visited;
  11. static int[][] tmp_map;
  12. static int count = 0;
  13. static int s_x, s_y;
  14. static int shark_size = 2;
  15. static int tmp_food = 0;
  16. static ArrayList<dot> arr = new ArrayList<dot>();
  17. static int time = 0;
  18.  
  19. public static void main(String[] args) throws Exception{
  20.  
  21. n = Integer.parseInt(br.readLine());
  22.  
  23. map = new int[n][n];
  24. tmp_map = new int[n][n];
  25. visited = new boolean[n][n];
  26.  
  27. for(int i=0; i<n; i++) {
  28. String[] str = br.readLine().split(" ");
  29. for(int j=0; j<n; j++) {
  30. map[i][j] = Integer.parseInt(str[j]);
  31.  
  32. if(map[i][j] == 9) {
  33. s_x = i;
  34. s_y = j;
  35. }
  36. }
  37. }
  38.  
  39. solve(new dot(s_x, s_y));
  40. System.out.println(time);
  41.  
  42. }
  43.  
  44. static void find(dot d) {
  45. arr = new ArrayList<dot>();
  46. Queue<dot> q = new LinkedList<dot>();
  47. visited[d.x][d.y] = true;
  48. q.add(d);
  49.  
  50. while(!q.isEmpty()) {
  51. dot t = q.remove();
  52. int x = t.x;
  53. int y = t.y;
  54.  
  55. if(map[x][y] < shark_size && map[x][y] >= 1 && map[x][y] <=6) {
  56. arr.add(new dot(x,y));
  57. }
  58.  
  59. for(int i=0; i<4; i++) {
  60. int x1 = x + dx[i];
  61. int y1 = y + dy[i];
  62.  
  63. if(isRange(x1,y1) && !visited[x1][y1] && tmp_map[x1][y1] == 0 && map[x1][y1] <= shark_size) {
  64. q.add(new dot(x1,y1));
  65. tmp_map[x1][y1] = tmp_map[x][y] + 1;
  66. visited[x1][y1] = true;
  67.  
  68. }
  69. }
  70. }
  71.  
  72. }
  73.  
  74. static boolean isRange(int x,int y) {
  75. if(x>=0 && x<n && y>=0 && y<n) {
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. }
  81.  
  82.  
  83. static dot whoEat() {
  84. ArrayList<Integer> distance = new ArrayList<Integer>();
  85. ArrayList<Integer> minDistanceDot_x = new ArrayList<Integer>();
  86. ArrayList<Integer> minDistanceDot_y = new ArrayList<Integer>();
  87.  
  88. //최소 거리 구하기
  89. for(int i=0; i<arr.size(); i++) {
  90. distance.add(tmp_map[arr.get(i).x][arr.get(i).y]);
  91. }
  92.  
  93. int min_distance = Collections.min(distance);
  94.  
  95. //최소 거리의 x,y 좌표 구하기
  96. for(int i=0; i<arr.size(); i++) {
  97. if(tmp_map[arr.get(i).x][arr.get(i).y] == min_distance) {
  98. minDistanceDot_x.add(arr.get(i).x);
  99. minDistanceDot_y.add(arr.get(i).y);
  100. }
  101. }
  102.  
  103. //가장 위의 x좌표 구하기
  104. int min_x = Collections.min(minDistanceDot_x);
  105. int min_y = Integer.MAX_VALUE;
  106.  
  107. //가장 위의 x좌표중 가장 왼쪽의 x좌표 구하기
  108. for(int i=0; i<minDistanceDot_x.size(); i++) {
  109. if(minDistanceDot_x.get(i) == min_x) {
  110. if(min_y > minDistanceDot_y.get(i)) {
  111. min_y = minDistanceDot_y.get(i);
  112. }
  113. }
  114. }
  115.  
  116. //min_y index에 있는 minDot구하기.
  117. dot minDot = arr.get(minDistanceDot_y.indexOf(min_y));
  118.  
  119. //아기상어가 먹이를 먹고 이동
  120. map[s_x][s_y] = 0;
  121. s_x = minDot.x;
  122. s_y = minDot.y;
  123. map[s_x][s_y] = 9;
  124. //먹이를 먹은 수 체크해서 shark_size와 같으면 shark_size 증가하고 먹은 count를 다시 0으로
  125. count++;
  126. if(count == shark_size) {
  127. shark_size++;
  128. count = 0;
  129. }
  130. //최소 거리 시간만큼 더해주기
  131. time += min_distance;
  132.  
  133. return minDot;
  134.  
  135. }
  136.  
  137. static void solve(dot d) {
  138. //맨 처음 d를 넣어주고 arr 사이즈 체크 (0이라면 종료)
  139. //아니라면 whoEat() 실행
  140. //whoEat()까지 실행하면 visited와 거리를 측정하는 tmp_map 초기화
  141. dot tmp = d;
  142. while(true) {
  143. find(tmp);
  144. if(arr.size()==0) {
  145. break;
  146. }
  147. tmp = whoEat();
  148.  
  149. visited = new boolean[n][n];
  150. tmp_map = new int[n][n];
  151.  
  152. }
  153. }
  154.  
  155. }
  156.  
  157. class dot {
  158. int x;
  159. int y;
  160.  
  161. public dot(int x,int y) {
  162. this.x = x;
  163. this.y = y;
  164. }
  165. }
Success #stdin #stdout 0.06s 32828KB
stdin
6
1 0 0 0 0 0
6 6 6 6 6 0
1 0 6 0 9 0
2 0 6 0 6 6
6 0 0 0 6 6
6 6 6 6 6 6
stdout
39