我正在尝试在 C 中的 2D 数组中即兴创作我的单词搜索程序。我还想打印找到单词的方向。南/西南/北等。
但是当 col 和 row 位置正确时,我似乎得到了一个不正确的方向。
通过调试,我看到意外的方向向量被迭代以匹配确切的单词。我很困惑为什么会发生这种情况,或者我错误地调试了。
在评论中,您将能够看到当找到第一个字母时,我如何构建用于在 8 个方向上遍历 2D 数组的向量。北、东假定为+ve。
预期输出:在南方向的第 0 行和第 0 列找到单词。
我的输出:在东方向的第 0 行和第 0 列找到单词。
这是我的代码:
#include <stdlib.h>
#include <stdbool.h>
/*
Considering N & E to be positive and S & W to be negative [x,y]
S = [0,-1]
SW = [-1,-1]
W = [-1,0]
NW = [-1,1]
N = [0,1]
NE = [1,1]
E = [1,0]
SE = [1,-1]
*/
//directional array for x and y axis creating the direction vectors.
int x[] = { 0, -1, -1, -1, 0, 1, 1, 1};
int y[] = { -1, -1, 0, 1, 1, 1, 0, -1};
void search2D(char word[], int wordSize, const int Size, char grid[Size][Size]);
bool search1D(char word[], int wordSize, const int Size, char grid[Size][Size], int row, int col);
void printFoundLocation(int rowDir, int colDir, int R, int C);
int rowDir = -2;
int colDir = -2;
void search2D(char word[], int wordSize, const int Size, char grid[Size][Size]) {
for (int i = 0; i < Size; i++)
{
for (int j = 0; j< Size; j++)
{
if (word[0] == grid[i][j])
{
if (search1D(word, wordSize, Size, grid, i, j) == true)
{
printFoundLocation(rowDir,colDir,i,j);
i = Size;
break;
} else {
printf("word not found");
i = Size;
break;
}
}
}
}
}
bool search1D(char word[], int wordSize, const int Size, char grid[Size][Size], int row, int col) {
for (int dir = 0; dir < 8; dir++)
{
int k, rd = row + x[dir], cd = col + y[dir];
//printf("x[dir] = %d\n", x[dir]);
//printf("y[dir] = %d\n", y[dir]);
for (k = 1; k < wordSize; k++) {
//if out of bounds, break.
if (rd >= Size || rd < 0 || cd >= Size || cd < 0)
break;
//if not matched, break.
if (grid[rd][cd] != word[k])
break;
//incremeant the x,y coordintes according to the direction vector.
rd += x[dir], cd += y[dir];
}
if (k == wordSize){
rowDir = x[dir];
//printf("rowdir = %d\n", rowDir);
colDir = y[dir];
//printf("coldir = %d\n", colDir);
return true;
}
}return false;
}
int main(void) {
char grid[4][4] = {
{'B', 'Q', 'D', 'F'},
{'E', 'S', 'X', 'E'},
{'N', 'A', 'C', 'D'},
{'B', 'E', 'T', 'F'}
};
char word[] = {'B','E','N'};
int wordSize = 3;
int gridSize = 4;
search2D(word, wordSize, gridSize, grid);
return 0;
}
void printFoundLocation(int rowDir, int colDir, int R, int C) {
if (rowDir == 0 && colDir == -1) printf("Word found at row %d and column %d in the south direction.",R,C);
if (rowDir == -1 && colDir == -1) printf("Word found at row %d and column %d in the south-west direction.",R,C);
if (rowDir == -1 && colDir == 0) printf("Word found at row %d and column %d in the west direction.",R,C);
if (rowDir == -1 && colDir == 1) printf("Word found at row %d and column %d in the north-west direction.",R,C);
if (rowDir == 1 && colDir == 1) printf("Word found at row %d and column %d in the north-east direction.",R,C);
if (rowDir == 1 && colDir == 0) printf("Word found at row %d and column %d in the east direction.",R,C);
if (rowDir == 1 && colDir == -1) printf("Word found at row %d and column %d in the south-east direction.",R,C);
} ```