/** * // 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(); * }; */ classSolution { public: voidcleanRoom(Robot& robot){ int x = 0; int y = 0; unordered_map<int, unordered_map<int, bool>> visited; int direction = 0; cleanHelper(robot, visited, x, y, direction); return; } voidcleanHelper(Robot& robot, unordered_map<int, unordered_map<int, bool>>& visited, int x, int y, int direction){ if (visited[x][y] == true) return; vector<vector<int>> directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; visited[x][y] = true; robot.clean(); for (int i = 0; i < 4; ++i) { if (robot.move()) { cleanHelper(robot, visited, x + directions[direction][0], y + directions[direction][1], direction); robot.turnLeft(); robot.turnLeft(); robot.move(); robot.turnLeft(); robot.turnLeft(); } robot.turnLeft(); direction = (direction + 1) % 4; } return; } };