문제

I have a problem that I can't seem to fix. I have to get the number of "holes" in a textfile. As you can see with this example it outputs 2 holes. My program however outputs every negative that has been switched to a blank item. I'm unsure how to convert my program from count the number of negatives to just counting the number of "holes".

enter image description here

Here is the code I have worked on so far:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class floodIntro {
// Make global variables (grid&blobSize) which are accessible
// from anywhere inside the class FloodIntro
public static Character newGrid[][];
public static int blobSize;

public static void main(String[] args) throws IOException{

    String[] grid = new String[15];
    newGrid = new Character[15][15];

    BufferedReader in = new BufferedReader(new FileReader("location.txt"));
    String str = null;
    ArrayList<String> lines = new ArrayList<String>();
    int i = 0;
    while ((str = in.readLine()) != null) {
        grid[i] = str;
    //  System.out.println(str);
        i++;
    }

    // so far can print out every line listed above
    for (int x = 0; x < grid.length; x++) {
        // for every line in the grid
        for (int y = 0; y < grid[x].length(); y++) {
            newGrid[x][y] = grid[x].charAt(y);
            }       
    }


    // Print out the current grid
    displayGrid();

    // variable to determine the size of the blob
    blobSize = 0;

    //System.out.println("The blob at " + blobRow + "," + blobCol + " will be removed.");
    for(int x=1;x<15;x++){
        for(int y=1;y<15;y++){
            floodFill(x,y);
        }
    }
    System.out.println("The blob had " + blobSize + " items in it");
    System.out.println("The new grid is:");
    // Print out the new grid
    displayGrid();
}

public static void floodFill(int row, int col) {
    if (newGrid[row][col].equals('-')) {
        newGrid[row][col] = ' ';
        blobSize++;
        floodFill(row - 1, col - 1);
        floodFill(row - 1, col);
        floodFill(row - 1, col + 1);
        floodFill(row, col - 1);
        floodFill(row, col + 1);
        floodFill(row + 1, col - 1);
        floodFill(row + 1, col);
        floodFill(row + 1, col + 1);
    }else if(newGrid[row][col].equals('+')){
        newGrid[row][col] = '+';
    }

}

  public static void displayGrid() {
      String output="";
        for (int row = 0; row <= 14; row++) {
          for (int col = 0; col <= 14; col++) {
            output += newGrid[row][col];
          }
          output += "\n";
        }
        System.out.println(output);
      }
}
도움이 되었습니까?

해결책

In floodFill you seem to be counting the size of the spots. If you want only the number of holes, then you don't need that. In that case you can change the loop in the main method like this:

int holes = 0;
for (int x = 1; x < newGrid.length; x++) {
    for (int y = 1; y < newGrid[x].length; y++) {
        if (newGrid[x][y] == '-') {
            ++holes;
            floodFill(x, y);
        }
    }
}
System.out.println("The file had " + holes + " cancer spots in it");

Basically, when you find a -, increment the count, and use the floodFill method to fill the hole so it's not counted again. (I took the liberty to change the loop end conditions to use newGrid.length and newGrid[x].length to make your code more flexible).

Also, in the floodFill method it's pointless to set values with + in them to + again. You could simplify the method like this:

public static void floodFill(int row, int col) {
    if (newGrid[row][col].equals('-')) {
        newGrid[row][col] = ' ';
        floodFill(row - 1, col - 1);
        floodFill(row - 1, col);
        floodFill(row - 1, col + 1);
        floodFill(row, col - 1);
        floodFill(row, col + 1);
        floodFill(row + 1, col - 1);
        floodFill(row + 1, col);
        floodFill(row + 1, col + 1);
    }
}

(Unless I missed something and you're looking for something more than just the number of holes.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top