Skip to content

Instantly share code, notes, and snippets.

@ik11235
Created February 28, 2015 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ik11235/10f942b059c3f106fcf4 to your computer and use it in GitHub Desktop.
Save ik11235/10f942b059c3f106fcf4 to your computer and use it in GitHub Desktop.
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