-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
51 lines (47 loc) · 1.41 KB
/
test.cpp
File metadata and controls
51 lines (47 loc) · 1.41 KB
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
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
#include <stack>
#include <unordered_map>
#include <queue>
#include <string.h>
using namespace std;
bool exist(vector<vector<char>>& board, string word) {
int rows = board.size();
int cols = board[0].size();
int lens = 0;
bool *visited = new bool[rows * cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (hasPathCore(board, rows, cols, i, j, word, lens, visited))
{
return true;
}
}
}
delete[] visited;
return false;
}
bool hasPathCore(const char* matrix, int rows, int cols, int i, int j,
const char* str, int& lens, bool* visited)
{
if(str[lens] == '\0') return true;
bool haspath = false;
if (i >= 0 && i < rows && j >=0 && j < cols
&& matrix[i * cols + j] == str[lens] && !visited[i * cols + j])
{
lens++;
visited[i * cols + j] = true;
haspath = hasPathCore(matrix, rows, cols, i, j - 1, str, lens, visited)
||hasPathCore(matrix, rows, cols, i - 1, j, str, lens, visited)
||hasPathCore(matrix, rows, cols, i + 1, j, str, lens , visited)
||hasPathCore(matrix, rows, cols, i + 1, j, str, lens, visited);
if (!haspath)
{
lens--;
visited[i * cols +j] = false;
}
}
return haspath;
}