0

私は必死で、あなたの助けが必要です! パズルと単語が与えられるクロスワードパズルソルバーに取り組んでおり、配列内の各インデックスに独自の文字が含まれるようにパズルをフォーマットする必要があります。現在、パズルのインデックスと単語を分割しているので、文字ごとに比較できますが、もっと良い方法はありますか? たとえば、探している単語を含む文字列配列を持ち、文字の配列でその単語を見つけることは可能ですか?

    My array looks like this: 
    [W, V, E, R, T, I, C, A, L, L]
    [R, O, O, A, F, F, L, S, A, B]
    [A, C, R, I, L, I, A, T, O, A]
    [N, D, O, D, K, O, N, W, D, C]
    [D, R, K, E, S, O, O, D, D, K]
    [O, E, E, P, Z, E, G, L, I, W]
    [M, S, I, I, H, O, A, E, R, A]
    [A, L, R, K, R, R, I, R, E, R]
    [K, O, D, I, D, E, D, R, C, D]
    [H, E, L, W, S, L, E, U, T, H]

そして、「垂直」を探しているとしましょう。文字列「垂直」を構成する文字を探すループを設定するにはどうすればよいですか。それとも、文字ごとに比較する必要がありますか?

これは私のコードです:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;


public class WordSearch {

    public static void main(String[] args) throws Exception{
            File file = new File("puzzle.txt"); 
            Scanner sc = new Scanner(file);
        int row = sc.nextInt();
        int col = sc.nextInt();
        sc.nextLine();

        //Make an array to hold the puzzle
        char[][] puzzle = new char[row][col];   


         //Read in the strings from the file into the array.
        for (int i=0; i<row; i++){
            String getChar = (new String(sc.next()));
            for(int j = 0;j<col; j++){
                puzzle[i][j] = getChar.charAt(j);
                }
            }

        //Test Print
         for (int i=0; i<puzzle.length; i++){

               System.out.print(Arrays.toString(puzzle[i]));
               System.out.println("");
         }



        //Read the number of words and move to the next line    
        int numwords = sc.nextInt();
        sc.nextLine();

        //Make an array to hold the words and read in the words from a file
        String[] words = new String[numwords];
        for(int i=0; i<numwords; i++){
            words[i] = sc.nextLine();
        }

        //look for each word
        for(int i=0; i<numwords; i++){
            String currentword = words[i];      

            String[] strings1 = currentword.split("");

            int range = currentword.length()+1;

            String[] strings2 = Arrays.copyOfRange(strings1, 1, range);

            int range2 = strings2.length;
            char[] charwords = new char[range2];    





            }



    //Close the scanner     
    sc.close();
    }

    }

これは対角線をテストするために機能しますか?

private boolean checkDiagonals(int row, int col, String word, char[][] puzzle) {
        //Checking diagonals direction
        for(int letter = 1; letter < word.length(); letter++) {
            if(puzzle[row + letter][col + letter] != word.charAt(letter)) {
                return false;
            }
            else if(puzzle[row + letter][col - letter] != word.charAt(letter)) {
                return false;
            }
            else if(puzzle[row - letter][col - letter] != word.charAt(letter)) {
                return false;
            }
            else if(puzzle[row - letter][col + letter] != word.charAt(letter)) {
                return false;
            }
        }
        return true;
    }
4

3 に答える 3

1

単語はどの方向にも向いている可能性があるため、単純に文字ごとに進む方が理にかなっています。文字の配列全体から、探している単語の最初の文字を検索します。たとえば、「VERTICAL」を探している場合は、配列全体を調べて文字「V」を探します。

次に、すべての方向 (上、下、左、右、斜め) の単語を検証する関数を用意します。と呼びましょうcheck(int row, int col)

for(int row = 0; row < puzzle.length; row++) {
    for(int col = 0; col < puzzle[0].length; col++) {
        if(puzzle[row][col] == word.charAt(0))
            if(check(row, col) == true)
                    //We found it.
    }
}

次に、 を書くcheck(int row, int col, String word, char[][] puzzle)には、各基本方向をチェックします。たとえば、正しいチェックをしていた場合は、単語が一致するか、矛盾が見つかるまで、右に進み続けて単語と文字を比較します。方向が機能しない場合は、false を返します。

例として、これは右側のチェックです (配列外の場合はエラー チェックはありません。それを実装する必要があります)。

private boolean checkRight(int row, int col, String word, char[][] puzzle) {
    //Checking right direction
    for(int letter = 1; letter < word.length(); letter++) {
        if(puzzle[row][col + letter] != word.charAt(letter)) {
            return false;
        }
    }
    return true;
}

次に、各方向にこれらのいずれかが必要であり、チェック機能では次のようになります

public boolean check(int row, int col, String word, char[][] puzzle) {
    if(checkRight(row, col, word, puzzle)) return true;
    if(checkLeft(row, col, word, puzzle)) return true;
    if(checkUp(row, col, word, puzzle)) return true;
    if(checkDown(row, col, word, puzzle)) return true;
    if(checkDiagonals(row, col, word, puzzle)) return true;
    return false;
}
于 2013-10-01T14:59:50.330 に答える
-1

配列の各行をStringthen useに変換することをお勧めしますString.indexOf()。これにより、探している文字列が行に存在するかどうか、およびその開始位置が決定されます。戻る-1と、文字列が行に存在しないことがわかります。

文字列.indexOf()

于 2013-10-01T14:44:29.450 に答える