0

我有一个程序应该在单词搜索谜题中搜索“ruby”、“python”和“java”。我的教授给了我从左到右搜索的代码,但我不确定如何从右到左和对角线搜索。我见过其他人编写同样的问题,但我认为我的教授希望我用她做过的类似方法来做。

我试图从右到左,但我要么得到一个超出范围的异常,要么搜索结果为负。

public static void main (String[] argv)
{
    char[][] puzzle = {
        {'n', 'o', 'h', 't', 'y', 'p', 's'},
        {'m', 'i', 'a', 'r', 'y', 'c', 'c'},
        {'l', 'l', 'e', 'k', 's', 'a', 'h'},
        {'r', 'u', 'b', 'y', 'v', 'm', 'e'},
        {'e', 'h', 'h', 'a', 'l', 'l', 'm'},
        {'p', 'c', 'j', 'n', 'i', 'c', 'e'},
        {'r', 'e', 'e', 'k', 'b', 'i', 'p'}
    };

    String result1 = findWordLefttoRight (puzzle, "ruby");
    String result2 = findWordRighttoLeft (puzzle, "python");
    //String result3 = findWordBottomLefttoTopRight (puzzle, "java");
    System.out.println (result1);
    System.out.println (result2);
    //System.out.println (result3);
}

/*Given by Professor*/

static String findWordLefttoRight (char[][] puzzle, String word)
{
// First convert the String into a char array.
char[] letters = word.toCharArray ();

// Now try every possible starting point in the puzzle array.
for (int i=0; i<puzzle.length; i++) {
    for (int j=0; j<puzzle[i].length; j++) {

    // Use (i,j) as the starting point.
    boolean found = true;

    // Try to find the given word's letters.
    for (int k=0; k<letters.length; k++) {
        if ( (j+k >= puzzle[i].length) || (letters[k] != puzzle[i][j+k]) ) {
        // Not a match.
        found = false;
        break;
        }
    }

    // If we went the whole length of the word, we found it.
    if (found) {
        return "String " + word + " found in row=" + i + " col=" +j;
    }

    }
}

return "String " + word + " not found";
}

/* My attempt at going from right to left */

static String findWordRighttoLeft (char[][] puzzle, String word)
{
// First convert the String into a char array.
char[] letters = word.toCharArray ();

// Now try every possible starting point in the puzzle array.
for (int i=puzzle.length; i>0; i--) {
    for (int j=puzzle.length; j>0; j--) {

    // Use (i,j) as the starting point.
    boolean found = true;

    // Try to find the given word's letters.
    for (int k=0; k<letters.length; k++) {          
        if ( (j+k <= puzzle.length) || (letters[k] == puzzle[i][j+k]) ) { 
        // Not a match.
        found = false;
        break;
        }
    }

    // If we went the whole length of the word, we found it.
    if (found) {
        return "String " + word + " found in row=" + i + " col=" +j;
    }

    }
}

return "String " + word + " not found";
}
4

2 回答 2

0

在一张纸上写出你的拼图矩阵(如果你有网格纸),并将每行和每列的索引放在矩阵的大小上。查看您用于从左到右搜索矩阵的嵌套 for 循环,它以 comment 开头//Now try every possible starting point in the puzzle array,并了解它是如何搜索您在纸上的矩阵的。然后看看如何从右到左搜索并决定如何更改代码以从右到左遍历矩阵。一旦你解决了这个问题,对对角线做同样的事情。请记住,对角线有四种情况:

  1. 左上到右下
  2. 左下到右上
  3. 右上到左下
  4. 右下到左上

对于英语读者来说,对角线可能更直观,如案例#1,所以首先从那个开始,这样你对问题的理解就会增加。

For OutOfBounds errors, add debug statements in the code (or even better, learn to use your IDE debugger) so you can understand how the for loop is working, and where it is going too far.

于 2013-09-14T16:30:24.063 回答
0

以下情况将导致越界异常

(letters[k] != puzzle[i][j+k])

在哪里

J max value can be puzzle[i].length  and
k max value can be letters.length

因为您同时添加了它们并检查了导致异常的索引。最大值只能是puzzle[i].length

注意: findWordLefttoRight 逻辑也不正确,存在同样的问题。它也会导致越界异常

于 2013-09-14T16:34:40.307 回答