We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4456fdd commit 3d58e71Copy full SHA for 3d58e71
Snake in Matrix.cpp
@@ -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