[LeetCode] 在矩阵中查找字符串 Search a string in a matrix of chars

在矩阵中查找字符串 Search a string in a matrix of chars [Leetcode 124: 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 .
思路:用递归的方法,递归函数的状态是,在矩阵中的位置(r,c),和在字符串中的位置n,如果这两个位置上的字符相等,那么向上下左右4个方向继续递归搜索。

正确无误的代码:

class Solution {
public:
 
    bool search(vector<vector<char> > &board, int i,int j,string str,vector<vector<bool> > &mask){
        if (str.size()==0){return true;}
        else{
            if ((i>0)&&(board[i-1][j]==str[0])&&(mask[i-1][j]==false)){
                mask[i-1][j]=true;
                if (search(board,i-1,j,str.substr(1),mask)){
                    return true;
                }
                mask[i-1][j]=false;
            }
            if ((i<board.size()-1)&&(board[i+1][j]==str[0])&&(mask[i+1][j]==false)){
                mask[i+1][j]=true;
                if (search(board,i+1,j,str.substr(1),mask)){
                    return true;
                }
                mask[i+1][j]=false;
            }
            if ((j>0)&&(board[i][j-1]==str[0])&&(mask[i][j-1]==false)){
                mask[i][j-1]=true;
                if (search(board,i,j-1,str.substr(1),mask)){
                    return true;
                }
                mask[i][j-1]=false;
            }
            if ((j<board[0].size()-1)&&(board[i][j+1]==str[0])&&(mask[i][j+1]==false)){
                mask[i][j+1]=true;
                if (search(board,i,j+1,str.substr(1),mask)){
                    return true;
                }
                mask[i][j+1]=false;
            }
        }
        return false;
    }
 
    bool exist(vector<vector<char> > &board, string word) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (word.size()==0) {return false;}
             
        for (int i=0;i<board.size();i++){
            for (int j=0;j<board[0].size();j++){
                if (word[0]==board[i][j]){
                    if (word.size()==1) {return true;}
                    else{
                        vector<vector<bool> > mask(board.size(),vector<bool>(board[0].size(),false));
                        mask[i][j]=true;
                        if (search(board,i,j,word.substr(1),mask)){
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
};

下面是我自己的代码。对于小的输入,下面的算法没问题;对于大的输入,下面的代码也能输出正确值,但是会导致OJ报出超时错误。

class Solution {
public:
 
     bool exist(vector<vector<char> > &board, string word) {
         
        for(int i=0; i<board.size() ; i++)
            for(int j=0; j<board[0].size() ; j++)
            {
                vector<bool> nonU(board[0].size(), true);
                vector< vector<bool>> nonUse(board.size(), nonU);
                if( existU( board, i, j, word, 0, nonUse) )
                    return true;
            }
        
        return false;
    }
    
    bool existU(vector<vector<char> > &board, int r, int c, string& word, int n, vector<vector<bool> >& nonUse) {
        // Use n==word.length()-1 && word[n]==board[r][c]
        // Do not use // use n==word.length()
        // If using n==word.length(), the function *may* return false even if it has found the string
        if(n==word.length()-1 && word[n]==board[r][c]) 
            return true;
            
        if(board[r][c]!=word[n])
            return false;
        else
        {
            bool tmp1 =false, tmp2=false, tmp3=false, tmp4=false;
            nonUse[r][c] = false;
            
            if(r-1>=0 && nonUse[r-1][c] )
                tmp1 = existU(board, r-1, c, word, n+1, nonUse);
            if(tmp1) return true;
                
            if(r+1<board.size() && nonUse[r+1][c] )
                tmp2 = existU(board, r+1, c, word, n+1, nonUse);    
            if(tmp2) return true;
                
            if(c-1>=0 && nonUse[r][c-1] )
                tmp3 = existU(board, r, c-1, word, n+1, nonUse); 
            if(tmp3) return true;    
                
            if(c+1 < board[0].size() && nonUse[r][c+1] )
                tmp4 = existU(board, r, c+1, word, n+1, nonUse);
            if(tmp4) return true;
            
			nonUse[r][c] = true;
			
            return false;
        }
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值