forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
114 lines (93 loc) · 2.93 KB
/
main.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/// Source : https://leetcode.com/problems/the-maze/description/
/// Author : liuyubobobo
/// Time : 2018-07-11
#include <iostream>
#include <vector>
#include <cassert>
#include <queue>
using namespace std;
/// BFS
/// Time Complexity: O(mn)
/// Space Complexity: O(mn)
class Solution {
private:
const int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int n, m;
public:
bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
n = maze.size();
if(n == 0) return false;
m = maze[0].size();
if(m == 0) return false;
assert(start.size() == 2 && destination.size() == 2);
assert(inArea(start[0], start[1]) && maze[start[0]][start[1]] == 0);
assert(inArea(destination[0], destination[1]) &&
maze[destination[0]][destination[1]] == 0);
queue<pair<int, int>> q;
q.push(make_pair(start[0], start[1]));
vector<vector<bool>> visited(n, vector<bool>(m, false));
visited[start[0]][start[1]] = true;
while(!q.empty()){
pair<int, int> cur = q.front();
q.pop();
for(int i = 0; i < 4 ; i ++){
pair<int, int> next = go(maze, cur, i);
if(!visited[next.first][next.second]){
if(next.first == destination[0] && next.second == destination[1])
return true;
q.push(next);
visited[next.first][next.second] = true;
}
}
}
return false;
}
private:
pair<int, int> go(const vector<vector<int>>& maze,
pair<int, int> pos, int i){
assert(maze[pos.first][pos.second] == 0);
while(true){
int nextX = pos.first + d[i][0];
int nextY = pos.second + d[i][1];
if(inArea(nextX, nextY) && maze[nextX][nextY] == 0){
pos.first = nextX;
pos.second = nextY;
}
else
return pos;
}
assert(false);
return make_pair(-1, -1);
}
bool inArea(int x, int y){
return x >= 0 && x < n && y >= 0 && y < m;
}
};
void print_bool(bool res){
cout << (res ? "True" : "False") << endl;
}
int main() {
vector<vector<int>> maze1 = {
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{1, 1, 0, 1, 1},
{0, 0, 0, 0, 0}
};
vector<int> start1 = {0, 4};
vector<int> destination1 = {4, 4};
print_bool(Solution().hasPath(maze1, start1, destination1));
// True
vector<vector<int>> maze2 = {
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{1, 1, 0, 1, 1},
{0, 0, 0, 0, 0}
};
vector<int> start2 = {0, 4};
vector<int> destination2 = {3, 2};
print_bool(Solution().hasPath(maze2, start2, destination2));
// False
return 0;
}