fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.LinkedList;
  7. import java.util.StringTokenizer;
  8.  
  9.  
  10. public class Main {
  11.  
  12. public static class Pair {
  13. int i , j;
  14. char direction;
  15. public Pair(int i , int j) {
  16. this.i = i;
  17. this.j = j;
  18. }
  19. public Pair(int i , int j , char c) {
  20. this.i = i;
  21. this.j = j;
  22. this.direction = c;
  23. }
  24. }
  25. static char[][] grid = new char[110][110];
  26. static boolean[][] visited = new boolean[110][110];
  27. static ArrayList<Pair> toflood = new ArrayList<Pair>(150);
  28. static ArrayList<Pair> Is = new ArrayList<Pair>(150);
  29. static LinkedList<Pair> q = new LinkedList<Pair>();
  30. static int[] dx = {-1, -1, 0, 0, 1, 1};
  31. static int[] dy = {0, 1, -1, 1, -1, 0};
  32. static StringBuffer sb = new StringBuffer();
  33. static boolean l = false;
  34. static int n , m;
  35.  
  36. public static void main(String[] args) throws IOException {
  37. // StringTokenizer st = new StringTokenizer(br.readLine());
  38. while(true) {
  39. StringTokenizer st = new StringTokenizer(br.readLine());
  40. n = Integer.parseInt(st.nextToken());
  41. m = Integer.parseInt(st.nextToken());
  42. if(n == 0 && m == 0)
  43. break;
  44. for(int i = 0 ; i < n ; i++) {
  45. String temp = br.readLine();
  46. for(int j = 0 ; j < m ; j++) {
  47. grid[i][j] = temp.charAt(j);
  48. if(grid[i][j] == 'I') {
  49. Is.add(new Pair(i , j , '('));
  50. // grid[i][j] = '(';
  51. }
  52. }
  53. }
  54. // for(int i = 0 ; i < n ; i++) {
  55. // for(int j = 0 ; j < m ; j++) {
  56. // System.out.print(grid[i][j] + " ");
  57. // }
  58. // System.out.println();
  59. // }
  60. for(int i = 0 ; i < Is.size() ; i++) {
  61. q.add(Is.get(i));
  62. bfs();
  63. }
  64. if (l)
  65. sb.append('\n');
  66. sb.append('\n');
  67. for(int i = 0 ; i < toflood.size() ; i++) {
  68. Pair curr = toflood.get(i);
  69. floodfill(curr.i , curr.j);
  70. }
  71. for(int i = 0 ; i < n ; i++) {
  72. for(int j = 0 ; j < m ; j++) {
  73. if(grid[i][j] == '*')
  74. sb.append("F");
  75. else
  76. sb.append(grid[i][j]);
  77. }
  78. if (i != (n - 1))
  79. sb.append('\n');
  80. }
  81. // System.out.println(toflood.isEmpty());
  82. q.clear();
  83. toflood.clear();
  84. Is.clear();
  85. l = true;
  86. for(int i = 0 ; i < n ; i++)
  87. Arrays.fill(visited[i], false);
  88. }
  89. System.out.print(sb);
  90. }
  91.  
  92. public static void floodfill(int i, int j) {
  93. if(i < 0 || i >= n || j < 0 || j >= m)
  94. return;
  95. if(grid[i][j] != '(' && grid[i][j] != ')' && grid[i][j] != '*')
  96. return;
  97. grid[i][j] = 'B';
  98. for(int k = 0 ; k < dx.length ; k++) {
  99. floodfill(i + dx[k], j + dy[k]);
  100. }
  101. }
  102.  
  103. public static void bfs() {
  104. while(!q.isEmpty()) {
  105. Pair curr = q.poll();
  106. if((grid[curr.i][curr.j] == '(' && curr.direction == ')') || (grid[curr.i][curr.j] == ')' && curr.direction == '(')) {
  107. toflood.add(new Pair(curr.i, curr.j));
  108. q.clear();
  109. continue;
  110. }
  111. else {
  112. grid[curr.i][curr.j] = curr.direction;
  113. }
  114. if(visited[curr.i][curr.j])
  115. continue;
  116. visited[curr.i][curr.j] = true;
  117. char newdirection;
  118. if(curr.direction == ')')
  119. newdirection = '(';
  120. else
  121. newdirection = ')';
  122. for(int j = 0 ; j < dx.length ; j++) {
  123. int newI = curr.i + dx[j];
  124. int newJ = curr.j + dy[j];
  125. if(newI >= 0 && newI < n && newJ >= 0 && newJ < m && grid[newI][newJ] != '.') {
  126. q.add(new Pair(newI , newJ , newdirection));
  127. }
  128. }
  129. }
  130. }
  131. }
Success #stdin #stdout 0.07s 380160KB
stdin
4 3
...
.*.
.I.
...
4 4
....
.**.
.I..
..*.
0 0
stdout
...
.).
.(.
...

....
.BB.
.B..
..F.