Skip to content

Commit 3d58e71

Browse files
Create Snake in Matrix.cpp
1 parent 4456fdd commit 3d58e71

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Snake in Matrix.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int finalPositionOfSnake(int n, vector<string>& commands) {
4+
vector<vector<int>>temp(n,vector<int>(n,0));
5+
int c = 0;
6+
for(int i=0;i<n;i++){
7+
for(int j=0;j<n;j++ ){
8+
temp[i][j] = c++;
9+
}
10+
}
11+
12+
13+
int curri =0,currj=0;
14+
for(int i=0;i<commands.size();i++){
15+
if(commands[i] == "RIGHT") {
16+
currj +=1;
17+
}else if(commands[i] =="UP"){
18+
curri -=1;
19+
}else if(commands[i] == "LEFT"){
20+
currj -=1;
21+
}else{
22+
curri +=1;
23+
}
24+
}
25+
26+
27+
return temp[curri][currj];
28+
}
29+
};

0 commit comments

Comments
 (0)