代码:
class Solution1030_easy {
public:
struct Point {
Point(int x, int y) : x_(x), y_(y) {
}
int x_;
int y_;
};
vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) {
vector<vector<int>> dirs{ {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
vector<vector<int>> output;
vector<vector<int>> byteMap(R, vector<int>(C, 0));
queue<Point> q;
q.push(Point(r0, c0));
byteMap[r0][c0] = 1;
while (q.size()) {
Point pt = q.front();
q.pop();
output.push_back(vector<int>{pt.x_, pt.y_});
for (int i = 0; i < 4; i++) {
int x = pt.x_ + dirs[i][0];
int y = pt.y_ + dirs[i][1];
if (x < R && y < C && x >= 0 && y >= 0 && !byteMap[x][y]) {
q.push(Point(x, y));
byteMap[x][y] = 1;
}
}
}
return output;
}
};