java遍历二维字符数组,如何遍历虽然二维字符数组搜索在java中的话?

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[i] = in.readLine().toUpperCase().toCharArray();

}

for(int i = 0;i

display.writeLine(" ");

for(int j = 0;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

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

int p = 0;

for(int row = 0;row

for(int col = 0;col

while(p

if(grid[row][col] == wordlist[k][p]){

//do something

}

}

}

}

}

}

Any help or pointers would be greatly appreciated!

解决方案

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);

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)

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;

}

I bet you'll have all processing code (except input-reading) in 40 lines.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值