zoukankan      html  css  js  c++  java
  • 【leetcode】Word Search

    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.
     
     
     1 class Solution {
     2 public:
     3  
     4  
     5     int n,n1,n2;
     6  
     7    
     8     bool exist(vector<vector<char> > &board, string word) {
     9        
    10         n1=board.size();
    11         n2=board[0].size();
    12         n=word.length();
    13        
    14         bool flag=false;
    15        
    16         //vector<vector<bool> > visited(n1,vector<bool>(n2,false));
    17        
    18         bool **visited=new bool*[n1];
    19         for(int i=0;i<n1;i++)
    20         {
    21             visited[i]=new bool[n2];
    22             for(int j=0;j<n2;j++)
    23             {
    24                 visited[i][j]=false;
    25             }
    26         }
    27        
    28        
    29        
    30         for(int i=0;i<n1;i++)
    31         {
    32             for(int j=0;j<n2;j++)
    33             {
    34                 if(board[i][j]==word[0])
    35                 {
    36                     //注意visited采用引用传值
    37                     flag=flag||dfs(board,word,i,j,visited);
    38                     if(flag) return true;
    39                 }
    40             }
    41         }
    42        
    43        
    44         for(int i=0;i<n1;i++) delete[] visited[i];
    45        
    46         return false;
    47     }
    48    
    49  
    50     bool dfs(vector<vector<char> > &board,string &word,int i,int j,bool** &visited,int index=0)
    51     {
    52  
    53         if(board[i][j]!=word[index]||visited[i][j]) return false;
    54         if(index==n-1) return true;
    55        
    56         visited[i][j]=true;
    57        
    58         bool flag1=i+1<n1&&dfs(board,word,i+1,j,visited,index+1);
    59         bool flag2=j+1<n2&&dfs(board,word,i,j+1,visited,index+1);
    60         bool flag3=j-1>=0&&dfs(board,word,i,j-1,visited,index+1);
    61         bool flag4=i-1>=0&&dfs(board,word,i-1,j,visited,index+1);
    62        
    63         bool result=flag1||flag2||flag3||flag4;
    64        
    65         //由于是引用传值,所以没有找到的目标串时要把visited复原
    66         //if(result==false) visited[i][j]=false;         
    67         visited[i][j]=result;
    68         return result;          
    69     }
    70 };
     
     
     
     
  • 相关阅读:
    centos mysql 编译安装
    vsftp配置主动模式和被动模式
    vim中选择匹配文本删除技巧
    解决zabbix图中出现中文乱码问题
    zabbix图中出现中文乱码问题
    Centos下Yum安装PHP5.5,5.6,7.0
    一款点击图片进行无限循环的jquery手风琴特效
    一款兼容IE6并带有多图横向滚动的jquery特效
    一款jQuery满屏自适应焦点图切换特效
    一款jQuery实现重力弹动模拟效果特效,弹弹弹,弹走IE6
  • 原文地址:https://www.cnblogs.com/reachteam/p/4199859.html
Copyright © 2011-2022 走看看