LeetCode--Word Search
2014-08-13 13:16
405 查看
dfs
https://github.com/cane1991/BasicAlogrithmSourceCode/blob/master/LeetCode/WordSearch.cpp
https://github.com/cane1991/BasicAlogrithmSourceCode/blob/master/LeetCode/WordSearch.cpp
/************************************************************************* > File Name: WordSearch.cpp > Author: zhoukang1991 > Mail: zhoukang199191@126.com > Created Time: 2014年08月14日 星期四 00时58分49秒 ************************************************************************/ #include <iostream> #include <vector> #include <string> using namespace std; class Solution{ public: bool exist(vector<vector<char> > &board,string word){ const int row = board.size(); if(row == 0) return false; const int col = board[0].size(); for(int i = 0 ; i < row ; ++i){ for(int j = 0 ; j < col ; ++j){ if(board[i][j] == word[0] && dfs(i,j,word,0,board)) return true; } } return false; } bool dfs(int row,int col,string &word,int index,vector<vector<char> > &board){ if(index = word.size()-1) return true; char ctmp = board[row][col]; board[row][col] = '.'; //left right up down dfs if(col-1 >= 0 && board[row][col-1] == word[index+1]){ if(dfs(row,col-1,word,index+1,board)){ return true; } } if(col+1 <= board[0].size() && board[row][col+1] == word[index+1]){ if(dfs(row,col+1,word,index+1,board)){ return true; } } if(row-1 >= 0 && board[row-1][col] == word[index+1]){ if(dfs(row-1,col,word,index+1,board)) return true; } if(row+1 <= board[0].size() && board[row+1][col] == word[index+1]){ if(dfs(row+1,col,word,index+1,board)) return true; } } }; int main() { return 0; }
相关文章推荐
- [leetcode] 212.Word Search II
- Word Search | LeetCode
- Leetcode之Word Search 问题
- [Leetcode]Word Search
- LeetCode-79-Word Search 爆搜
- [LeetCode] Word Search
- LeetCode | Word Search
- 【Leetcode】Word Search
- leetcode笔记:Word Search
- [LeetCode]105. Word Search单词查找
- 【LeetCode-面试算法经典-Java实现】【079-Word Search(单词搜索)】
- leetcode: word search
- leetcode 79. Word Search DFS 单词搜索 + 深度优先遍历
- (java)leetcode-79:Word Search
- 算法分析与设计课程——LeetCode刷题之 Word Search
- [LeetCode]Word Search
- LeetCode:Word Search
- 【LeetCode】Word Search
- 【leetcode】Word Search
- leetcode79 Word Search