在单词搜索中从右到左和对角搜索 - Java

时间:2013-09-14 16:10:29

标签: java arrays

我有一个程序应该在单词搜索拼图中搜索'ruby','python'和'java'。我的教授给了我从左到右搜索的代码,但我不确定如何从右到左和对角线。我见过其他人编码同样的问题,但我想我的教授希望我用类似的方法来完成它。

我试图从右向左走,但我要么得到Out of Bounds异常,要么搜索结果为负。

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

2 个答案:

答案 0 :(得分:0)

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

  1. 左上角到右下角
  2. 左下角到右上角
  3. 右上角到左下角
  4. 从右下角到左上角
  5. 对于英语读者来说,与对象#1相比,它可能更直观,所以首先从这开始,这样你对问题的理解就会增长。

    对于OutOfBounds错误,在代码中添加调试语句(甚至更好,学习使用IDE调试器),这样你就可以了解for循环是如何工作的,以及它在哪里太过分了。

答案 1 :(得分: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逻辑也不正确,同样的问题是exsist。它也会导致超出范围的异常