LeetCode LCP 3 Robot Adventure

Topic Analysis:

For the main core of this problem is for such "RURUU" If we assume that it is the end point coordinates (8,8) In fact, the number of a command string, as long as the number of R and U of statistics command string (for me For the example given, num_R == 2, num_U == 3 ), apparently regardless of whether we can reach the end, this instruction is at least possible to go more than once, then we will just go before it will reach the end of x subtracting wheel (for the example I given in terms of x == min (8/2, 8/3) == 2, the number of intermediate wheels go ignored, the position of the end point can be converted to (4,2 )), is able to find out at the last round of the situation that is less than the number required U, R number to reach the finish line, and then traverse the command string, step by step simulation can, once more than the end of x or y is never It may reach, for barrier point, too, as they can reach the target point to seek to, this judgment process can be written as a single function more convenient. When problem solving, we first determine the barriers to the point (x <= end point x, y <= end y, otherwise the meaning of the questions, the disorder is not valid point) can arrive at the point it is necessary to determine all the obstacles will not come back Analyzing then the end can reach the

This problem Code:

. 1  class Solution {
 2  public :
 . 3      int min ( int X, int Y) {
 . 4          return X <Y? X: Y;
 . 5      }
 . 6      BOOL Judge ( String Command, int x_num, int y_num, int X, int Y) {
 . 7          int TEMP = min (X / x_num, Y / y_num);        // TEMP common multiple recording 
. 8          X = X - * TEMP x_num;       // X is recorded in R & lt 
. 9          Y = Y - y_num * TEMP;      // y record is the U-
 10          // case of x and y represents the relative position of the end of the first cycle 
. 11          IF (x == 0 && y == 0 ) return  to true ;
 12 is          the else {
 13 is              int len_cmd = Command .size ();
 14              for ( int I = 0 ; I <len_cmd; I ++ ) {
 15                  IF (Command [I] == ' the U- ' ) {
 16                      -y - ;
 . 17                      IF (Y < 0 ) return  to false ;
18                 }else{
19                     x--;
20                     if(x < 0) return false;
21                 }
22                 if(x == 0 && y == 0) return true;
23             }
24         }
25         return true;
26     }
27     bool robot(string command, vector<vector<int>>& obstacles, int x, int Y) {
 28          int len_cmd = command.size ();
 29          int x_1 = 0 ;
 30          int Y_1 = 0 ;
 31 is          // statistical first cycle can come to a place 
32          for ( int I = 0 ; I <len_cmd ; I ++ ) {
 33 is              IF (Command [I] == ' R & lt ' ) {
 34 is                  x_1 ++ ;
 35              } the else {
 36                  Y_1 ++ ;
 37 [              }
 38 is         }
 39          // put every obstacle point (point of the disorder to be less than the end of the x and y x and y) as is the end of the seeking can reach the reach to false return 
40          int len_obs = obstacles.size ();
 41 is          for ( int I = 0 ; I <len_obs; I ++ ) {
 42 is              IF (Obstacles [I] [ 0 ] <= X && Obstacles [I] [ . 1 ] <= Y) {
 43 is                  // reach the return to false 
44 is                  IF ( Judge (Command, x_1, Y_1, Obstacles [I] [ 0 ], Obstacles [I] [ . 1 ]) == to true ) return  to false ;
 45              }
 46 is         }
 47          // if obstacles in all points to the end points will not directly reach the endpoint determines whether to reach 
48          IF (Judge (Command, x_1, Y_1, X, Y) == to true ) return  to true ;
 49          the else  return  to false ;
 50      }
 51 };

 

Guess you like

Origin www.cnblogs.com/findview/p/11616836.html