kk
This commit is contained in:
52
src/main/java/com/dota/gridGraph/_1210/Solution.java
Normal file
52
src/main/java/com/dota/gridGraph/_1210/Solution.java
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package com.dota.gridGraph._1210;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
record snake(int headX, int headY, int tailX, int tailY) {
|
||||||
|
}
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public int minimumMoves(int[][] grid) {
|
||||||
|
var queue = new ArrayDeque<snake>();
|
||||||
|
var cnt = 0;
|
||||||
|
int n = grid.length;
|
||||||
|
queue.offer(new snake(0, 1, 0, 0));
|
||||||
|
var book = new HashSet<snake>();
|
||||||
|
book.add(new snake(0, 1, 0, 0));
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
int size = queue.size();
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
var s = queue.poll();
|
||||||
|
if (book.contains(s)) continue;
|
||||||
|
book.add(s);
|
||||||
|
if (s.headX() == n - 1 && s.headY() == n - 1 && s.tailX() == n - 1 && s.tailY() == n - 2) return cnt;
|
||||||
|
//蛇水平情况
|
||||||
|
//1:往右移动
|
||||||
|
//2:往下顺时针旋转(下面两个单元格是空的)
|
||||||
|
if (s.headX() == s.tailX()) {
|
||||||
|
if (s.headY()+1<n && grid[s.headX()][s.headY()+1]==0) {
|
||||||
|
queue.offer(new snake(s.headX(), s.headY()+1, s.tailX(), s.tailY()+1));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s.headX()+1<n && grid[s.headX()+1][s.headY()]==0 && grid[s.tailX()+1][s.tailY()]==0) {
|
||||||
|
queue.offer(new snake(s.tailX()+1, s.tailY(), s.tailX(), s.tailY()));
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//垂直情况
|
||||||
|
//1:往下移动
|
||||||
|
//2:往右逆时针旋转(右边两个是空的)
|
||||||
|
if (s.headX()+1<n&&grid[s.headX()+1][s.headY()]==0) {
|
||||||
|
queue.offer(new snake(s.headX()+1, s.headY(), s.headX(), s.headY()));
|
||||||
|
}
|
||||||
|
if (s.headY()+1<n && grid[s.headX()][s.headY()+1]==0 && grid[s.tailX()][s.tailY()+1]==0) {
|
||||||
|
queue.offer(new snake(s.headX()-1, s.headY()+1,s.tailX(),s.tailY()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cnt++;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
90
src/main/java/com/dota/gridGraph/_675/Solution.java
Normal file
90
src/main/java/com/dota/gridGraph/_675/Solution.java
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package com.dota.gridGraph._675;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new Solution().cutOffTree(List.of(List.of(54581641, 64080174, 24346381, 69107959), List.of(86374198, 61363882, 68783324, 79706116),
|
||||||
|
List.of(668150, 92178815, 89819108, 94701471), List.of(83920491, 22724204, 46281641, 47531096), List.of(89078499, 18904913, 25462145, 60813308)));
|
||||||
|
}
|
||||||
|
|
||||||
|
int[] next = new int[]{1, -1};
|
||||||
|
int n, m;
|
||||||
|
|
||||||
|
public int cutOffTree(List<List<Integer>> forest) {
|
||||||
|
if (forest.get(0).get(0) == 0) return -1;
|
||||||
|
n = forest.size();
|
||||||
|
m = forest.get(0).size();
|
||||||
|
if (!check(forest)) return -1;
|
||||||
|
// 步数
|
||||||
|
int step = 0;
|
||||||
|
var treeList = new ArrayList<Node>();
|
||||||
|
for (int i = 0; i < forest.size(); i++) {
|
||||||
|
for (int j = 0; j < forest.get(i).size(); j++) {
|
||||||
|
int t = forest.get(i).get(j);
|
||||||
|
if (t > 1) {
|
||||||
|
treeList.add(new Node(i, j, forest.get(i).get(j)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
treeList.sort(Comparator.comparingInt(a -> a.v));
|
||||||
|
var last = new int[]{0,0};
|
||||||
|
for (Node node : treeList) {
|
||||||
|
step += bfs(last[0], last[1], node.i,node.j, forest);
|
||||||
|
last = new int[]{node.i, node.j};
|
||||||
|
}
|
||||||
|
return step;
|
||||||
|
}
|
||||||
|
|
||||||
|
int bfs(int i, int j, int endI, int endJ, List<List<Integer>> forest) {
|
||||||
|
var book = new boolean[n][m];
|
||||||
|
var step = 0;
|
||||||
|
var queue = new ArrayDeque<int[]>();
|
||||||
|
queue.offer(new int[]{i, j});
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
int size = queue.size();
|
||||||
|
for (int k = 0; k < size; k++) {
|
||||||
|
var c = queue.poll();
|
||||||
|
if (c[0] == endI && c[1] == endJ) return step;
|
||||||
|
if (c[0] < 0 || c[1] < 0 || c[0] >= n || c[1] >= m || book[c[0]][c[1]] || forest.get(c[0]).get(c[1])==0 ) continue;
|
||||||
|
book[c[0]][c[1]] = true;
|
||||||
|
queue.offer(new int[]{c[0] + 1, c[1]});
|
||||||
|
queue.offer(new int[]{c[0] - 1, c[1]});
|
||||||
|
queue.offer(new int[]{c[0], c[1] + 1});
|
||||||
|
queue.offer(new int[]{c[0], c[1] - 1});
|
||||||
|
}
|
||||||
|
|
||||||
|
step++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
record Node(int i, int j, int v) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
boolean check(List<List<Integer>> forest) {
|
||||||
|
int cnt = 0;
|
||||||
|
int x = 0;
|
||||||
|
int y = 0;
|
||||||
|
for (int i = 0; i < forest.size(); i++) {
|
||||||
|
for (int j = 0; j < forest.get(i).size(); j++) {
|
||||||
|
if (forest.get(i).get(j) > 0) {
|
||||||
|
cnt++;
|
||||||
|
x = i;
|
||||||
|
y = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var book = new boolean[forest.size()][forest.get(0).size()];
|
||||||
|
return cnt == df(forest, x, y, book);
|
||||||
|
}
|
||||||
|
|
||||||
|
int df(List<List<Integer>> forest, int x, int y, boolean[][] book) {
|
||||||
|
if (x < 0 || y < 0 || x >= forest.size() || y >= forest.get(0).size() || book[x][y] || forest.get(x).get(y) == 0)
|
||||||
|
return 0;
|
||||||
|
book[x][y] = true;
|
||||||
|
return 1 + df(forest, x + 1, y, book) + df(forest, x, y + 1, book) + df(forest, x - 1, y, book) + df(forest, x, y - 1, book);
|
||||||
|
}
|
||||||
|
}
|
89
src/main/java/com/dota/gridGraph/_749/Solution.java
Normal file
89
src/main/java/com/dota/gridGraph/_749/Solution.java
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
package com.dota.gridGraph._749;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public int containVirus(int[][] isInfected) {
|
||||||
|
var res = 0;
|
||||||
|
while(true) {
|
||||||
|
int max = 0;
|
||||||
|
Node node = null;
|
||||||
|
for (int i = 0; i < isInfected.length; i++) {
|
||||||
|
for (int j = 0; j < isInfected[i].length; j++) {
|
||||||
|
if (isInfected[i][j] == 1) {
|
||||||
|
var set = new HashSet<Node>();
|
||||||
|
df(isInfected, i, j, set);
|
||||||
|
if (set.size() > max) {
|
||||||
|
max = set.size();
|
||||||
|
node = new Node(i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node == null) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
res += quarantine(isInfected, node.x, node.y);
|
||||||
|
|
||||||
|
// 病毒扩展
|
||||||
|
for (int i = 0; i < isInfected.length; i++) {
|
||||||
|
for (int j = 0; j < isInfected[i].length; j++) {
|
||||||
|
if (isInfected[i][j] == 2) {
|
||||||
|
growth(isInfected, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void growth(int[][] isInfected, int x, int y) {
|
||||||
|
if (x<0|| x>=isInfected.length || y<0|| y>=isInfected[0].length||isInfected[x][y]==1 || isInfected[x][y]==3) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (isInfected[x][y]==0) {
|
||||||
|
isInfected[x][y] = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
isInfected[x][y] = 1;
|
||||||
|
growth(isInfected, x, y-1);
|
||||||
|
growth(isInfected, x, y+1);
|
||||||
|
growth(isInfected, x-1, y);
|
||||||
|
growth(isInfected, x+1, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 隔离病毒
|
||||||
|
int quarantine(int[][] isInfected, int row, int col) {
|
||||||
|
if (row < 0 || row >= isInfected.length || col < 0 || col >= isInfected[row].length || isInfected[row][col] == 3) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (isInfected[row][col] == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
isInfected[row][col] = 3;
|
||||||
|
return quarantine(isInfected, row, col + 1) + quarantine(isInfected, row, col - 1) + quarantine(isInfected, row + 1, col) + quarantine(isInfected, row - 1, col);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 遍历病毒
|
||||||
|
void df(int[][] isInfected, int row, int col, Set<Node> set) {
|
||||||
|
if (row < 0 || row >= isInfected.length || col < 0 || col >= isInfected[row].length || isInfected[row][col] == 2 || isInfected[row][col] == 3) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isInfected[row][col] == 0) {
|
||||||
|
set.add(new Node(row, col));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
isInfected[row][col] = 2;
|
||||||
|
df(isInfected, row - 1, col, set);
|
||||||
|
df(isInfected, row + 1, col, set);
|
||||||
|
df(isInfected, row, col - 1, set);
|
||||||
|
df(isInfected, row, col + 1, set);
|
||||||
|
}
|
||||||
|
|
||||||
|
record Node(int x, int y) {
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user