Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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