forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
32 lines (32 loc) · 1 KB
/
s1.cpp
File metadata and controls
32 lines (32 loc) · 1 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
// OJ: https://leetcode.com/problems/robot-room-cleaner/
// Author: github.com/lzl124631x
// Time: O(MN) where M and N are the width and height of the room.
// Space: O(MN)
class Solution {
private:
int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
unordered_map<int, unordered_set<int>> seen;
void dfs(Robot& robot, int x, int y, int dir) {
for (int i = 0; i < 4; ++i, robot.turnRight(), dir = (dir + 1) % 4) {
int nx = x + dirs[dir][0];
int ny = y + dirs[dir][1];
if (seen[nx].find(ny) != seen[nx].end()) continue;
seen[nx].insert(ny);
bool movable = robot.move();
if (!movable) continue;
robot.clean();
dfs(robot, nx, ny, dir);
}
robot.turnRight();
robot.turnRight();
robot.move();
robot.turnRight();
robot.turnRight();
}
public:
void cleanRoom(Robot& robot) {
robot.clean();
seen[0].insert(0);
dfs(robot, 0, 0, 0);
}
};