fork(1) download
  1. import java.util.*;
  2. import java.io.*;
  3. /*
  4. 원숭이 무빙으로 가던 와중 앞에 장애물이 나타났을 때,
  5. 말 무빙을 치면 최소거리지 않을까
  6. */
  7.  
  8. public class Main
  9. {
  10. static class node implements Comparable<node>
  11. {
  12. int r;
  13. int c;
  14. int k;
  15. int time;
  16.  
  17. public node(int r , int c , int k , int time)
  18. {
  19. this.r = r;
  20. this.c = c;
  21. this.k = k;
  22. this.time = time;
  23. }
  24.  
  25. public int compareTo(node n)
  26. {
  27. return time - n.time;
  28. }
  29. }
  30. static int k , r , c , ans;
  31. static int[][] map;
  32. static boolean[][] visit;
  33. static int[] mr = {-1,0,1,0};
  34. static int[] mc = {0,1,0,-1};
  35. static int[] hr = {-2,-2,-1,1,2,2,1,-1};
  36. static int[] hc = {-1,1,2,2,1,-1,-2,-2};
  37. static PriorityQueue<node> pq;
  38.  
  39. public static void main (String[] args) throws IOException
  40. {
  41. k = Integer.parseInt(br.readLine());
  42.  
  43. StringTokenizer st = new StringTokenizer(br.readLine());
  44. c = Integer.parseInt(st.nextToken());
  45. r = Integer.parseInt(st.nextToken());
  46. map = new int[r][c];
  47. visit = new boolean[r][c];
  48. pq = new PriorityQueue<>();
  49.  
  50. for(int i = 0 ; i < r ; i++)
  51. {
  52. st = new StringTokenizer(br.readLine());
  53. for(int j = 0 ; j < c ; j++)
  54. {
  55. map[i][j] = Integer.parseInt(st.nextToken());
  56. }
  57. }
  58.  
  59. visit[0][0] = true;
  60. pq.add(new node(0,0,k,0));
  61.  
  62. bfs();
  63. System.out.println(ans);
  64. }
  65. public static void bfs()
  66. {
  67. while(!pq.isEmpty())
  68. {
  69. node nod = pq.poll();
  70. System.out.println(nod.r+","+nod.c+" // "+nod.time);
  71.  
  72.  
  73. if(nod.r == r-1 && nod.c == c-1)
  74. {
  75. ans = nod.time;
  76. return;
  77. }
  78.  
  79. for(int i = 0 ; i < 4 ; i++)
  80. {
  81. int nr = nod.r + mr[i];
  82. int nc = nod.c + mc[i];
  83.  
  84. if(nr >= 0 && nc >= 0 && nr < r && nc < c)
  85. {
  86. if(nod.k > 0 /*&& map[nr][nc] == 1*/)
  87. {
  88. horse_moving(nod.r,nod.c,nod.k,nod.time);
  89. }
  90. if(map[nr][nc] == 0 && !visit[nr][nc])
  91. {
  92. visit[nr][nc] = true;
  93. pq.add(new node(nr,nc,nod.k,nod.time+1));
  94. }
  95. }
  96. }
  97. }
  98.  
  99. ans = -1;
  100. }
  101. public static void horse_moving(int r , int c , int k , int time)
  102. {
  103. System.out.println("뛰어넘으려는 시도: "+r+","+c);
  104. for(int i = 0 ; i < 8 ; i++)
  105. {
  106. int nr = r + hr[i];
  107. int nc = c + hc[i];
  108.  
  109. if(nr >= 0 && nc >= 0 && nr < r && nc < c)
  110. {
  111. System.out.println("뛰어넘기 검사중...: "+nr+","+nc+" // "+map[nr][nc]);
  112. if(!visit[nr][nc] && map[nr][nc] == 0)
  113. {
  114. System.out.println("뛰어넘은 결과: "+nr+","+nc);
  115. visit[nr][nc] = true;
  116. pq.add(new node(nr,nc,k-1,time+1));
  117. }
  118.  
  119. }
  120. }
  121. }
  122. }
Success #stdin #stdout 0.22s 53436KB
stdin
1
4 4
0 0 0 0
1 0 0 0
0 0 1 0
0 1 0 0
stdout
0,0 // 0
뛰어넘으려는 시도: 0,0
뛰어넘으려는 시도: 0,0
0,1 // 1
뛰어넘으려는 시도: 0,1
뛰어넘으려는 시도: 0,1
뛰어넘으려는 시도: 0,1
0,2 // 2
뛰어넘으려는 시도: 0,2
뛰어넘으려는 시도: 0,2
뛰어넘으려는 시도: 0,2
1,1 // 2
뛰어넘으려는 시도: 1,1
뛰어넘으려는 시도: 1,1
뛰어넘으려는 시도: 1,1
뛰어넘으려는 시도: 1,1
1,2 // 3
뛰어넘으려는 시도: 1,2
뛰어넘기 검사중...: 0,0 // 0
뛰어넘으려는 시도: 1,2
뛰어넘기 검사중...: 0,0 // 0
뛰어넘으려는 시도: 1,2
뛰어넘기 검사중...: 0,0 // 0
뛰어넘으려는 시도: 1,2
뛰어넘기 검사중...: 0,0 // 0
2,1 // 3
뛰어넘으려는 시도: 2,1
뛰어넘기 검사중...: 0,0 // 0
뛰어넘으려는 시도: 2,1
뛰어넘기 검사중...: 0,0 // 0
뛰어넘으려는 시도: 2,1
뛰어넘기 검사중...: 0,0 // 0
뛰어넘으려는 시도: 2,1
뛰어넘기 검사중...: 0,0 // 0
0,3 // 3
뛰어넘으려는 시도: 0,3
뛰어넘으려는 시도: 0,3
2,0 // 4
뛰어넘으려는 시도: 2,0
뛰어넘으려는 시도: 2,0
뛰어넘으려는 시도: 2,0
1,3 // 4
뛰어넘으려는 시도: 1,3
뛰어넘기 검사중...: 0,1 // 0
뛰어넘으려는 시도: 1,3
뛰어넘기 검사중...: 0,1 // 0
뛰어넘으려는 시도: 1,3
뛰어넘기 검사중...: 0,1 // 0
3,0 // 5
뛰어넘으려는 시도: 3,0
뛰어넘으려는 시도: 3,0
2,3 // 5
뛰어넘으려는 시도: 2,3
뛰어넘기 검사중...: 0,2 // 0
뛰어넘기 검사중...: 1,1 // 0
뛰어넘으려는 시도: 2,3
뛰어넘기 검사중...: 0,2 // 0
뛰어넘기 검사중...: 1,1 // 0
뛰어넘으려는 시도: 2,3
뛰어넘기 검사중...: 0,2 // 0
뛰어넘기 검사중...: 1,1 // 0
3,3 // 6
6