给定一个二维字符矩阵,我们必须检查给定的单词是否存在于其中。例如
s f t
d a h
r y o
我们可以在其中找到“老鼠” (自上而下、笔直、对角线或任何路径)..即使是相反的顺序。复杂性最低。
我的方法是
While traversing the 2d matrix ( a[][] ) row wise.
If ( a[i][j] == first character of given word ) {
search for rest of the letters in 4 directions i.e. right, right diagonally down, down and left diagonally down.
} else if( a[i][j] == last character of the given word ) {
search for remaining characters in reverse order in 4 directions i.e. left, right diagonally up, up, left diagonally up.
}
有没有更好的方法?