使用Recusion解决Word迷宫(Java)

时间:2018-03-13 06:22:49

标签: java arrays recursion maze

所以,我对Java非常非常,并且我已经在这个问题上工作了一段时间。这是一个学校项目(已经过期约2周),我正在寻找提示或一些可能的解决方案,可以指导我从这一点。

该程序的目标是获取.txt文件(类似于:https://pastebin.com/hvg0P2Pe)并通过2D网格字母以查找提供的单词;这些单词可以水平,垂直和对角连接。这个程序的问题是它无法使用迭代完成,必须以递归方式完成。

以下是以下代码的输出:https://pastebin.com/uXiDHjvU

我不完全确定我的程序有什么问题;我好几天都在摸不着头脑。任何支持将不胜感激。

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

public class wordMaze1 {

    private static boolean wordCheck;

    public static boolean wordSearch(char[][] letter, String words, int r, int c) {

        if (letter[r][c] != words.charAt(0))
            return false;
        if (letter[r][c] == words.charAt(c))
            return true;
        if (letter[r][c] == words.charAt(0)) {
            letter.toString().toLowerCase();
            return wordSearch(letter, words.substring(1), r, c - 1);
        } else if (letter[r][c] == words.charAt(0)) {
            letter.toString().toLowerCase();
            return wordSearch(letter, words.substring(1), r, c + 1);
        } else if (letter[r][c] == words.charAt(0)) {
            letter.toString().toLowerCase();
            return wordSearch(letter, words.substring(1), r - 1, c);
        } else if (letter[r][c] == words.charAt(0)) {
            letter.toString().toLowerCase();
            return wordSearch(letter, words.substring(1), r + 1, c);
        } else if (letter[r][c] == words.charAt(0)) {
            letter.toString().toLowerCase();
            return wordSearch(letter, words.substring(1), r - 1, c - 1);
        } else if (letter[r][c] == words.charAt(0)) {
            letter.toString().toLowerCase();
            return wordSearch(letter, words.substring(1), r + 1, c - 1);
        } else if (letter[r][c] == words.charAt(0)) {
            letter.toString().toLowerCase();
            return wordSearch(letter, words.substring(1), r - 1, c + 1);
        } else if (letter[r][c] == words.charAt(0)) {
            letter.toString().toLowerCase();
            return wordSearch(letter, words.substring(1), r + 1, c + 1);
        }
        return false;
    }

    public static void main(String[] args) {
        try {
            Scanner scan = new Scanner(new File("maze.txt"));
            int numGrids = Integer.parseInt(scan.nextLine());
            for (int i = 1; i < numGrids + 1; i++) {
                System.out.println("Grid #" + i + ":");
                int size = Integer.parseInt(scan.nextLine());
                char[][] grid = new char[size][size];
                String[] tempGrid = new String[size];
                for (int t = 0; t < size; t++) {
                    tempGrid[t] = scan.nextLine();
                    System.out.println(tempGrid[t]);
                }
                int wordNum = Integer.parseInt(scan.nextLine());
                String[] words = new String[wordNum];
                for (int w = 0; w < wordNum; w++) {
                    words[w] = scan.nextLine();

                    for (int r = 0; r < size; r++) {
                        System.out.println();
                        for (int c = 0; c < size; c++) {
                            grid[r][c] = tempGrid[r].charAt(c);
                            System.out.print(grid[r][c]);
                            if (grid[r][c] == words[w].charAt(0))
                                if (wordSearch(grid, words[w], r, c)) {
                                    wordCheck = true;
                                    break;
                                }
                        }
                        //System.out.print(grid);
                    }
                    System.out.println();
                    if (wordCheck == true)
                        System.out.println(words[w] + " is found.");
                    else
                        System.out.println(words[w] + " is NOT found.");
                }
                System.out.println();

            }
        } catch (FileNotFoundException e) {

        }

    }

}

1 个答案:

答案 0 :(得分:0)

此行导致异常:

if (letter[r][c] == words.charAt(c))

如果用charAt(0)替换charAt(c),它应该更好用