java - Java 中的 Wordhunt 程序

标签 java

这就是它的作用... 我有一个 .txt 文件,它存储在二维数组中,它可以工作...... 这就是问题,我应该在表中搜索一个单词,但我无法找到一种方法来比较数组中的字母并返回它的索引......(所以基本上我想在表中找到一个字母二维数组,我希望它返回它的索引 int c[][]) 真正的作业就像一个寻字谜题(它可以找到水平垂直对角线(左和右)以及向后(在所述方向上),但我很确定一旦我知道如何获得它,我就可以绕过它我搜索的单词的第二个字母的索引

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


public class Mp6 {


public static void main(String[] args) throws IOException {

    FileReader a = new FileReader("data/Data.txt");
    BufferedReader ss = new BufferedReader(a);
    char wla[][];
     int x = 80;
     int y = 80;
    wla = new char [x][y];
    String d;
    Scanner p = new Scanner(System.in);
    int c = 0;
    int n = 0 ; 


    try {
        for( c = 0; c < x; c++){ 
                d = ss.readLine(); 
        for ( n = 0; n<y;n++){
                wla[c][n]=d.charAt(n);
                System.out.print(wla[c][n]); 
                }
                 System.out.println("");
            }

        System.out.println("Search for:");
         String h = p.nextLine();
         h=h.toUpperCase();
         char [] ph = new char[h.length()];
         int counter = 0;
         for(counter = 0 ; counter < h.length();counter++){
             ph[counter]=h.charAt(counter);
             System.out.print(ph[counter]);
         }
         //horizontal


    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
}

}

http://www.mediafire.com/file/6ti20007cvyi4y7/DATA.txt

最佳答案

如果我理解正确的话,你想在二维数组中向各个方向搜索单词。在这种情况下,最好的算法应该是这样的:

  1. 循环遍历数组查找 这个词的第一个字母。
  2. 找到一个后 - 搜索所有 剩余字母的指示。

这是如何完成此操作的示例:

char[] word = getWordToFind();
char[][] array = getArrayWhereToSearch();

//Search for first letter of the word
for (int x = 0; x<array.length;x++) {
    for (int y=0; y<array[x].length;y++) {
        //If the first letter is found
        if (array[x][y] == word[0]) {
            //Search in all directions
            if (searchEast(array, word, x, y)) {
                println(String.format("Word match found! X: %d, Y: %d, direction: '%s'", x, y, "east"));
            }
            if (searchSouth(array, word, x, y)) {
                println(String.format("Word match found! X: %d, Y: %d, direction: '%s'", x, y, "south"));
            }
            //More directions if needed
            ...
        }
    }
}

public boolean searchEast(char[][] array, char[] word, int startX, int startY) {
    //If the word can't fit to the right
    if (x >= array.length-word.length) {
        return false;
    }

    //Search the remaining letters to the right starting from startX+1
    for (int dx = 1; dx<word.length;dx++) {
        if (array[startX+dx] != word[dx]) { //If the letter mismatches
            return false;
        }
    }

    //All letters matched
    return true;
}

public boolean searchSouth(char[][] array, char[] word, int startX, int startY) {
    //If the word can't fit to the bottom
    if (y >= array[startX].length-word.length) {
        return false;
    }

    //Search the remaining letters to the bottom starting from startY+1
    for (int dy = 1; dy<word.length;dy++) {
        if (array[startX][startY+dy] != word[dy]) { //If the letter mismatches
            return false;
        }
    }

    //All letters matched
    return true;
}

关于java - Java 中的 Wordhunt 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5296866/

相关文章:

java - 在 Android 中裁剪和调整图像大小

java - 使用 BaseActivity 进行 OnResume 操作栏样式设置

java - 如何检查包含字符串的数组是否包含另一个字符串数组中的某些单词

java - 在jtable中计算%,总计

java - fragment 中微调器的 setOnItemSelectedListener()

java - HibernatemappedBy 给出了调用方法时发生 com.sun.jdi.InitationException

java - 关于增加 JVM 的堆大小

java - org.hibernate.exception.GenericJDBCException : could not execute query

java - 应用程序读取错误短信

java - 将Gradle-Kotlin构建转换为Maven?