Posts

Showing posts with the label 回溯

1514. Robot Room Cleaner

Given a robot cleaner in a room 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 90 degrees. When it tries to move into a blocked cell, its bumper sensor detects the obstacle and it stays on the current cell. Design an algorithm to clean the entire room using only 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 on the current cell. boolean move(); // Robot will stay on the same cell after calling turnLeft/turnRight. // Each turn will be 90 degrees. void turnLeft(); void turnRight(); // Clean the current cell. void clean(); } The input is only given to initialize the room and the robot's position internally. You must solve this problem "blindfolded". In other words, you must control the ro...

914. Flip Game

class Solution { public:     /**      * @param s: the given string      * @return: all the possible states of the string after one valid move      */     vector<string> generatePossibleNextMoves(string &s) {         // write your code here         //寻找所有的‘++’然后变‘--’         vector<string> res;         if(s.size() == 0){             return res;         }         for(int i = 0; i < s.size() - 1; i++){             if(s[i] == '+' && s[i + 1] == '+'){                 s[i]= '-';                 s[i + 1] = '-';                 res.push_back(s);             ...