Advertisement
nikunjsoni

489

May 1st, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. /**
  2.  * // This is the robot's control interface.
  3.  * // You should not implement it, or speculate about its implementation
  4.  * class Robot {
  5.  *   public:
  6.  *     // Returns true if the cell in front is open and robot moves into the cell.
  7.  *     // Returns false if the cell in front is blocked and robot stays in the current cell.
  8.  *     bool move();
  9.  *
  10.  *     // Robot will stay in the same cell after calling turnLeft/turnRight.
  11.  *     // Each turn will be 90 degrees.
  12.  *     void turnLeft();
  13.  *     void turnRight();
  14.  *
  15.  *     // Clean the current cell.
  16.  *     void clean();
  17.  * };
  18.  */
  19.  
  20. class Solution {
  21. unordered_map<int, bool> vis;
  22. int rowMax = (1<<16);
  23. int d[4][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}};
  24. public:
  25.     void cleanRoom(Robot& robot) {
  26.         dfs(robot, 0, 0, 0);
  27.     }
  28.    
  29.     void dfs(Robot &robot, int x, int y, int dir){
  30.         int curr = x*rowMax+y;
  31.         if(vis.count(curr)) return;
  32.         vis[curr] = true;
  33.         robot.clean();
  34.        
  35.         for(int i=0; i<4; i++){
  36.             if(robot.move()){
  37.                 dfs(robot, x+d[dir][0], y+d[dir][1], dir);
  38.                 robot.turnRight();
  39.                 robot.turnRight();
  40.                 robot.move();
  41.                 robot.turnRight();
  42.                 robot.turnRight();
  43.             }
  44.             robot.turnRight();
  45.             dir = (dir+1)%4;
  46.         }
  47.     }
  48.    
  49. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement