-
Notifications
You must be signed in to change notification settings - Fork 3
/
489.cpp
46 lines (44 loc) · 1.53 KB
/
489.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
/**
* // This is the robot's control interface.
* // You should not implement it, or speculate about its implementation
* class Robot {
* public:
* // Returns true if the cell in front is open and robot moves into the cell.
* // Returns false if the cell in front is blocked and robot stays in the current cell.
* bool move();
*
* // Robot will stay in the same cell after calling turnLeft/turnRight.
* // Each turn will be 90 degrees.
* void turnLeft();
* void turnRight();
*
* // Clean the current cell.
* void clean();
* };
*/
class Solution {
public:
vector<pair<int, int>> directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
void dfs(Robot& robot, int x, int y, int currDirect, unordered_set<string>& st) {
robot.clean();
st.insert(to_string(x) + "_" + to_string(y));
for (int i = 0; i < directions.size(); i++) {
int _currDirect = (currDirect + i) % 4;
int _x = x + directions[_currDirect].first;
int _y = y + directions[_currDirect].second;
if (st.find(to_string(_x) + "_" + to_string(_y)) == st.end() && robot.move()) {
dfs(robot, _x, _y, _currDirect, st);
robot.turnRight();
robot.turnRight();
robot.move();
robot.turnLeft();
robot.turnLeft();
}
robot.turnRight();
}
}
void cleanRoom(Robot& robot) {
unordered_set<string> st;
dfs(robot, 0, 0, 0, st);
}
};