Advertisement
nikunjsoni

1041

Sep 1st, 2021
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     bool isRobotBounded(string ins) {
  4.         // north=0, east=1, south=2, west=3
  5.         int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
  6.         int x=0, y=0;
  7.         int idx=0;
  8.         for(char ch: ins){
  9.             if(ch == 'L'){
  10.                 idx = (idx+3)%4;
  11.             }
  12.             else if( ch == 'R'){
  13.                 idx = (idx+1)%4;
  14.             }
  15.             else{
  16.                 x += dir[idx][0];
  17.                 y += dir[idx][1];
  18.             }
  19.         }
  20.         return (x==0 && y==0) || (idx != 0);
  21.     }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement