利用洪水填充算法删除一个文本文件的具体内容洪水、具体内容、文本文件、算法

2023-09-11 06:58:41 作者:炎阳灼心i

有关我的课我有一个问题,我必须采取所有文本文件的癌细胞,并用颜色填充算法摆脱他们。

For one of my classes I have a problem where I have to take all of the "cancer" cells of a text file and get rid of them using the flood fill algorithm.

这是确切的问题:

创建一个程序,读取包含一个15×15的网格,从而重新presents人体细胞的文本文件。这些细胞被描述为一个加号+如果他们是健康的细胞,减号 - 如果他们是恶性的。外侧行和网格的列将只包含加号+。注意:用颜色填充算法来确定此信息

Create a program that reads a text file containing a 15 x 15 grid, which represents human cells. The cells are depicted with a plus sign "+" if they are healthy cells, and a minus sign "-" if they are cancerous. The outside rows and columns of the grid will contain only plus signs "+". Note: Use the Flood Fill algorithm to determine this information.

到目前为止,我可以得到癌细胞地区的至少一个更改为,但我不能得到其他。这是我到目前为止所。这是它应该是什么样子的一个例子:

So far I can get at least one of the cancer cell areas to change to " ", but I can't get the other. This is what I have so far. This is an example of what it should look like:

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;

    // Pick one random element in the array that is not along the
    // border and remove the blob at that location
    // NOTE: if a blank is chosen, the blob size is 0
    // and nothing is removed
    int blobRow = (int) (Math.random() * 13 + 1);
    int blobCol = (int) (Math.random() * 13 + 1);

    System.out.println("The blob at " + blobRow + "," + blobCol + " will be removed.");
    floodFill(blobRow, blobCol);
    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);
    }
}

  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);
      }
}

下面是正在使用的TXT文件:

Here is the txt file being used:

+++++++++++++++
++--+++++++++++
++---++++++++++
+----++++++++++
++++-++++++++++
+++++++++++++++
+++++++++++++++
+++++++++-----+
+++++++-----+++
+++++-----+++++
++++++------+++
+++++++-----+++
+++++++++--++++
++++++++++-++++
+++++++++++++++

推荐答案

的一个问题是随机生成的位置:

One problem is that the random generation of locations:

int blobRow = (int) (Math.random() * 13 + 1);
int blobCol = (int) (Math.random() * 13 + 1);

可能最终不会在 - ,因此不会做任何事情(不进入if语句,如果你得到一个 + )。

Might not end up at a -, thus doesn't do anything (it doesn't go into the if-statement if you get a +).

您可以在这个演示它确实正确填写癌症现货,如果你通过它有效的坐标。

You can see in this demo that it does fill a cancer spot correctly if you pass it valid coordinates.

除此之外,你应该实际运行洪水填充几次(一次为每个癌症点)。是这样的:

Beyond this, you should actually run flood-fill a few times (once for each cancer spot). Something like:

寻找一个 - (通过整个网格循环) 如果发现: 递增计数器(因为你需要输出的癌症点的数量) 在继续执行从那里洪水填充 并重复#1 Look for a - (by looping through the entire grid) If found: Increment a counter (since you need to output the number of cancer spots) Proceed with flood-fill from there And repeat from #1