forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
77 lines (59 loc) · 1.75 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
/// Source : https://leetcode.com/problems/walls-and-gates/description/
/// Author : liuyubobobo
/// Time : 2018-08-25
/// Updated: 2019-06-12
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/// BFS
/// Time Complexity: O(n*m)
/// Space Complexity: O(n*m)
class Solution {
private:
const int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int m, n;
const int GATE = 0;
const int EMPTY = INT_MAX;
const int WALL = -1;
public:
void wallsAndGates(vector<vector<int>>& rooms) {
m = rooms.size();
if(m == 0)
return;
n = rooms[0].size();
bfs(rooms);
return;
}
private:
void bfs(vector<vector<int>>& rooms){
queue<pair<pair<int, int>, int>> q;
for(int i = 0; i < m; i ++)
for(int j = 0; j < n; j ++)
if(rooms[i][j] == GATE)
q.push(make_pair(make_pair(i, j), 0));
vector<vector<bool>> visited(m, vector<bool>(n, false));
while(!q.empty()){
int curx = q.front().first.first;
int cury = q.front().first.second;
int step = q.front().second;
q.pop();
rooms[curx][cury] = step;
for(int i = 0; i < 4; i ++){
int newX = curx + d[i][0];
int newY = cury + d[i][1];
if(inArea(newX, newY) && !visited[newX][newY] && rooms[newX][newY] == EMPTY){
visited[newX][newY] = true;
rooms[newX][newY] = step + 1;
q.push(make_pair(make_pair(newX, newY), step + 1));
}
}
}
}
bool inArea(int x, int y){
return x >= 0 && x < m && y >= 0 && y < n;
}
};
int main() {
return 0;
}