kk
This commit is contained in:
39
src/main/java/com/dota/gridGraph/_1091/Solution.java
Normal file
39
src/main/java/com/dota/gridGraph/_1091/Solution.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package com.dota.gridGraph._1091;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
int[] next = {-1, 0, 1};
|
||||||
|
|
||||||
|
|
||||||
|
public int shortestPathBinaryMatrix(int[][] grid) {
|
||||||
|
int n = grid.length;
|
||||||
|
if (grid[0][0] == 1 || grid[n - 1][n - 1] == 1) return -1;
|
||||||
|
return bfs(grid);
|
||||||
|
}
|
||||||
|
|
||||||
|
int bfs(int[][] grid) {
|
||||||
|
var book = new boolean[grid.length][grid.length];
|
||||||
|
var queue = new ArrayDeque<int[]>();
|
||||||
|
queue.add(new int[]{0, 0});
|
||||||
|
int cnt = 0;
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
int size = queue.size();
|
||||||
|
cnt++;
|
||||||
|
for (int ii = 0; ii < size; ii++) {
|
||||||
|
var n = queue.poll();
|
||||||
|
if (n[0] < 0 || n[1] < 0 || n[0] >= grid.length || n[1] >= grid[0].length || book[n[0]][n[1]] || grid[n[0]][n[1]] == 1)
|
||||||
|
continue;
|
||||||
|
if (n[0] == n[1] && n[0] == grid.length - 1) return cnt;
|
||||||
|
book[n[0]][n[1]] = true;
|
||||||
|
for (int i : next) {
|
||||||
|
for (int i1 : next) {
|
||||||
|
queue.offer(new int[]{n[0] + i, n[1] + i1});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
37
src/main/java/com/dota/gridGraph/_1926/Solution.java
Normal file
37
src/main/java/com/dota/gridGraph/_1926/Solution.java
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package com.dota.gridGraph._1926;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
|
||||||
|
public int nearestExit(char[][] maze, int[] entrance) {
|
||||||
|
int x = entrance[0];
|
||||||
|
int y = entrance[1];
|
||||||
|
var queue = new ArrayDeque<int[]>();
|
||||||
|
queue.add(new int[]{x+1, y});
|
||||||
|
queue.add(new int[]{x-1, y});
|
||||||
|
queue.add(new int[]{x, y+1});
|
||||||
|
queue.add(new int[]{x, y-1});
|
||||||
|
maze[x][y] = '+';
|
||||||
|
int cnt = 0;
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
cnt++;
|
||||||
|
int t =queue.size();
|
||||||
|
for (int i = 0; i < t; i++) {
|
||||||
|
var n = queue.poll();
|
||||||
|
if (n[0] < 0 || n[1] < 0 || n[0] >= maze.length || n[1] >= maze[0].length || maze[n[0]][n[1]] == '+') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ((n[0]==0||n[1]==0||n[0]==maze.length-1||n[1]==maze[0].length-1)) {
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
|
maze[n[0]][n[1]] = '+';
|
||||||
|
queue.offer(new int[]{n[0] + 1, n[1]});
|
||||||
|
queue.offer(new int[]{n[0] - 1, n[1]});
|
||||||
|
queue.offer(new int[]{n[0], n[1] + 1});
|
||||||
|
queue.offer(new int[]{n[0], n[1] - 1});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
77
src/main/java/com/dota/gridGraph/_lcp63/Solution.java
Normal file
77
src/main/java/com/dota/gridGraph/_lcp63/Solution.java
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
package com.dota.gridGraph._lcp63;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
|
||||||
|
public int[][] ballGame(int num, String[] plate) {
|
||||||
|
var res = new ArrayList<int[]>();
|
||||||
|
int n = plate.length;
|
||||||
|
int m = plate[0].length();
|
||||||
|
// 方向 上下左右 1,2,3,4
|
||||||
|
for (int i = 1; i < n - 1; i++) {
|
||||||
|
if (dfs(num, i, 0, plate, 4) && plate[i].charAt(0)=='.') {
|
||||||
|
res.add(new int[]{i, 0});
|
||||||
|
}
|
||||||
|
if (dfs(num, i, m - 1, plate, 3)&&plate[i].charAt(m-1)=='.') {
|
||||||
|
res.add(new int[]{i, m - 1});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 1; i < m - 1; i++) {
|
||||||
|
if (dfs(num, 0, i, plate, 2)&&plate[0].charAt(i)=='.') {
|
||||||
|
res.add(new int[]{0, i});
|
||||||
|
}
|
||||||
|
if (dfs(num, n - 1, i, plate, 1)&&plate[n-1].charAt(i)=='.') {
|
||||||
|
res.add(new int[]{n - 1, i});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var arr = new int[res.size()][];
|
||||||
|
for (int i = 0; i < res.size(); i++) {
|
||||||
|
arr[i] = res.get(i);
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean dfs(int num, int i, int j, String[] plate, int to) {
|
||||||
|
if (i < 0 || j < 0 || i >= plate.length || j >= plate[0].length() || num < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return switch (plate[i].charAt(j)) {
|
||||||
|
case 'W' -> {
|
||||||
|
if (to == 1) {
|
||||||
|
yield dfs(num - 1, i, j - 1, plate, 3);
|
||||||
|
} else if (to == 2) {
|
||||||
|
yield dfs(num - 1, i, j + 1, plate, 4);
|
||||||
|
} else if (to == 3) {
|
||||||
|
yield dfs(num - 1, i + 1, j, plate, 2);
|
||||||
|
} else {
|
||||||
|
yield dfs(num - 1, i - 1, j, plate, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 'E' -> {
|
||||||
|
if (to == 1) {
|
||||||
|
yield dfs(num - 1, i, j + 1, plate, 4);
|
||||||
|
} else if (to == 2) {
|
||||||
|
yield dfs(num - 1, i, j - 1, plate, 3);
|
||||||
|
} else if (to == 3) {
|
||||||
|
yield dfs(num - 1, i - 1, j, plate, 1);
|
||||||
|
} else {
|
||||||
|
yield dfs(num - 1, i + 1, j, plate, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case '.' -> {
|
||||||
|
if (to == 1) {
|
||||||
|
yield dfs(num - 1, i - 1, j, plate, to);
|
||||||
|
} else if (to == 2) {
|
||||||
|
yield dfs(num - 1, i + 1, j, plate, to);
|
||||||
|
} else if (to == 3) {
|
||||||
|
yield dfs(num - 1, i, j - 1, plate, to);
|
||||||
|
} else {
|
||||||
|
yield dfs(num - 1, i, j + 1, plate, to);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default -> true;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user