Description
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
Example:
1 | board = |
解法
典型的DFS搜索题,以矩阵的每一个点为起点进行搜索,知道找到匹配项或者矩阵搜索完成,注意不能重复访问点,所以采用一个数组记录是否该点被访问过,还有回溯的写法,搜索完成后重新将点标记为未访问状态。
具体代码如下:
1 | class Solution { |