- 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记录最小公倍数
- 8 x = x - x_num*temp; //x记录的是R
- 9 y = y - y_num*temp; //y记录的是U
- 10 //此时的x和y代表终点相对于第一次循环的位置
- 11 if(x == 0 && y == 0) return true;
- 12 else{
- 13 int len_cmd = command.size();
- 14 for(int i = 0; i < len_cmd; i++){
- 15 if(command[i] == 'U'){
- 16 y--;
- 17 if(y < 0) return 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 //统计第一轮循环能走到的地方
- 32 for(int i = 0; i < len_cmd; i++){
- 33 if(command[i] == 'R'){
- 34 x_1++;
- 35 }else{
- 36 y_1++;
- 37 }
- 38 }
- 39 //把每一个障碍点(该障碍点的x和y都要小于终点的x和y)当做是终点求能否到达 能到达则return false
- 40 int len_obs = obstacles.size();
- 41 for(int i = 0; i < len_obs; i++){
- 42 if(obstacles[i][0] <= x && obstacles[i][1] <= y){
- 43 //能到达则return false
- 44 if(judge(command, x_1, y_1, obstacles[i][0], obstacles[i][1]) == true) return false;
- 45 }
- 46 }
- 47 //如果所有的终点之内的障碍点都不会到达 则直接判断终点是否可以到达
- 48 if(judge(command, x_1, y_1, x, y) == true) return true;
- 49 else return false;
- 50 }
- 51 };