-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexist.cpp
41 lines (36 loc) · 1.29 KB
/
exist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
bool back(vector<vector<char>>& board, string word, vector<vector<bool>>& hash, int row, int col){
if(word.size() == 0)
return true;
if(row < 0 || row >= board.size() || col < 0 || col >= board[0].size())
return false;
if(hash[row][col] == true)
return false;
if(board[row][col] != word[0])
return false;
hash[row][col] = 1;
word = word.substr(1);
if(back(board, word, hash, row - 1, col) == false &&
back(board, word, hash, row + 1, col) &&
back(board, word, hash, row , col - 1) &&
back(board, word, hash, row , col + 1)){
hash[row][col] = false;
return false;
}
return true;
}
bool exist(vector<vector<char>>& board, string word) {
vector<vector<bool> > hash(board.size(), vector<bool>(board[0].size(), false));
for(int i = 0; i < board.size(); ++i){
for(int j = 0; j < board[0].size(); ++j){
if(back(board, word, hash, i, j)){
return true;
}
}
}
return false;
}