我有一个从10x10网格中查找word位置的代码,我希望它能够输出它,如下所示:
added - Pattern found vertically at 10, 8但是我现在得到了很多关于每个运动的打印声明,还有一些没有打印的单词的运动。我尝试将打印机方法移动到patternSearch中,但输出结果相同。但是,如果我移除打印机方法而不实现移动输出,它将运行良好,如下所示:
added - Pattern found at 10, 8但是我也想要移动,因为将整个单词放置在网格中会更容易。
以下是代码:
import java.io.*;
import java.util.*;
class Main {
static int R, C;
// For searching in all 8 direction
static int[] x = { -1, -1, -1, 0, 0, 1, 1, 1 };
static int[] y = { -1, 0, 1, -1, 1, -1, 0, 1 };
static boolean search2D(char[][] grid, int row,
int col, String word)
{
if (grid[row][col] != word.charAt(0))
return false;
int len = word.length();
// Search word in all 8 directions
// starting from (row, col)
for (int dir = 0; dir < 8; dir++) {
//Make current direction starting point
int k, rd = row + x[dir], cd = col + y[dir];
// First character is already checked, then check others
for (k = 1; k < len; k++) {
if (rd >= R || rd < 0 || cd >= C || cd < 0)
break;
if (grid[rd][cd] != word.charAt(k))
break;
// Moving in that direction
rd += x[dir];
cd += y[dir];
}
// If all character matched,value equal to length of word
if (k == len)
return true;
}
return false;
}
static String printer(char[][] grid, int row, int col, String word) {
String move="";
char found=word.charAt(1);
int g=row;
int h=col;
if(grid[g+1][h]==found){
move="vertically";
}
else if(grid[g][h+1]==found){
move="horizontally";
}
else if(grid[g-1][h]==found){
move="vertically";
}
else if(grid[g][h-1]==found){
move="horizontally";
}
else if(grid[g-1][h-1]==found){
move="diag";
}
else if(grid[g+1][h+1]==found){
move="diag";
}
else if(grid[g-1][h+1]==found){
move="diag";
}
else if(grid[g+1][h-1]==found){
move="diag";
}
return move;
}
static void patternSearch( char[][] grid, String word) {
String mov="";
for (int row = 0; row < R; row++) {
for (int col = 0; col < C; col++) {
if (search2D(grid, row, col, word))
mov=printer(grid, row,col,word);
System.out.println(word +" - Pattern found " + mov +" at " + (row+1) + ", " + (col+1));
}
}
}
public static void main(String args[])
{
R = 10;
C = 10;
char[][] grid = {
{'t', 'a', 'b', 'o', 'v', 'e', 'o', 'y', 'm', 'z'},
{'s', 'u', 'q', 'o', 'e', 'a', 'v', 'i', 'u', 's'},
{'j', 'f', 'o', 'e', 'o', 'a', 'd', 'l', 'f', 'w'},
{'t', 'h', 'r', 'b', 'q', 'f', 'b', 'u', 'x', 'z'},
{'d', 'g', 'r', 't', 'a', 't', 'y', 'u', 'l', 'p'},
{'a', 'w', 'c', 's', 'n', 'e', 't', 'd', 's', 't'},
{'z', 's', 'w', 'e', 'i', 'r', 'u', 'e', 'q', 'e'},
{'q', 'v', 'g', 'g', 'a', 'x', 'l', 'd', 'z', 'z'},
{'v', 'a', 'n', 'i', 'g', 'r', 'p', 'd', 'y', 't'},
{'c', 'u', 'd', 'j', 'a', 'w', 'd', 'a', 'n', 'k'}
};
for (int row = 0; row < R; row++) {
for (int col = 0; col < C; col++) {
System.out.print(grid[row][col]+"\t");
}
System.out.println();
}
System.out.println();
patternSearch(grid, "about");
patternSearch(grid, "above");
patternSearch(grid, "abuse");
patternSearch(grid, "added");
patternSearch(grid, "adult");
patternSearch(grid, "after");
patternSearch(grid, "again");
patternSearch(grid, "agent");
patternSearch(grid, "agree");
}
} 我目前的产出:
t a b o v e o y m z
s u q o e a v i u s
j f o e o a d l f w
t h r b q f b u x z
d g r t a t y u l p
a w c s n e t d s t
z s w e i r u e q e
q v g g a x l d z z
v a n i g r p d y t
c u d j a w d a n k
about - Pattern found at 1, 1
about - Pattern found at 1, 2
about - Pattern found at 1, 3
about - Pattern found at 1, 4
about - Pattern found at 1, 5
about - Pattern found at 1, 6
about - Pattern found at 1, 7
about - Pattern found at 1, 8
about - Pattern found at 1, 9
about - Pattern found at 1, 10
about - Pattern found at 2, 1
about - Pattern found at 2, 2
about - Pattern found at 2, 3
about - Pattern found at 2, 4
about - Pattern found at 2, 5
about - Pattern found at 2, 6
about - Pattern found at 2, 7
about - Pattern found at 2, 8
about - Pattern found at 2, 9
about - Pattern found at 2, 10
about - Pattern found at 3, 1
about - Pattern found at 3, 2
about - Pattern found at 3, 3
about - Pattern found at 3, 4
about - Pattern found at 3, 5
about - Pattern found at 3, 6
about - Pattern found at 3, 7
about - Pattern found at 3, 8
about - Pattern found at 3, 9
about - Pattern found at 3, 10
about - Pattern found at 4, 1
about - Pattern found at 4, 2
about - Pattern found at 4, 3
about - Pattern found at 4, 4
about - Pattern found at 4, 5
about - Pattern found at 4, 6
about - Pattern found at 4, 7
about - Pattern found at 4, 8
about - Pattern found at 4, 9
about - Pattern found at 4, 10
about - Pattern found at 5, 1
about - Pattern found at 5, 2
about - Pattern found at 5, 3
about - Pattern found at 5, 4
about - Pattern found diag at 5, 5
about - Pattern found diag at 5, 6
about - Pattern found diag at 5, 7
about - Pattern found diag at 5, 8
about - Pattern found diag at 5, 9
about - Pattern found diag at 5, 10
about - Pattern found diag at 6, 1
about - Pattern found diag at 6, 2
about - Pattern found diag at 6, 3
about - Pattern found diag at 6, 4
about - Pattern found diag at 6, 5
about - Pattern found diag at 6, 6
about - Pattern found diag at 6, 7
about - Pattern found diag at 6, 8
about - Pattern found diag at 6, 9
about - Pattern found diag at 6, 10
about - Pattern found diag at 7, 1
about - Pattern found diag at 7, 2
about - Pattern found diag at 7, 3
about - Pattern found diag at 7, 4
about - Pattern found diag at 7, 5
about - Pattern found diag at 7, 6
about - Pattern found diag at 7, 7
about - Pattern found diag at 7, 8
about - Pattern found diag at 7, 9
about - Pattern found diag at 7, 10
about - Pattern found diag at 8, 1
about - Pattern found diag at 8, 2
about - Pattern found diag at 8, 3
about - Pattern found diag at 8, 4
about - Pattern found diag at 8, 5
about - Pattern found diag at 8, 6
about - Pattern found diag at 8, 7
about - Pattern found diag at 8, 8
about - Pattern found diag at 8, 9
about - Pattern found diag at 8, 10
about - Pattern found diag at 9, 1
about - Pattern found diag at 9, 2
about - Pattern found diag at 9, 3
about - Pattern found diag at 9, 4
about - Pattern found diag at 9, 5
about - Pattern found diag at 9, 6
about - Pattern found diag at 9, 7
about - Pattern found diag at 9, 8
about - Pattern found diag at 9, 9
about - Pattern found diag at 9, 10
about - Pattern found diag at 10, 1
about - Pattern found diag at 10, 2
about - Pattern found diag at 10, 3
about - Pattern found diag at 10, 4
about - Pattern found diag at 10, 5
about - Pattern found diag at 10, 6
about - Pattern found diag at 10, 7
about - Pattern found diag at 10, 8
about - Pattern found diag at 10, 9
about - Pattern found diag at 10, 10
above - Pattern found at 1, 1
above - Pattern found horizontally at 1, 2
above - Pattern found horizontally at 1, 3
above - Pattern found horizontally at 1, 4
above - Pattern found horizontally at 1, 5
above - Pattern found horizontally at 1, 6
above - Pattern found horizontally at 1, 7
above - Pattern found horizontally at 1, 8
above - Pattern found horizontally at 1, 9
above - Pattern found horizontally at 1, 10
above - Pattern found horizontally at 2, 1
above - Pattern found horizontally at 2, 2
above - Pattern found horizontally at 2, 3
above - Pattern found horizontally at 2, 4
above - Pattern found horizontally at 2, 5
above - Pattern found horizontally at 2, 6
above - Pattern found horizontally at 2, 7
above - Pattern found horizontally at 2, 8
above - Pattern found horizontally at 2, 9
above - Pattern found horizontally at 2, 10
above - Pattern found horizontally at 3, 1
above - Pattern found horizontally at 3, 2
above - Pattern found horizontally at 3, 3
above - Pattern found horizontally at 3, 4
above - Pattern found horizontally at 3, 5
above - Pattern found horizontally at 3, 6
above - Pattern found horizontally at 3, 7
above - Pattern found horizontally at 3, 8
above - Pattern found horizontally at 3, 9
above - Pattern found horizontally at 3, 10
above - Pattern found horizontally at 4, 1
above - Pattern found horizontally at 4, 2
above - Pattern found horizontally at 4, 3
above - Pattern found horizontally at 4, 4
above - Pattern found horizontally at 4, 5
above - Pattern found horizontally at 4, 6
above - Pattern found horizontally at 4, 7
above - Pattern found horizontally at 4, 8
above - Pattern found horizontally at 4, 9
above - Pattern found horizontally at 4, 10
above - Pattern found horizontally at 5, 1
above - Pattern found horizontally at 5, 2
above - Pattern found horizontally at 5, 3
above - Pattern found horizontally at 5, 4
above - Pattern found horizontally at 5, 5
above - Pattern found horizontally at 5, 6
above - Pattern found horizontally at 5, 7
above - Pattern found horizontally at 5, 8
above - Pattern found horizontally at 5, 9
above - Pattern found horizontally at 5, 10
above - Pattern found horizontally at 6, 1
above - Pattern found horizontally at 6, 2
above - Pattern found horizontally at 6, 3
above - Pattern found horizontally at 6, 4
above - Pattern found horizontally at 6, 5
above - Pattern found horizontally at 6, 6
above - Pattern found horizontally at 6, 7
above - Pattern found horizontally at 6, 8
above - Pattern found horizontally at 6, 9
above - Pattern found horizontally at 6, 10
above - Pattern found horizontally at 7, 1
above - Pattern found horizontally at 7, 2
above - Pattern found horizontally at 7, 3
above - Pattern found horizontally at 7, 4
above - Pattern found horizontally at 7, 5
above - Pattern found horizontally at 7, 6
above - Pattern found horizontally at 7, 7
above - Pattern found horizontally at 7, 8
above - Pattern found horizontally at 7, 9
above - Pattern found horizontally at 7, 10
above - Pattern found horizontally at 8, 1
above - Pattern found horizontally at 8, 2
above - Pattern found horizontally at 8, 3
above - Pattern found horizontally at 8, 4
above - Pattern found horizontally at 8, 5
above - Pattern found horizontally at 8, 6
above - Pattern found horizontally at 8, 7
above - Pattern found horizontally at 8, 8
above - Pattern found horizontally at 8, 9
above - Pattern found horizontally at 8, 10
above - Pattern found horizontally at 9, 1
above - Pattern found horizontally at 9, 2
above - Pattern found horizontally at 9, 3
above - Pattern found horizontally at 9, 4
above - Pattern found horizontally at 9, 5
above - Pattern found horizontally at 9, 6
above - Pattern found horizontally at 9, 7
above - Pattern found horizontally at 9, 8
above - Pattern found horizontally at 9, 9
above - Pattern found horizontally at 9, 10
above - Pattern found horizontally at 10, 1
above - Pattern found horizontally at 10, 2
above - Pattern found horizontally at 10, 3
above - Pattern found horizontally at 10, 4
above - Pattern found horizontally at 10, 5
above - Pattern found horizontally at 10, 6
above - Pattern found horizontally at 10, 7
above - Pattern found horizontally at 10, 8
above - Pattern found horizontally at 10, 9
above - Pattern found horizontally at 10, 10
abuse - Pattern found at 1, 1
abuse - Pattern found at 1, 2
abuse - Pattern found at 1, 3
abuse - Pattern found at 1, 4
abuse - Pattern found at 1, 5
abuse - Pattern found at 1, 6
abuse - Pattern found at 1, 7
abuse - Pattern found at 1, 8
abuse - Pattern found at 1, 9
abuse - Pattern found at 1, 10
abuse - Pattern found at 2, 1
abuse - Pattern found at 2, 2
abuse - Pattern found at 2, 3
abuse - Pattern found at 2, 4
abuse - Pattern found at 2, 5
abuse - Pattern found at 2, 6
abuse - Pattern found at 2, 7
abuse - Pattern found at 2, 8
abuse - Pattern found at 2, 9
abuse - Pattern found at 2, 10
abuse - Pattern found at 3, 1
abuse - Pattern found at 3, 2
abuse - Pattern found at 3, 3
abuse - Pattern found at 3, 4
abuse - Pattern found at 3, 5
abuse - Pattern found diag at 3, 6
abuse - Pattern found diag at 3, 7
abuse - Pattern found diag at 3, 8
abuse - Pattern found diag at 3, 9
abuse - Pattern found diag at 3, 10
abuse - Pattern found diag at 4, 1
abuse - Pattern found diag at 4, 2
abuse - Pattern found diag at 4, 3
abuse - Pattern found diag at 4, 4
abuse - Pattern found diag at 4, 5
abuse - Pattern found diag at 4, 6
abuse - Pattern found diag at 4, 7
abuse - Pattern found diag at 4, 8
abuse - Pattern found diag at 4, 9
abuse - Pattern found diag at 4, 10
abuse - Pattern found diag at 5, 1
abuse - Pattern found diag at 5, 2
abuse - Pattern found diag at 5, 3
abuse - Pattern found diag at 5, 4
abuse - Pattern found diag at 5, 5
abuse - Pattern found diag at 5, 6
abuse - Pattern found diag at 5, 7
abuse - Pattern found diag at 5, 8
abuse - Pattern found diag at 5, 9
abuse - Pattern found diag at 5, 10
abuse - Pattern found diag at 6, 1
abuse - Pattern found diag at 6, 2
abuse - Pattern found diag at 6, 3
abuse - Pattern found diag at 6, 4
abuse - Pattern found diag at 6, 5
abuse - Pattern found diag at 6, 6
abuse - Pattern found diag at 6, 7
abuse - Pattern found diag at 6, 8
abuse - Pattern found diag at 6, 9
abuse - Pattern found diag at 6, 10
abuse - Pattern found diag at 7, 1
abuse - Pattern found diag at 7, 2
abuse - Pattern found diag at 7, 3
abuse - Pattern found diag at 7, 4
abuse - Pattern found diag at 7, 5
abuse - Pattern found diag at 7, 6
abuse - Pattern found diag at 7, 7
abuse - Pattern found diag at 7, 8
abuse - Pattern found diag at 7, 9
abuse - Pattern found diag at 7, 10
abuse - Pattern found diag at 8, 1
abuse - Pattern found diag at 8, 2
abuse - Pattern found diag at 8, 3
abuse - Pattern found diag at 8, 4
abuse - Pattern found diag at 8, 5
abuse - Pattern found diag at 8, 6
abuse - Pattern found diag at 8, 7
abuse - Pattern found diag at 8, 8
abuse - Pattern found diag at 8, 9
abuse - Pattern found diag at 8, 10
abuse - Pattern found diag at 9, 1
abuse - Pattern found diag at 9, 2
abuse - Pattern found diag at 9, 3
abuse - Pattern found diag at 9, 4
abuse - Pattern found diag at 9, 5
abuse - Pattern found diag at 9, 6
abuse - Pattern found diag at 9, 7
abuse - Pattern found diag at 9, 8
abuse - Pattern found diag at 9, 9
abuse - Pattern found diag at 9, 10
abuse - Pattern found diag at 10, 1
abuse - Pattern found diag at 10, 2
abuse - Pattern found diag at 10, 3
abuse - Pattern found diag at 10, 4
abuse - Pattern found diag at 10, 5
abuse - Pattern found diag at 10, 6
abuse - Pattern found diag at 10, 7
abuse - Pattern found diag at 10, 8
abuse - Pattern found diag at 10, 9
abuse - Pattern found diag at 10, 10
added - Pattern found at 1, 1
added - Pattern found at 1, 2
added - Pattern found at 1, 3
added - Pattern found at 1, 4
added - Pattern found at 1, 5
added - Pattern found at 1, 6
added - Pattern found at 1, 7
added - Pattern found at 1, 8
added - Pattern found at 1, 9
added - Pattern found at 1, 10
added - Pattern found at 2, 1
added - Pattern found at 2, 2
added - Pattern found at 2, 3
added - Pattern found at 2, 4
added - Pattern found at 2, 5
added - Pattern found at 2, 6
added - Pattern found at 2, 7
added - Pattern found at 2, 8
added - Pattern found at 2, 9
added - Pattern found at 2, 10
added - Pattern found at 3, 1
added - Pattern found at 3, 2
added - Pattern found at 3, 3
added - Pattern found at 3, 4
added - Pattern found at 3, 5
added - Pattern found at 3, 6
added - Pattern found at 3, 7
added - Pattern found at 3, 8
added - Pattern found at 3, 9
added - Pattern found at 3, 10
added - Pattern found at 4, 1
added - Pattern found at 4, 2
added - Pattern found at 4, 3
added - Pattern found at 4, 4
added - Pattern found at 4, 5
added - Pattern found at 4, 6
added - Pattern found at 4, 7
added - Pattern found at 4, 8
added - Pattern found at 4, 9
added - Pattern found at 4, 10
added - Pattern found at 5, 1
added - Pattern found at 5, 2
added - Pattern found at 5, 3
added - Pattern found at 5, 4
added - Pattern found at 5, 5
added - Pattern found at 5, 6
added - Pattern found at 5, 7
added - Pattern found at 5, 8
added - Pattern found at 5, 9
added - Pattern found at 5, 10
added - Pattern found at 6, 1
added - Pattern found at 6, 2
added - Pattern found at 6, 3
added - Pattern found at 6, 4
added - Pattern found at 6, 5
added - Pattern found at 6, 6
added - Pattern found at 6, 7
added - Pattern found at 6, 8
added - Pattern found at 6, 9
added - Pattern found at 6, 10
added - Pattern found at 7, 1
added - Pattern found at 7, 2
added - Pattern found at 7, 3
added - Pattern found at 7, 4
added - Pattern found at 7, 5
added - Pattern found at 7, 6
added - Pattern found at 7, 7
added - Pattern found at 7, 8
added - Pattern found at 7, 9
added - Pattern found at 7, 10
added - Pattern found at 8, 1
added - Pattern found at 8, 2
added - Pattern found at 8, 3
added - Pattern found at 8, 4
added - Pattern found at 8, 5
added - Pattern found at 8, 6
added - Pattern found at 8, 7
added - Pattern found at 8, 8
added - Pattern found at 8, 9
added - Pattern found at 8, 10
added - Pattern found at 9, 1
added - Pattern found at 9, 2
added - Pattern found at 9, 3
added - Pattern found at 9, 4
added - Pattern found at 9, 5
added - Pattern found at 9, 6
added - Pattern found at 9, 7
added - Pattern found at 9, 8
added - Pattern found at 9, 9
added - Pattern found at 9, 10
added - Pattern found at 10, 1
added - Pattern found at 10, 2
added - Pattern found at 10, 3
added - Pattern found at 10, 4
added - Pattern found at 10, 5
added - Pattern found at 10, 6
added - Pattern found at 10, 7
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Main.printer(Main.java:67)
at Main.patternSearch(Main.java:116)
at Main.main(Main.java:160)发布于 2020-07-14 05:21:12
根据我的理解,您的代码有两个问题。
搜索“added”. times.
我会一个接一个地解释。
1.打印说明
要只在找到整个单词时才打印语句,而不是每次都打印语句,请将print语句放入if子句如下:
if (search2D(grid, row, col, word)){
mov=printer(grid, row,col,word);
System.out.println(word +" - Pattern found " + mov +" at " + (row+1) + ", " + (col+1));
}这样,只有在找到单词时才执行打印。
2.Exception
这是因为即使在到达最后一行之后,您仍在搜索row+1以查找下一个字母。这就是扔ArrayIndexOutOfBoundsException。因此,将另一个检查添加到打印机方法中的if语句如下:
if((g+1)<10 && grid[g+1][h]==found){
move="vertically";
}
else if((h+1)<10 && grid[g][h+1]==found){
move="horizontally";
}
else if((g-1)>-1 && grid[g-1][h]==found){
move="vertically";
}
else if((h-1)>-1 && grid[g][h-1]==found){
move="horizontally";
}
else if((g-1)>-1 && (h-1)>-1 && grid[g-1][h-1]==found){
move="diag";
}
else if((g+1)<10 && (h+1)<10 && grid[g+1][h+1]==found){
move="diag";
}
else if((g-1)>-1 && (h+1)<10 && grid[g-1][h+1]==found){
move="diag";
}
else if((h-1)>-1 && (g+1)<10 &&grid[g+1][h-1]==found){
move="diag";
}这样,只有在单元格不超过网格的行或列时,它才会检查单元格。
样本输出:
t a b o v e o y m z
s u q o e a v i u s
j f o e o a d l f w
t h r b q f b u x z
d g r t a t y u l p
a w c s n e t d s t
z s w e i r u e q e
q v g g a x l d z z
v a n i g r p d y t
c u d j a w d a n k
about - Pattern found diag at 5, 5
above - Pattern found horizontally at 1, 2
abuse - Pattern found diag at 3, 6
added - Pattern found vertically at 10, 8
adult - Pattern found diag at 2, 6
after - Pattern found vertically at 3, 6
again - Pattern found vertically at 10, 5
agent - Pattern found diag at 9, 2
agree - Pattern found diag at 6, 1发布于 2020-07-14 05:09:57
如果没有花括号,if语句只适用于后续语句。您应该添加花括号,以便只有当printer()返回true时,才能执行控制台的printer()和后续打印:
if (search2D(grid, row, col, word)) {
mov = printer(grid, row, col, word);
System.out.println(word + " - Pattern found " + mov + " at " + (row + 1) + ", " + (col + 1));
}https://stackoverflow.com/questions/62888167
复制相似问题