diff --git a/src/main/java/com/dota/gridGraph/_1210/Solution.java b/src/main/java/com/dota/gridGraph/_1210/Solution.java new file mode 100644 index 0000000..d25b4e5 --- /dev/null +++ b/src/main/java/com/dota/gridGraph/_1210/Solution.java @@ -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(); + var cnt = 0; + int n = grid.length; + queue.offer(new snake(0, 1, 0, 0)); + var book = new HashSet(); + 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> 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(); + 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> forest) { + var book = new boolean[n][m]; + var step = 0; + var queue = new ArrayDeque(); + 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> 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> 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); + } +} \ No newline at end of file diff --git a/src/main/java/com/dota/gridGraph/_749/Solution.java b/src/main/java/com/dota/gridGraph/_749/Solution.java new file mode 100644 index 0000000..aab63fd --- /dev/null +++ b/src/main/java/com/dota/gridGraph/_749/Solution.java @@ -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(); + 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 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) { + } +} \ No newline at end of file