-
Notifications
You must be signed in to change notification settings - Fork 12
/
Last Day Where you can still cross.cpp
50 lines (50 loc) · 1.31 KB
/
Last Day Where you can still cross.cpp
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
class Solution {
public:
int ROW;
int COL;
vector<vector<int>>directions={{1,0},{0,1},{-1,0},{0,-1}};
bool DFS(vector<vector<int>>&grid,int i,int j){
if(i<0 or i>=ROW or j<0 or j>=COL or grid[i][j]==1){
return false;
}
if(i==ROW-1)return true;
grid[i][j]=1;
for(auto &dir: directions){
int newi= i+dir[0];
int newj= j+dir[1];
if(DFS(grid,newi,newj)){
return true;
}
}
return false;
}
bool canCross( vector<vector<int>>& cells, int mid){
vector<vector<int>>grid(ROW,vector<int>(COL));
for(int i=0;i<=mid;i++){
int x=cells[i][0]-1;
int y=cells[i][1]-1;
grid[x][y]=1;
}
for(int j=0;j<COL;j++){
if(grid[0][j]==0 && DFS(grid,0,j)){
return true;
}
}
return false;
}
int latestDayToCross(int row, int col, vector<vector<int>>& cells) {
ROW=row;
COL=col;
int l=0,r=cells.size()-1;
int lastDay=0;
while(l<=r){
int mid= l+(r-l)/2;
if(canCross(cells,mid)){
lastDay=mid+1;
l=mid+1;
}
else r=mid-1;
}
return lastDay;
}
};