Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.Queue;
  3. import java.util.Scanner;
  4.  
  5. public class ccc03s3_2 {
  6.  
  7. public static void main(String[] args) {
  8. Scanner s = new Scanner(System.in);
  9. int flooring = s.nextInt();
  10. int rows = s.nextInt();
  11. int columns = s.nextInt();
  12. char[][] floorPlan = new char[rows][columns];
  13.  
  14. // read each line in the floor plan
  15. for (int row = 0; row < rows; row++) {
  16. String line = s.next();
  17. // read each letter in the line
  18. for (int col = 0; col < columns; col++) {
  19. floorPlan[row][col] = line.charAt(col);
  20. }
  21. }
  22.  
  23. for (int startRow = 0; startRow < rows; startRow++) {
  24. for (int startCol = 0; startCol < columns; startCol++) {
  25. // create the queue
  26. Queue<Integer> rowQueue = new LinkedList<Integer>();
  27. Queue<Integer> colQueue = new LinkedList<Integer>();
  28. rowQueue.add(startRow);
  29. colQueue.add(startCol);
  30. int cnt = 0;
  31. while (!rowQueue.isEmpty()) {
  32. int currentRow = rowQueue.remove();
  33. int currentCol = colQueue.remove();
  34.  
  35. // check if out of bounds
  36. if (currentRow < 0 || currentCol < 0) {
  37. continue;
  38. }
  39. if (currentCol >= columns || currentRow >= rows) {
  40. continue;
  41. }
  42.  
  43. // check if wall
  44. if (floorPlan[currentRow][currentCol] == 'I') {
  45. continue;
  46. }
  47.  
  48. // check if already marked
  49. if (floorPlan[currentRow][currentCol] == 'X') {
  50. continue;
  51. }
  52.  
  53. // mark it
  54. floorPlan[currentRow][currentCol] = 'X';
  55.  
  56. cnt++;
  57. // spread to cells beside
  58. rowQueue.add(currentRow - 1);
  59. colQueue.add(currentCol);
  60.  
  61. rowQueue.add(currentRow + 1);
  62. colQueue.add(currentCol);
  63.  
  64. rowQueue.add(currentRow);
  65. colQueue.add(currentCol - 1);
  66.  
  67. rowQueue.add(currentRow);
  68. colQueue.add(currentCol + 1);
  69. }
  70. if (cnt > 0) {
  71. for (int row = 0; row < rows; row++) {
  72. for (int col = 0; col < columns; col++) {
  73. System.out.print(floorPlan[row][col]);
  74. }
  75. System.out.println();
  76. }
  77. System.out.println();
  78. }
  79.  
  80. }
  81. }
  82. }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement