兄弟们想不出来,求解答
这两道题写不出来,想了好久{:10_266:} {:5_96:} 本帖最后由 412046761 于 2021-12-26 15:56 编辑第一题
public class Test {
int a = 0,b = 0;
boolean includeWord(String[][]board, int m,int n,String word){
int wordIndex = 0;
// 遍历board 找word第一个字符
for(int x = 0; x < n; x++){
for (int y = 0; y < m; y++){
if(!board.equals(String.valueOf(word.charAt(0))))
continue;
a=x;b=y;
do{
wordIndex ++;
if(wordIndex == word.length())
return true;
// 周围没有下个字符跳出
if(!gorupHaveWord(board,a,b,word.charAt(wordIndex))){
wordIndex = 0;
break;
}
}while (wordIndex < word.length()) ;
}
}
return false;
}
public static void main(String[] args) {
Test a =new Test();
String[][]board = {{"A","B","C","D"},{"S","F","C","S"},{"A","D","E","E"}};
String word = "ABCCED";
boolean inclu = a.includeWord(board,4,3,word);
System.out.println(inclu);
}
/**
* 判断 二维数组board(x,y)坐标周围一圈是否等于字符word
* @param board 二维数组
* @param x 二维数组横坐标
* @param y 二维数组纵坐标
* @param word 字符
* @return
*/
booleangorupHaveWord(String[][]board ,int x,int y,char word){
return includeWord(board,x-1,y-1,word)
||includeWord(board,x-1,y,word)
||includeWord(board,x-1,y+1,word)
||includeWord(board,x,y-1,word)
||includeWord(board,x,y+1,word)
||includeWord(board,x+1,y-1,word)
||includeWord(board,x+1,y,word)
||includeWord(board,x+1,y+1,word);
}
/**
* 判断 二维数组board(x,y)坐标是否等于字符word
* @param board 二维数组
* @param x 二维数组横坐标
* @param y 二维数组纵坐标
* @param word 字符
* @return
*/
boolean includeWord(String[][]board ,int x,int y,char word){
try{
if(String.valueOf(word).equals(board)){
// 这里用a,b记录一下坐标
a = x;
b = y;
return true;
}
}catch (Exception e){}
return false;
}
}
页:
[1]