Skip to content

Commit 8128f9f

Browse files
authored
Update 489-robot-room-cleaner.js
1 parent b9ea328 commit 8128f9f

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

489-robot-room-cleaner.js

+36
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,39 @@ const cleanRoom = function(robot) {
6363
}
6464
}
6565
}
66+
67+
// another
68+
69+
/**
70+
* @param {Robot} robot
71+
* @return {void}
72+
*/
73+
const cleanRoom = function(robot) {
74+
const dirs = [
75+
[-1, 0],
76+
[0, 1],
77+
[1, 0],
78+
[0, -1]
79+
]
80+
const visited = new Set()
81+
clean(0, 0, 0)
82+
function clean( x, y, curDirection) {
83+
robot.clean()
84+
visited.add(`${x},${y}`)
85+
for(let i = curDirection; i < curDirection + 4; i++) {
86+
const nx = dirs[i % 4][0] + x
87+
const ny = dirs[i % 4][1] + y
88+
if(!visited.has(`${nx},${ny}`) && robot.move()) {
89+
clean(nx, ny, i % 4)
90+
}
91+
robot.turnRight()
92+
}
93+
robot.turnRight()
94+
robot.turnRight()
95+
robot.move()
96+
robot.turnRight()
97+
robot.turnRight()
98+
99+
}
100+
};
101+

0 commit comments

Comments
 (0)