-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1091. Shortest Path in Binary Matrix.cpp
More file actions
143 lines (97 loc) · 3.51 KB
/
1091. Shortest Path in Binary Matrix.cpp
File metadata and controls
143 lines (97 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include<vector>
#include<stack>
#include<unordered_set>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
struct pair_hash {
inline size_t operator()(const pair<int,int> & v) const {
return v.first*31+v.second;
}
};
class Solution {
public:
void breathFirstSearch(vector<vector<int>>& grid){
pair<int,int> node = {0,0};
alreadyVisited.insert(node);
queue<pair<pair<int,int>,int>> q;
q.push({node,1});
while(q.size()>0){
int x = q.front().first.first;
int y = q.front().first.second;
int distance = q.front().second;
if( q.front().first == finalNode){
pathLength = distance;
break;
}
q.pop();
vector<pair<int,int>> validPaths = getValidPathsForNode({x,y},grid);
for(int i=0;i<validPaths.size();i++){
if(alreadyVisited.find(validPaths[i]) == alreadyVisited.end()){
alreadyVisited.insert(validPaths[i]);
q.push({validPaths[i],distance+1});
}
}
}
}
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
if(grid[0][0] == 1 || grid[grid.size()-1][grid.size()-1] == 1){
return -1;
}
finalNode = {grid.size()-1,grid.size()-1};
graph = grid;
breathFirstSearch(grid);
return pathLength;
}
vector<pair<int,int>> getValidPathsForNode(pair<int,int> node,vector<vector<int>>& graph){
vector<pair<int,int>> paths;
int x = node.first;
int y = node.second;
// right
if(y+1<=finalNode.second && graph[x][y+1] == 0){
paths.push_back({x,y+1});
}
// left
if(y-1>=0 && graph[x][y-1] == 0){
paths.push_back({x,y-1});
}
// bot
if(x+1 <= finalNode.first && graph[x+1][y] == 0){
paths.push_back({x+1,y});
}
// top
if(x-1 >= 0 && graph[x-1][y] == 0){
paths.push_back({x-1,y});
}
// diagonal bottom right
if(x+1 <= finalNode.first && y+1 <= finalNode.second && graph[x+1][y+1] == 0){
paths.push_back({x+1,y+1});
}
// diagonal bottom left
if(x+1 <= finalNode.first && y-1 >= 0 && graph[x+1][y-1] == 0){
paths.push_back({x+1,y-1});
}
// diagonal top left
if(x-1 >= 0 && y-1 >= 0 && graph[x-1][y-1] == 0){
paths.push_back({x-1,y-1});
}
// diagonal top right
if(x-1 >= 0 && y+1 <= finalNode.second && graph[x-1][y+1] == 0){
paths.push_back({x-1,y+1});
}
return paths;
}
private:
unordered_set<pair<int, int>,pair_hash> alreadyVisited;
vector<vector<int>> graph;
pair<int,int> finalNode;
int pathLength = -1;
};
int main(){
Solution solution;
vector<vector<int>> input = {{0,1,1,0,0,0},{0,1,0,1,1,0},{0,1,1,0,1,0},{0,0,0,1,1,0},{1,1,1,1,1,0},{1,1,1,1,1,0}};
int answer = solution.shortestPathBinaryMatrix(input);
cout << "Answer is: " << answer << endl;
return 0;
}