fork download
  1. import java. io.*;
  2. import java. util.*;
  3. public class Main {
  4. static int[] dx = {0,0,1,-1};
  5. static int[] dy = {1,-1,0,0};
  6. static int[][] time;
  7. static String[][] arr;
  8. static int h,w;
  9. static Queue<Node> q = new LinkedList<>();
  10. static boolean visited[][];
  11. static boolean fire_visited[][];
  12. static int fire_time[][];
  13. static Queue<Node>fire_q = new LinkedList<>();
  14. static boolean bl;
  15. static int fin_x, fin_y;
  16. static int ans;
  17. public static void main(String[] args) throws IOException {
  18. int test_case = Integer.parseInt(br.readLine());
  19.  
  20. for(int m=0; m<test_case; m++) {
  21. ans=Integer.MAX_VALUE;
  22. fin_x = Integer.MAX_VALUE;
  23. fin_y = Integer.MAX_VALUE;
  24. bl = false;
  25. q.clear();
  26. fire_q.clear();
  27. String[] t = br.readLine().split(" ");
  28. w = Integer.parseInt(t[0]); // 너비
  29. h = Integer.parseInt(t[1]); //높이
  30. arr = new String[h][w];
  31. time = new int[h][w];
  32. fire_time = new int[h][w];
  33. fire_visited = new boolean[h][w];
  34.  
  35.  
  36. visited = new boolean[h][w];
  37.  
  38. for(int i=0; i<h; i++) {
  39. String[] input = br.readLine().split("");
  40. for(int j=0; j<w; j++) {
  41. arr[i][j] = input[j];
  42. if(arr[i][j].equals("@")) {
  43. q.add(new Node(i,j)); // 시작위치를 넣어준다
  44. if(i==0 || j==0 || i==h-1 || j==w-1) {
  45. fin_x=0;
  46. fin_y=0;
  47. bl = true;
  48. }
  49. }
  50. if(arr[i][j].equals("*")) {
  51. fire_q.add(new Node(i,j));
  52. }
  53. }
  54. }
  55. fire_bfs();
  56. bfs();
  57.  
  58. if(bl) {
  59. System.out.println(ans+1);
  60. }
  61. else {
  62. System.out.println("IMPOSSIBLE");
  63. }
  64. }
  65.  
  66. }
  67. public static void fire_bfs() {
  68. while(!fire_q.isEmpty()) {
  69. Node a = fire_q.poll();
  70. fire_visited[a.x][a.y] = true;
  71. for(int i=0; i<4; i++) {
  72. int x_1 = a.x+dx[i];
  73. int y_1 = a.y +dy[i];
  74. if(x_1>=0 && y_1>=0 && x_1<h && y_1<w) {
  75. if(!fire_visited[x_1][y_1] && !arr[x_1][y_1].equals("#")) {
  76. fire_visited[x_1][y_1] = true;
  77. fire_time[x_1][y_1] = fire_time[a.x][a.y]+1;
  78. fire_q.add(new Node(x_1,y_1));
  79. }
  80. }
  81. }
  82. }
  83. }
  84. public static void bfs() {
  85. while(!q.isEmpty()) {
  86. Node a = q.poll();
  87. visited[a.x][a.y] = true;
  88. for(int i=0; i<4; i++) {
  89. int x_1 = a.x+dx[i];
  90. int y_1 = a.y+dy[i];
  91. if(x_1>=0 && y_1>=0 && x_1<h && y_1<w) {
  92.  
  93. if(!visited[x_1][y_1] && arr[x_1][y_1].equals(".")) {
  94. if(!visited[x_1][y_1] && fire_time[x_1][y_1]>time[a.x][a.y]+1 || fire_time[x_1][y_1]==0) {
  95. visited[x_1][y_1] = true;
  96. q.add(new Node(x_1,y_1));
  97. time[x_1][y_1] = time[a.x][a.y]+1;
  98. if(x_1==0 || y_1==0 || x_1==h-1 || y_1==w-1) {
  99. bl = true;
  100. ans=Math.min(ans, time[x_1][y_1]);
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. class Node{
  110. int x;
  111. int y;
  112.  
  113. Node(int x, int y){
  114. this.x= x;
  115. this. y= y;
  116. }
  117. }
Success #stdin #stdout 0.06s 33036KB
stdin
1
3 2
@.#
###
stdout
2