ฉันกำลังหาวิธีค้นหาคำในตารางตัวต่อคำศัพท์
ตัวอักษรถูกใส่ไว้ในเมทริกซ์ วิธีการควรพิมพ์แถวและคอลัมน์ที่พบตัวอักษรตัวแรกของคำและทิศทาง (n, s, e, w, ne, nw, se, sw) ที่รูปแบบเข้าไป
วิธีที่ฉันมีในปัจจุบันคือ:
public static String wordSearch(int r, int c, String letters, String search){
int x[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int y[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
char [][] puzzle = new char [r][c];
int count = 0;
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++){
puzzle[i][j] = letters.charAt(count);
count++;
}
}
int length = search.length();
for (int dir = 0; dir < 8; dir++)
{
int k;
int rd = r + x[dir];
int cd = c + y[dir];
for (k = 1; k < length; k++)
{
rd += x[dir];
cd += y[dir];
}
if (k == length)
return rd+ ", "+cd;
}
return "Not found";}
ปัญหาคือการหาวิธีคืนทิศทางของรูปแบบไปพร้อมกับตัวอักษรตัวแรกของคำ (ผ่านกรณีทดสอบเพียงกรณีเดียว) อย่างไรก็ตาม ฉันมั่นใจว่าวิธีการนี้สามารถบอกได้ว่าคำนั้นอยู่ในตารางหรือไม่