Created
February 28, 2015 18:51
-
-
Save ik11235/10f942b059c3f106fcf4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RobotOnMoonEasy { | |
public: | |
string isSafeCommand(vector<string> board, string S) { | |
int x,y; | |
for (int i=0; i<board.size(); i++) { | |
for (int j=0;j<board[i].size(); j++) { | |
if(board[i][j]=='S') | |
{ | |
x=j; | |
y=i; | |
board[i][j]=='.'; | |
break; | |
} | |
} | |
} | |
int dx[]={-1,1,0,0}; | |
int dy[]={0,0,-1,1}; | |
string move="UDLR"; | |
for(int i=0;i<S.size();i++) | |
{ | |
for(int j=0;j<4;j++) | |
{ | |
if(move[j]==S[i]) | |
{ | |
int nx=x+dx[j]; | |
int ny=y+dy[j]; | |
if(nx<0 ||nx>=board.size() || | |
ny<0 || ny>=board[x].size()) | |
return "Dead"; | |
if(board[nx][ny]=='.') | |
{ | |
x+=dx[j]; | |
y+=dy[j]; | |
} | |
break; | |
} | |
} | |
} | |
return "Alive"; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment