【问题标题】:Recursion using BufferedReader [closed]使用 BufferedReader 进行递归 [关闭]
【发布时间】:2014-04-18 20:52:40
【问题描述】:

在我的计算机科学课上,我们遇到了一个我似乎无法解决的问题。问题如下。我们还必须使用洪水填充算法

创建一个程序来读取包含 15 x 15 网格的文本文件,该网格代表人体细胞。如果细胞是健康细胞,则用加号“+”表示,如果是癌性细胞,则用减号“-”表示。网格的外部行和列将仅包含加号“+”。”基本上会发生什么是您需要使用递归来检查 txt 文件中的每个项目,如果字符是“-”,那么您更改它到“”,如果字符为“+”,则将其保留为“+”。

这是我目前拥有的代码。到目前为止,我已经将文本文件放入 arraylist,然后创建了一个字符串数组。我不确定如何检查每个值以查看它们是否为“-”,因为一旦我能够做到这一点,递归应该相当简单。任何帮助将不胜感激!

public class Cancer {

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

      String[][] grid = new String[14][14];

      BufferedReader in = new BufferedReader(new FileReader("location.txt"));
      String str=null;
        ArrayList<String> lines = new ArrayList<String>();
        while((str = in.readLine()) != null){
            lines.add(str);
        }
        String[] linesArray = lines.toArray(new String[lines.size()]);
  }
}

【问题讨论】:

  • 奇怪的问题。这种映射操作通常是迭代完成的。另外,文中的实际问题似乎与标题无关。
  • 这不是递归适用的问题。你确定你被要求使用递归吗?

标签: java arrays recursion arraylist bufferedreader


【解决方案1】:

首先,我认为您最好使用 char 网格,但让我们继续使用 String 方法:

当你声明你的数组时,你声明了它的大小,所以一个有 15 个元素的字符串数组将被声明为new String[15]

我真的不明白为什么需要一个二维字符串数组,因为一个字符串是一整行(行)。 这是您的程序的略微修改版本,它将文件的行读入一维字符串数组,然后遍历每行中的每个字符并找到“癌症”(迭代地):

import java.util.*;
import java.io.*;

public class Cancer {

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

      String[] grid = new String[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;
            i++;
        }

        for (int x = 0; x < grid.length; x++) {
            String row = grid[x];
            for (int y = 0; y < row.length(); y++) {
                if(row.charAt(y) == '-') {
                    System.out.println("Cancer found at " + x + "," + y);
                }
            }
        }
  }
}

希望这能让您了解如何检查您的输入。 String 和 charAt 方法没有任何问题(因为“网格”变成一维的),它只是使实际的“算法”变得不那么便携。正如其他人所提到的,使用递归解决这个问题有点奇怪。一种方法是利用网格坐标进行递归调用。另一种选择是“砍掉”数组/字符串。

【讨论】:

  • @KEYSER 不是不需要 grid 数组吗?使用iterator 我们可以获取字符串
  • @KNU 因为我们只处理一次,不,它不是必需的。但这是一种很好的做法:)
【解决方案2】:

这是我的尝试:

package com.college.assignment;

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

/**
* @author Kunal Krishna
* 
*/
public class Cancer {

public static final String cancerSamplePath = "C://Users//Kunal//workspace//Cancer///src//com//college//assignment//location.txt";
static List<String> lines = new ArrayList<>(0);

public static void main(String[] args) {
    readFile();

    displayFile();

    System.out.println("\n========================================");
    if (searchInfectedCells(lines) == 0) {
        System.out.println("None of the Cells are damaged");
    }
    System.out.println("========================================"); 
}

private static void displayFile() {
    for (String string : lines) {
        System.out.println(string);
    }
}

private static int searchInfectedCells(List<String> lines) {
    int infectedCellRowPosition = 0;
    int totalInfectedCells = 0;
    for (String row : lines) {
        infectedCellRowPosition++;
        for (int infectedCellColPosition = 0; infectedCellColPosition < row
                .length(); infectedCellColPosition++) {
            if (row.charAt(infectedCellColPosition) == '-') {
                                    totalInfectedCells++;
                System.out.println("Cancerous cell found at location ("
                        + infectedCellRowPosition + ","
                        + infectedCellColPosition + ")");
            }
        }
    }
    return totalInfectedCells;
}

public static void readFile() {

    BufferedReader in;
    String str;
    try {
        in = new BufferedReader(new FileReader(cancerSamplePath));
        while ((str = in.readLine()) != null) {
            lines.add(str);
        }
    } catch (FileNotFoundException fne) {
        System.out.println("Cancer sample data missing.");
        fne.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

}

输出(也显示输入):

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

========================================
Cancerous cell found at location (2,1)
Cancerous cell found at location (14,1)
Cancerous cell found at location (14,13)
========================================

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-04
    • 2011-06-04
    • 2018-11-02
    • 2015-02-02
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多