-
Notifications
You must be signed in to change notification settings - Fork 0
/
LastDayWhereYouCanStillCross_1970.java
83 lines (79 loc) · 2.77 KB
/
LastDayWhereYouCanStillCross_1970.java
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
package org.example;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class LastDayWhereYouCanStillCross_1970 {
class Solution {
public int latestDayToCross(int row, int col, int[][] cells) {
int l=0;
int r=cells.length;
while (l<r){
int n=l+(r-l)/2;
if (checkPossible(n, cells, row, col)){
l=n+1;
// System.out.println(l+" "+r);
}
else {
r=n;
}
}
return l-1;
}
public boolean checkPossible(int day, int[][] cells, int row, int col){
int[][] matrix = new int[row][col];
int[][] visited = new int[row][col];
for (int cur=0;cur<day;cur++){
int[] pos = cells[cur];
matrix[pos[0]-1][pos[1]-1] = 1;
}
List<int[]> queue = new LinkedList<>();
for (int i=0;i<col;i++){
if (matrix[0][i]==0){
queue.add(new int[]{0, i});
visited[0][i]=1;
}
}
while (queue.size()>0){
int[] pos = queue.remove(0);
int curRow = pos[0];
int curCol = pos[1];
if (curRow==row-1){
return true;
}
if (curCol>0){
if (matrix[curRow][curCol-1]==0){
if (visited[curRow][curCol-1]==0){
visited[curRow][curCol-1]=1;
queue.add(new int[]{curRow, curCol-1});
}
}
}
if (curRow<row-1){
if (matrix[curRow+1][curCol]==0){
if (visited[curRow+1][curCol]==0){
visited[curRow+1][curCol]=1;
queue.add(new int[]{curRow+1, curCol});
}
}
}
if (curRow>0){
if (matrix[curRow-1][curCol]==0){
if (visited[curRow-1][curCol]==0){
visited[curRow-1][curCol]=1;
queue.add(new int[]{curRow-1, curCol});
}
}
}
if (curCol<col-1){
if (matrix[curRow][curCol+1]==0){
if (visited[curRow][curCol+1]==0){
visited[curRow][curCol+1]=1;
queue.add(new int[]{curRow, curCol+1});
}
}
}
}
return false;
}
}
}