LeeCode874. 模拟行走机器人 其中对于4个方向的机器人进行累加前进一步的处理方式和方向的计算方式

 class Solution {
public:
    int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
        int dx[4] = {0, 1, 0, -1};
        int dy[4] = {1, 0, -1, 0};
        int x = 0, y = 0, di = 0;

        unordered_set<pair<int, int>> obstacleSet;
        for (vector<int> obstacle: obstacles)
            obstacleSet.insert(make_pair(obstacle[0], obstacle[1]));

        int ans = 0;
        for (int cmd: commands) {
            if (cmd == -2)
                di = (di + 3) % 4;
            else if (cmd == -1)
                di = (di + 1) % 4;
            else {
                for (int k = 0; k < cmd; ++k) {
                    int nx = x + dx[di];
                    int ny = y + dy[di];
                    if (obstacleSet.find(make_pair(nx, ny)) == obstacleSet.end()) {
                        x = nx;
                        y = ny;
                        ans = max(ans, x*x + y*y);
                    }
                }
            }
        }

        return ans;
    }
};

//不那么漂亮的代码

class Solution {
     const static int x_direction = 1;
     const static int x_reverse_direction = 2;
     const static int y_direction = 3;
     const static int y_reverse_direction = 4;
public:
    int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {

        unordered_set<string> s;
        for(auto & vec : obstacles) {
            s.insert(string(to_string(vec[0])+"x"+to_string(vec[1])));
        }

        int max_distance = 0;
        int direction = y_direction;
        int x = 0;
        int y = 0;

        for(auto command : commands) {
            if(command == -2 || command == -1){
                direction = getDirection(command, direction);
            }else{
                switch(direction) {
                    case y_direction:
                        {
                            for(int i = 0; i<command; i++) {
                                if(s.find(to_string(x)+"x"+to_string(y+1)) == s.end()) {
                                    y+=1;
                                }else{
                                    break;
                                }

                            }
                            break;       
                        }
                    case x_direction:
                        {
                            for(int i = 0; i<command; i++) {
                                if(s.find(to_string(x+1)+"x"+to_string(y)) == s.end()) {
                                    x+=1;
                                }else{
                                    break;
                                }  
                            }
                            break;       
                        }
                    case y_reverse_direction:
                        {
                            for(int i = 0; i<command; i++) {
                                if(s.find(to_string(x)+"x"+to_string(y-1)) == s.end()) {
                                    y-=1;
                                }else{
                                    break;
                                }  
                            }
                            break;       
                        }
                    case  x_reverse_direction:
                         {
                            for(int i = 0; i<command; i++) {
                                if(s.find(to_string(x-1)+"x"+to_string(y)) == s.end()) {
                                    x-=1;
                                }else{
                                    break;
                                }  
                            }
                            break;       
                        }
                    default:
                        break;
                }

                int d = x*x+ y*y;

                if(max_distance < d) {
                    max_distance = d;
                }               
            }

        } 

        return max_distance;                      

    }


    int getDirection(int command, int direction) {
        if(command == -2) {
            switch(direction) {
                case y_direction:
                    return x_reverse_direction;
                case x_direction:
                    return y_direction;
                case y_reverse_direction:
                    return x_direction;
                case  x_reverse_direction:
                    return y_reverse_direction;
                default:
                    break;
            }
        }else if(command == -1) {
              switch(direction) {
                case y_direction:
                    return x_direction;
                case x_direction:
                    return y_reverse_direction;
                case y_reverse_direction:
                    return x_reverse_direction;
                case  x_reverse_direction:
                    return y_direction;
                default:
                    break;
            }  
        }
        return direction;
    }
};