fork(1) download
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.io.StringReader;
  5. import java.util.ArrayList;
  6.  
  7. class NewMain {
  8. // Make global variables (grid&blobSize) which are accessible
  9. // from anywhere inside the class FloodIntro
  10. public static Character newGrid[][];
  11. public static int blobSize;
  12.  
  13. public static void main(String[] args) throws IOException{
  14.  
  15. String[] grid = new String[15];
  16. newGrid = new Character[15][15];
  17.  
  18. BufferedReader in = new BufferedReader(new StringReader("+++++++++++++++\n" +
  19. "++--+++++++++++\n" +
  20. "++---++++++++++\n" +
  21. "+----++++++++++\n" +
  22. "++++-++++++++++\n" +
  23. "+++++++++++++++\n" +
  24. "+++++++++++++++\n" +
  25. "+++++++++-----+\n" +
  26. "+++++++-----+++\n" +
  27. "+++++-----+++++\n" +
  28. "++++++------+++\n" +
  29. "+++++++-----+++\n" +
  30. "+++++++++--++++\n" +
  31. "++++++++++-++++\n" +
  32. "+++++++++++++++"));
  33. String str = null;
  34. ArrayList<String> lines = new ArrayList<String>();
  35. int i = 0;
  36. while ((str = in.readLine()) != null) {
  37. grid[i] = str;
  38. // System.out.println(str);
  39. i++;
  40. }
  41.  
  42. // so far can print out every line listed above
  43. for (int x = 0; x < grid.length; x++) {
  44. // for every line in the grid
  45. for (int y = 0; y < grid[x].length(); y++) {
  46. newGrid[x][y] = grid[x].charAt(y);
  47. }
  48. }
  49.  
  50.  
  51. // Print out the current grid
  52. displayGrid();
  53.  
  54. // variable to determine the size of the blob
  55. blobSize = 0;
  56.  
  57. // Pick one random element in the array that is not along the
  58. // border and remove the blob at that location
  59. // NOTE: if a blank is chosen, the blob size is 0
  60. // and nothing is removed
  61. int blobRow = 3;
  62. int blobCol = 3;
  63.  
  64. System.out.println("The blob at " + blobRow + "," + blobCol + " will be removed.");
  65. floodFill(blobRow, blobCol);
  66. System.out.println("The blob had " + blobSize + " items in it");
  67. System.out.println("The new grid is:");
  68. // Print out the new grid
  69. displayGrid();
  70. }
  71.  
  72. public static void floodFill(int row, int col) {
  73. if (newGrid[row][col].equals('-')) {
  74. newGrid[row][col] = ' ';
  75. blobSize++;
  76. floodFill(row - 1, col - 1);
  77. floodFill(row - 1, col);
  78. floodFill(row - 1, col + 1);
  79. floodFill(row, col - 1);
  80. floodFill(row, col + 1);
  81. floodFill(row + 1, col - 1);
  82. floodFill(row + 1, col);
  83. floodFill(row + 1, col + 1);
  84. }
  85. }
  86.  
  87. public static void displayGrid() {
  88. String output="";
  89. for (int row = 0; row <= 14; row++) {
  90. for (int col = 0; col <= 14; col++) {
  91. output += newGrid[row][col];
  92. }
  93. output += "\n";
  94. }
  95. System.out.println(output);
  96. }
  97. }
  98.  
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
+++++++++++++++
++--+++++++++++
++---++++++++++
+----++++++++++
++++-++++++++++
+++++++++++++++
+++++++++++++++
+++++++++-----+
+++++++-----+++
+++++-----+++++
++++++------+++
+++++++-----+++
+++++++++--++++
++++++++++-++++
+++++++++++++++

The blob at 3,3 will be removed.
The blob had 10 items in it
The new grid is:
+++++++++++++++
++  +++++++++++
++   ++++++++++
+    ++++++++++
++++ ++++++++++
+++++++++++++++
+++++++++++++++
+++++++++-----+
+++++++-----+++
+++++-----+++++
++++++------+++
+++++++-----+++
+++++++++--++++
++++++++++-++++
+++++++++++++++