Skip to content

Commit

Permalink
Create ExitPointInMatrix.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitiz11101 authored Apr 26, 2024
1 parent 38989ec commit 131cb61
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Arrays/ExitPointInMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution {
public:
vector<int> FindExitPoint(int n, int m, vector<vector<int>>& matrix) {
// Code here
// vector<int>ans;
int i=0,j=0,dir=1;
while(i>=0 && i<n && j>=0 && j<m){
if(matrix[i][j]==1){
matrix[i][j]=0;
dir=(dir%2)+1;
}
if(dir==1){
j++;
}
else if(dir==2){
i++;
}
else if(dir==3){
j--;
}
else{
i--;
}
}
if(dir==1){
j--;
}
else if(dir==2){
i--;
}
else if(dir==3){
j++;
}
else{
i++;
}
return {i,j};

}
};

0 comments on commit 131cb61

Please sign in to comment.