leetcode79单词搜索
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例:
board =
[
[‘A’,‘B’,‘C’,‘E’],
[‘S’,‘F’,‘C’,‘S’],
[‘A’,‘D’,‘E’,‘E’]
]
给定 word = “ABCCED”, 返回 true.
给定 word = “SEE”, 返回 true.
给定 word = “ABCB”, 返回 false.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/word-search
题目分析
很经典的dfs问题,这类问题一般解法是栈和递归,思路如下:先从二维数组找到和word第一个字符相同的元素,然后遍历数组四个方向,和下一个字符相等就继续dfs,否则就要回溯
栈版本
public class WordSearch {
public static void main(String[] args) {
System.out.println(
new WordSearch().exist(
new char[][]{{'A','C','C','F'},{'S','F','C','S'},{'A','D','E','E'}},
"FCSEEDAS"));
}
public boolean exist(char[][] board, String word) {
m = board.length;
if(m==0||word.length()==0)return false;
n = board[0].length;
for(int i=0;i<board.length;i++){
for(int j=0;j<board[0].length;j++){
if(board[i][j]==word.charAt(0)){
if(exist(board,word,i,j)){
return true;
}
}
}
}
return false;
}
private int m,n;
private int[][] direction = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
public boolean exist(char[][] board, String word,int i,int j) {
int len = 1;
int[][] mark = new int[board.length][board[0].length];
Stack<int[]> stack = new Stack<>();
stack.push(new int[]{i,j,len});
// i-1,j(上) i,j-1(左) i,j+1(右) i+1,j(下)
while(!stack.isEmpty()){
//只peek不pop
int x = stack.peek()[0];
int y = stack.peek()[1];
len = stack.peek()[2];
//出口
if(len==word.length())return true;
//坑已被占了,此路不通
if(mark[x][y]==1){
stack.pop();
mark[x][y]=0;
continue;
}
//占坑
mark[x][y]=1;
//遍历四个方向
for(int k=0;k<4;k++){
//新坐标
int newX = x+direction[k][0];
int newY = y+direction[k][1];
//符合条件就入栈
if(inRange(newX,newY)&&word.charAt(len)==board[newX][newY]&&mark[newX][newY]!=1){
stack.push(new int[]{newX,newY,len+1});
}
}
}
return false;
}
private boolean inRange(int i,int j){
if(i>=0&&i<m&&j>=0&&j<n){
return true;
}
return false;
}
}
递归版本
public class WordSearch1 {
public static void main(String[] args) {
System.out.println(
new WordSearch1().exist(
new char[][]{{'A','C','C','F'},{'S','F','C','S'},{'A','D','E','E'}},
"FCSEEDAS"));
}
private int m,n;
private int[][] direction = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
private boolean[][] mark;
public boolean exist(char[][] board, String word) {
m = board.length;
if(m==0||word.length()==0)return false;
n = board[0].length;
mark = new boolean[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(dfs(board,word,i,j,0)){
return true;
}
}
}
return false;
}
private boolean dfs(char[][] board, String word, int i, int j,int index) {
if(word.charAt(index)==board[i][j]){
if(index==word.length()-1){
return word.charAt(index)==board[i][j];
}
mark[i][j] = true;
for(int k=0;k<4;k++){
int newI = i+direction[k][0];
int newJ = j+direction[k][1];
//在范围内,相等,没有被占
if(inRange(newI,newJ)&&word.charAt(index+1)==board[newI][newJ]&&!mark[newI][newJ]){
if(dfs(board,word,newI,newJ,index+1)){
return true;
}
}
}
mark[i][j] = false;
}
return false;
}
private boolean inRange(int i,int j){
if(i>=0&&i<m&&j>=0&&j<n){
return true;
}
return false;
}
}