Given a robot cleaner in a class modeled as a grid.
Each cell in the grid can be empty or blocked.
The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is degrees.
When it tries-move into a blocked cell, its bumper sensor detects the obstacle and it stays on the current cell.
The Design an algorithm to clean the entire-in-the-4 given APIs shown below.
Interface Robot {
Returns true if Next cell is open and robot moves into the cell.
Returns False if Next cell is obstacle and robot stays in the current cell.
Boolean move ();
Robot would stay on the same cell after calling Turnleft/turnright.
Each turn would be degrees.
void TurnLeft ();
void TurnRight ();
Clean the current cell.
void Clean ();
}
Example:
Input:
the * = [
[1,1,1,1,1,0,1,1],
[1,1,1,1,1,0,1,1],
[1,0,1,1,1,1,1,1],
[0,0,0,1,0,0,0,0],
[1,1,1,1,1,1,1,1]
],
row = 1,
Col = 3
Explanation:
All grids in the hostel is marked by either 0 or 1.
0 means the cell is blocked and while 1 means the cell is accessible.
The robot initially starts at the position of Row=1, col=3.
From the top left corner, the IT position is a row below and three columns right.
Notes:
1. The input is a given to initialize the the and the robot ' s position internally. You must solve this problem "blindfolded". In other words, you must control the robot using only the mentioned 4 APIs, without knowing the "the" layout and the Initia L robot ' s position.
2. The robot ' s initial position is always is in an accessible cell.
3. The initial direction of the robot would be a facing up.
4. All accessible cells is connected, which means the all cells marked as 1 would be accessible by the robot.
5. Assume all four edges of the grid is all surrounded by wall.
Dfs.
1. Recursive exit: If swept, go.
2. Deal with this state: sweep the floor, add records to Memo.
3. Recursion, four direction: turn to try to move, recursive, back to the original position.
Details:
1. Control two objects when the subject is moved: one is to use the robot's API to make it really try to move in four directions, one is to use the traditional dxdy to calculate the moving coordinates for the weight.
2. Coordinates with the set to the weight of small trick, can be in a normalized string form into the set.
3. The main thing is to backtrack because the API is inconvenient, and the general DFS write is not quite the same, nothing else changes.
Realize:
/***//This is the robot's control interface. *//You should not implement it, or speculate on its implementation * Interface Robot {*///Returns True if the cell in front are open and Robot moves into the cell. *//Returns Fals e If the cell in front are blocked and robot stays in the current cell. * Public boolean move (); * *//Robot'll stay in the same cell after calling Turnleft/turnright. *//each turn would be degrees. * public void turnleft (); * public void turnright (); * *//clean the current cell. * public void clean (); * } */classSolution { Public voidcleanroom (Robot Robot) {dfs (Robot,NewHashset<string> (), 0, 0, 0); } Private voidDFS (Robot Robot, Set<string> visited,intXintYintCrtdir) { if(Visited.contains (x + "+" +y)) {return; } int[] dx = {-1, 0, 1, 0}; int[] dy = {0,-1, 0, 1}; Robot.clean (); Visited.add (x+ "+" +y); for(inti = 0; I < 4; i++) { //P1:4 Direction cycle time not only to control their own virtual coordinates, but also to control the robot's API so that it really turned a 90 degrees. intDir = (Crtdir + i + 1)% 4; Robot.turnright (); if(!Robot.move ()) { Continue; } intNX = x +Dx[dir]; intNY = y +Dy[dir]; DFS (robot, visited, NX, NY, dir); //go back. Because executed "Move ()", backtrack for it.Robot.turnleft (); Robot.turnleft (); Robot.move (); Robot.turnright (); Robot.turnright (); } }}
Leetcode489-robot Cleaner-hard