-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1030.cpp
More file actions
24 lines (24 loc) · 823 Bytes
/
Copy path1030.cpp
File metadata and controls
24 lines (24 loc) · 823 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) {
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
vector<vector<bool>> visited(R, vector<bool>(C, false));
vector<vector<int>> res;
queue<vector<int>> q;
q.push(vector<int>{r0, c0});
visited[r0][c0] = true;
while (!q.empty()) {
vector<int> current = q.front();
q.pop();
res.push_back(current);
for (auto d:dir) {
int x = current[0] + d[0], y = current[1] + d[1];
if (x >= 0 && x < R && y >= 0 && y < C && !visited[x][y]) {
visited[x][y] = true;
q.push(vector<int>{x, y});
}
}
}
return res;
}
};