-1

我有这段代码,它将进行单词搜索并在其中找到单词。我让它在一些东西上工作,我厌倦了 BINARY,我尝试了 DRAC,它工作了,但是对于一些东西,尤其是对角线,不断给我出界错误。

import java.util.Scanner;

public class WordSearch { 

    public static void main(String[] args) {
        Scanner kboard = new Scanner(System.in);
        char[][] maze = {   {'A','P','B','I','N','A','R','Y','D','C','O','C','A','R','D'},
                            {'M','T','C','O','S','M','A','L','L','E','A','A','T','E','B'},
                            {'P','O','E','N','A','M','G','I','S','R','L','N','S','I','R'},
                            {'R','L','D','H','M','A','S','P','I','N','S','T','E','S','T'},
                            {'A','E','A','E','T','G','T','A','B','M','U','H','A','S','G'},
                            {'L','M','G','N','R','E','D','O','O','P','B','E','F','E','H'},
                            {'U','Y','L','O','E','N','O','K','A','L','L','R','A','M','T'},
                            {'C','I','E','V','E','T','E','C','U','N','A','C','D','Y','I'},
                            {'R','C','C','A','E','H','S','E','E','W','N','U','E','T','A'},
                            {'I','I','I','S','O','N','W','D','D','O','O','L','O','H','N'},
                            {'C','T','N','L','E','H','S','O','H','R','L','E','U','O','J'},
                            {'I','A','E','P','I','R','A','M','O','C','I','S','T','L','I'},
                            {'M','N','R','T','A','E','L','D','E','R','S','P','O','O','L'},
                            {'E','E','E','E','N','R','E','T','T','A','P','A','T','G','I'},
                            {'S','V','B','L','A','C','K','H','O','L','E','S','O','Y','N'},  };                      
        for(int i = 0; i <= 14; i++)
        {
             for(int j = 0; j <= 14; j++)
             {
                 System.out.print(maze[i][j] + " ");
             }
             System.out.println();
        }
        String input = kboard.nextLine();

        //checking the word search horizontally

        for(int r = 0; r <= maze.length - 1; r++)
        {
            for(int c = 0; c <= (maze.length - input.length()); c++)
            {
                boolean match = true;
                for(int i=0; i<input.length(); i++)
                {
                    if(maze[r][c + i] != input.charAt(i))
                    {
                        match = false;
                        break;
                    }
                }
                if(match)
                {
                    System.out.print("Found match (horizontal) for " + input + " starting at (" + r + ", " + c + ")");
                }
            }
        }

        //checking the word search backwards horizontally

        for(int r = 0; r < maze.length - 1; r++)
        {
            for(int c = 0; c <= (maze.length + 3 - input.length()); c++)
            {
                boolean match = true;
                for(int i = 0; i < input.length(); i++)
                {
                    if(maze[r][c - i] != input.charAt(i))
                    {
                        match = false;
                        break;
                    }
                }
                if(match)
                {
                    System.out.print("Found match (backwards horizontal) for " + input + " starting at (" + r + ", " + c + ")");
                }
            }
        }

        //checking the word search diagonally down

        for(int r = 0; r < maze.length - 1; r++)
        {
            for(int c = 0; c <= (maze.length - input.length()); c++)
            {
                boolean match = true;
                for(int i = 0; i < input.length(); i++)
                {
                    if(maze[r + i][c + i] != input.charAt(i))
                    {
                        match = false;
                        break;
                    }
                }
                if(match)
                {
                    System.out.print("Found match (diagonal down) for " + input + " starting at (" + r + ", " + c + ")");
                }
            }
        }

        //searching the word search diagonally up

        for(int r = 0; r < maze.length - 1; r++)
        {
            for(int c = 0; c <= (maze.length - input.length()); c++)
            {
                boolean match = true;
                for(int i = 0; i < input.length(); i++)
                {
                    if(maze[r - i][c - i] != input.charAt(i))
                    {
                        match = false;
                        break;
                    }
                }
                if(match)
                {
                    System.out.print("Found match (diagonal up) for " + input + " starting at (" + r + ", " + c + ")");
                }
            }
        }
    }
}

我试过数字并不断收到错误,例如

Found match (horizontal) for BLACKHOLE starting at (14, 2)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at WordSearch.main(WordSearch.java:112)`

另外,每次都使用相同的 for 循环是不是很糟糕?我只需要更改每个循环的第三个 for 中的一行吗?

4

1 回答 1

0

此行导致问题:

                if(maze[r - i][c - i] != input.charAt(i))

因为循环只增加 i 和 c,r 保持为 0,所以 0 - 1 = -1,因此超出范围。

调整为:

                if(maze[r][c - i] != input.charAt(i))

停止越界错误,并且在搜索“BLACK”时,循环似乎会遍历每行的所有列,但不会使对角搜索工作良好。

您不需要不同的方法,但是您可以尝试从网格上的每个点向各个方向移动,并捕获/忽略任何越界异常,因为您知道它们将是错误的结果,只是一个想法。

于 2018-02-27T23:46:55.773 回答