| 12
 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
 144
 145
 146
 147
 
 | import java.util.ArrayList;import java.util.List;
 
 public class Solution {
 private static final int[][] DIRS = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
 
 
 
 
 
 
 
 
 
 
 private List<int[]> spreadFire(int[][] grid, boolean[][] fire, List<int[]> f) {
 int m = grid.length;
 int n = grid[0].length;
 List<int[]> tmp = f;
 f = new ArrayList<>();
 for (int[] p : tmp) {
 for (int[] d : DIRS) {
 int x = p[0] + d[0];
 int y = p[1] + d[1];
 if (0 <= x && x < m && 0 <= y && y < n && !fire[x][y] && grid[x][y] == 0) {
 fire[x][y] = true;
 f.add(new int[] { x, y });
 }
 }
 }
 return f;
 }
 
 
 
 
 private boolean check(int[][] grid, int t) {
 int m = grid.length;
 int n = grid[0].length;
 boolean[][] onFire = new boolean[m][n];
 List<int[]> f = new ArrayList<>();
 for (int i = 0; i < m; i++) {
 for (int j = 0; j < n; j++) {
 if (grid[i][j] == 1) {
 onFire[i][j] = true;
 f.add(new int[] { i, j });
 }
 }
 }
 
 
 while (t-- > 0 && !f.isEmpty()) {
 f = spreadFire(grid, onFire, f);
 }
 
 if (onFire[0][0]) {
 return false;
 }
 
 
 boolean[][] vis = new boolean[m][n];
 vis[0][0] = true;
 List<int[]> nextCircle = List.of(new int[] { 0, 0 });
 while (!nextCircle.isEmpty()) {
 List<int[]> curCircle = nextCircle;
 nextCircle = new ArrayList<>();
 for (int[] p : curCircle) {
 if (onFire[p[0]][p[1]]) {
 
 
 continue;
 
 }
 for (int[] d : DIRS) {
 int x = p[0] + d[0];
 int y = p[1] + d[1];
 
 if (0 <= x && x < m && 0 <= y && y < n && !onFire[x][y] && !vis[x][y] && grid[x][y] == 0) {
 if (x == m - 1 && y == n - 1) {
 return true;
 }
 vis[x][y] = true;
 nextCircle.add(new int[] { x, y });
 }
 }
 }
 f = spreadFire(grid, onFire, f);
 }
 return false;
 }
 
 public int maximumMinutes(int[][] grid) {
 int m = grid.length;
 int n = grid[0].length;
 int left = -1;
 int right = m * n + 1;
 while (left + 1 < right) {
 int mid = (left + right) >>> 1;
 if (check(grid, mid)) {
 left = mid;
 } else {
 right = mid;
 }
 }
 return left < m * n ? left : 1_000_000_000;
 }
 
 public static void main(String[] args) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Solution solu = new Solution();
 int[][] grid = new int[][] { { 0, 2, 0, 0, 0, 0, 0 }, { 0, 0, 0, 2, 2, 1, 0 }, { 0, 2, 0, 0, 1, 2, 0 },
 { 0, 0, 2, 2, 2, 0, 2 }, { 0, 0, 0, 0, 0, 0, 0 } };
 int ans = solu.maximumMinutes(grid);
 System.out.println(ans);
 
 grid = new int[][] { { 0, 0, 0, 0 }, { 0, 1, 2, 0 }, { 0, 2, 0, 0 } };
 ans = solu.maximumMinutes(grid);
 System.out.println(ans);
 
 grid = new int[][] { { 0, 0, 0 }, { 2, 2, 0 }, { 1, 2, 0 } };
 ans = solu.maximumMinutes(grid);
 System.out.println(ans);
 }
 }
 
 |