Robot Problems

  1. The robot needs to explore all cells. However ,we need to pick one direction from one point at a given moment and go fully with it. We use DFS to do so
DFS()
{
mark the cell as visited in this dfs
For all possible directions()
{
//make sure the robot can move and the next cell is unvisited
dfs(with new position)
//rotate the robot's direction
}//Add snippet to update the robot's position and orientation as per the API given below}
// Moves backward one step while maintaining the orientation.
robot.turnRight();
robot.turnRight();
robot.move();
robot.turnRight();
robot.turnRight();
class Solution {
public:
set<pair<int,int> >vis;
int directions[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
void goBack(Robot&obj)
{
obj.turnRight();
obj.turnRight();
obj.move();
obj.turnRight();
obj.turnRight();

}
void func(int x,int y,Robot&obj,int dir)
{

vis.insert(make_pair(x,y));
obj.clean();

for(int i=0;i<4;i++)
{
int idx=(i+dir)%4;
int newx=x+directions[idx][0];
int newy=y+directions[idx][1];
if(vis.find(make_pair(newx,newy))==vis.end()&&obj.move())
{
func(newx,newy,obj,idx);
goBack(obj);


}
obj.turnRight();

}
}
void cleanRoom(Robot& robot) {
/*
if you hit an obstacle moveright again
*/
func(0,0,robot,2);

}
};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store