[LeetCode124]Word Search

来源:互联网 发布:注册短信验证php 编辑:程序博客网 时间:2024/06/05 19:44


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 rightpublic 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){//downif(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){//upif(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;}};




0 0
原创粉丝点击