如何遍历虽然二维字符数组搜索在java中的话? [英] how to traverse though a 2d char array searching for words in java?
问题描述
我有一个作业麻烦,真的很AP preciate一些见解。有人问我使用的是25×25二维字符数组来创建一个单词搜索,并以某种方式通过该阵列通过开发一个算法,将通过其搜索找到21 pre定义的话。
I am having trouble with a school assignment and would really appreciate some insight. I am asked to create a wordsearch using a 25x25 2d char array and somehow go through that array by developing an algorithm that will search through it to find 21 pre-defined words.
到目前为止,我已经能够创造的,我需要找到和二维阵列放置在每个位置上的字符的话一个衣衫褴褛的数组。
So far I have been able to create a ragged array of the words that I need to find and the 2d array with the chars placed in each position.
in = new ASCIIDataFile("wordsearch.txt");
display = new ASCIIDisplayer();
int numberWords = in.readInt();
wordlist = new char[numberWords][];
for (int i =0; i<wordlist.length; i++){
wordlist[i] = in.readLine().toUpperCase().toCharArray();
}
for(int i = 0;i<wordlist.length; i++){
display.writeLine(" ");
for(int j = 0;j<wordlist[i].length; j++){
display.writeChar(wordlist[i][j]);
}
}
//done wordlists
int gridLength = in.readInt();
int gridHeight = in.readInt();
grid = new char[gridHeight][gridLength];
for(int i = 0;i<gridLength; i++){
grid[i] = in.readLine().toCharArray();
}
我在创建的算法问题,搜索虽然二维数组并在单词表一个字符相匹配。 我应该做不同的方法,用于搜索向前,向后和对角线。 我一直在挣扎了好几天刚做了向前搜索。
My problem in creating the algorithm to search though the 2d array and match it with a character in the wordlist. I am supposed to make different methods, for searching forwards, backwards and diagonal. I have been struggling for days just to do the forward search.
我真的怎么不知道如何去了解这个问题,到目前为止,我所拥有的是
I really how no idea about how to go about this problem, so far all I have is
for(int k = 0; k<wordlist.length; k++){
int p = 0;
for(int row = 0;row<gridLength; row++){
for(int col = 0;col<gridHeight; col++){
while(p<wordlist[k].length){
if(grid[row][col] == wordlist[k][p]){
//do something
}
}
}
}
}
}
任何帮助或指针将不胜AP preciated!
Any help or pointers would be greatly appreciated!
推荐答案
诀窍是,你并不需要单独考虑所有8可能的方向。您可以重新每一个向量present。例如,前进的方向将是(0,1)
(第一个行号,然后列) - 一个向量指向右侧。对角线左上方的方向将是( - 1,-1)
。好了,你的想法。
The trick is, you don't need to consider all 8 possible directions separately. You can represent each with a vector. E.g., 'forward' direction would be (0, 1)
(first row number, then column) - a vector pointing to the right. Diagonal top-left direction would be (-1, -1)
. Well, you get the idea.
然后,只需创建一个函数
Then, just create a function
boolean findWord(int row, int col, int d_row, int d_col, char[] word);
它可以采取目前的矩阵位置((行,列)
,其中单词应该开始),搜索方向((d_row,d_col )
)和一个词来寻找。它返回给定的字是否是在这里。
然后,你可以调用它不同的方向,如:
It can take current matrix position ((row, col)
, where word is supposed to start), search direction ((d_row, d_col)
) and a word to look for. It returns whether the given word is here.
Then you can invoke it for different directions, e.g.
findWord(row, col, -1, 0, word);
findWord(row, col, -1, 1, word);
findWord(row, col, 0, 1, word);
...
(我列出他们按顺时针顺序)
(I'm listing them in clock-wise order)
实施 findWord
只是递增当前位置由 d_row
和 d_col
,直到我们找到匹配的字符或单词的目的。其基本程序是这样的
Implementing findWord
is just incrementing current position by d_row
and d_col
, until we find mismatch in characters or word ends. The basic routine is like this
while (row < total_rows && row >= 0 && col < total_columns && col >= 0) {
// check character here
...
row += d_row;
col += d_col;
}
我敢打赌,你都处理code(除输入读取)在40行。
I bet you'll have all processing code (except input-reading) in 40 lines.
这篇关于如何遍历虽然二维字符数组搜索在java中的话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!