[LeetCode124]Word Search


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.

For example,
Given board =

[
  ["ABCE"],
  ["SFCS"],
  ["ADEE"]
]
word  =  "ABCCED" , -> returns  true ,
word  =  "SEE" , -> returns  true ,
word  =  "ABCB" , -> returns  false .

Analysis:

The idea of this question is as follows:
(1) Find the 1st element of the word in the board.
(2) For each position found where the 1st element lies, recursively do:
(i) Search the around cell to see if the next element exists. (4 directions: (i-1,j),(i+1,j),(i,j-1),(i,j+1) )
(ii) If the word ends, return true.
(3) Return false if no matching found.
Note: A visited matrix is needed to store the positions where have already been visited. Details can be found in code.

Java

public class Solution {
    int[][] visited;
	public boolean exist(char[][] board, String word) {
        if(word.length()==0) return false;
		if(board.length==0 || board[0].length==0) return false;
        int row = board.length;
        int col = board[0].length;
        visited = new int [row][col];
        for(int i=0;i<row;i++){
        	for(int j=0;j<col;j++){
        		if(board[i][j] == word.charAt(0)){
        			visited[i][j] = 1;
        			if(search(board, word, -1, i, j, 1))
        				return true;
        			visited[i][j] = 0;
        		}
        	}
        }
        
        return false;
    }
	//op 0123 up down left right
	public boolean search(char[][] board, String word,int op,int i, int j, int matchLen){
		if(matchLen == word.length()) return true;
		int row = board.length;
		int col = board[0].length;
		if(i+1<row && op!=0){//down
			if(visited[i+1][j]==0 && board[i+1][j]==word.charAt(matchLen)){
				visited[i+1][j] =1;
				if(search(board, word, 1, i+1, j, matchLen+1))
					return true;
				visited[i+1][j]=0;
			}
		}
		if(i-1>=0 && op!=1){//up
			if(visited[i-1][j]==0 && board[i-1][j] == word.charAt(matchLen)){
				visited[i-1][j] = 1;
				if(search(board, word, 0, i-1, j, matchLen+1))
					return true;
				visited[i-1][j] = 0;
			}
		}
		if(j+1<col && op!=2){
			if(visited[i][j+1] ==0 && board[i][j+1] == word.charAt(matchLen)){
				visited[i][j+1] = 1;
				if(search(board, word, 3, i, j+1, matchLen+1))
					return true;
				visited[i][j+1] = 0;
			}
		}
		if(j-1>=0 && op!=3){
			if(visited[i][j-1] ==0 && board[i][j-1] == word.charAt(matchLen)){
				visited[i][j-1] = 1;
				if(search(board, word, 2, i, j-1, matchLen+1))
					return true;
				visited[i][j-1] = 0;
			}
		}
		return false;
	}
}
c++

class Solution {
public:
    bool searchWord(vector<vector<char>> &board,
            string word,
            int i,
            int j,
            int matchLen,
            int op,// 0 up, 1 down, 2 left, 3 right
            vector<vector<int>> &visited){
    if(matchLen == word.size()) return true;
    int row = board.size();
    int col = board[0].size();
    if(i+1<row && op!=0){
        if(visited[i+1][j]==0 && board[i+1][j]==word[matchLen]){
            visited[i+1][j] = 1;
            if(searchWord(board,word,i+1,j,matchLen+1,1,visited))
                return true;
            visited[i+1][j] = 0;
        }
    }
    if(i-1>=0 && op!=1){
        if(visited[i-1][j]==0 && board[i-1][j]==word[matchLen]){
            visited[i-1][j] = 1;
            if(searchWord(board,word,i-1,j,matchLen+1,0,visited))
                return true;
            visited[i-1][j] = 0;
        }
    }
    if(j+1<col && op!=2){
        if(visited[i][j+1]==0 && board[i][j+1]==word[matchLen]){
            visited[i][j+1] = 1;
            if(searchWord(board,word,i,j+1,matchLen+1,3,visited))
                return true;
            visited[i][j+1] = 0;
        }
    }
    if(j-1>=0 && op!=3){
        if(visited[i][j-1]==0 && board[i][j-1]==word[matchLen]){
            visited[i][j-1] = 1;
            if(searchWord(board,word,i,j-1,matchLen+1,2,visited))
                return true;
            visited[i][j-1] = 0;
        }
    }
    return false;
}
bool exist(vector<vector<char> > &board, string word) {
    int row = board.size();
    int col = board[0].size();
    vector<vector<int>> visited(row,vector<int>(col,0));
    for(int i=0;i<row;i++){
        for(int j=0;j<col;j++){
            if(board[i][j] == word[0])
                if(word.size()==1) return true;
                else{
                    visited[i][j] = 1;
                    if(searchWord(board,word,i,j,1,-1,visited))
                        return true;
                    visited[i][j] = 0;
                }
        }
    }
    return false;
}
};




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值