Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.0 kB
5
Indexable
Never
class Solution {
public:
    bool isRobotBounded(string commands) {
        int x = 0;
        int y = 0;
                                      // N       S      E         W
        vector<pair<int,int>>coordinates{{1, 0}, {-1, 0}, {0, 1}, {0,-1}};
        
             int dir=0;
        
        for(int i = 0; i < commands.size(); ++i) {
            if(commands[i] == 'L') {
                if(dir==0)dir=3;
                else if(dir ==1)dir=2;
                 else if(dir==2)dir=0;
                else dir=1;
                
            }
            else if(commands[i] == 'R') {
                if(dir==0)dir=2;
                else if(dir==1)dir=3;
                 else if(dir==2)dir=1;
                else dir=0;
            }
            else {
                x += coordinates[dir].first;
                y += coordinates[dir].second;
            }
        }
        
       if((x == 0 & y == 0)||dir!=0)  return true;
       
       return false;
    }
};