标签:tco pre exist board || char 搜索 code public
#include <vector> class Solution { public: int n,m; //标记是否被访问过 vector<vector<bool>> visit; // idx(不包括)之前的都已经被访问过了 bool dfs(int i,int j,vector<vector<char>>& board, int idx, string word) { if(idx == word.size()) return true; // 判断先决条件 if(i<0 || j<0 || i>=n || j>=m || visit[i][j] || word[idx]!=board[i][j]) return false; visit[i][j]=true; bool ret= dfs(i-1,j,board,idx+1,word) || dfs(i+1,j,board,idx+1,word) || dfs(i,j-1,board,idx+1,word) || dfs(i,j+1,board,idx+1,word); visit[i][j] = false; return ret; } bool exist(vector<vector<char>>& board, string word) { // 条件判断 n= board.size(); if(n==0) return false; m= board[0].size(); if (m==0) return false; visit.resize(n,vector<bool>(m, false)); for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j) { if(dfs(i,j,board,0,word)) { return true; } } } return false; } };
标签:tco pre exist board || char 搜索 code public
原文地址:https://www.cnblogs.com/randyniu/p/9229134.html