• ​力扣解法汇总934. 最短的桥


     目录链接:

    力扣编程题-解法汇总_分享+记录-CSDN博客

    GitHub同步刷题项目:

    https://github.com/September26/java-algorithms

    原题链接:力扣


    描述:

    给你一个大小为 n x n 的二元矩阵 grid ,其中 1 表示陆地,0 表示水域。

     是由四面相连的 1 形成的一个最大组,即不会与非组内的任何其他 1 相连。grid 中 恰好存在两座岛 。

    你可以将任意数量的 0 变为 1 ,以使两座岛连接起来,变成 一座岛 。

    返回必须翻转的 0 的最小数目。

    示例 1:

    输入:grid = [[0,1],[1,0]]
    输出:1
    

    示例 2:

    输入:grid = [[0,1,0],[0,0,0],[0,0,1]]
    输出:2
    

    示例 3:

    输入:grid = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
    输出:1
    

    提示:

    • n == grid.length == grid[i].length
    • 2 <= n <= 100
    • grid[i][j] 为 0 或 1
    • grid 中恰有两个岛

    解题思路:

    * 解题思路:
    * 由题目描述可知,grid中只存在三种类型的点,归属陆地A的点,归属陆地B的点,归属水域的点。
    * 那么针对于归属水域的这些点,我们就可以依次的求出每个点距离陆地A最近的点。
    * 分别用setA和setB代表归属陆地A和陆地B的点,针对水域,我们直接修改其值为距离setA陆地最近距离即可。
    * 我们先求出setA和setB的集合,然后针对setA,分别求出1步,2步,3步..的范围,直到没有下一级的范围。
    * 最后,我们只要遍历B中所有的点,找到一步可达的水域中最小值即可。
    

    代码:

    1. public class Solution934 {
    2. int[][] intss = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
    3. public int shortestBridge(int[][] grid) {
    4. Set<String> setA = new HashSet<>();
    5. Set<String> setB = new HashSet<>();
    6. outer:
    7. for (int i = 0; i < grid.length; i++) {
    8. for (int j = 0; j < grid[0].length; j++) {
    9. if (grid[i][j] == 0) {
    10. continue;
    11. }
    12. if (setA.size() == 0) {
    13. createSet(grid, setA, i, j);
    14. continue;
    15. }
    16. if (setA.contains(i + "_" + j)) {
    17. continue;
    18. }
    19. createSet(grid, setB, i, j);
    20. break outer;
    21. }
    22. }
    23. //
    24. searchStep(grid, setA, 0);
    25. int minValue = Integer.MAX_VALUE;
    26. int length = grid.length;
    27. for (String location : setB) {
    28. String[] s = location.split("_");
    29. int y = Integer.parseInt(s[0]);
    30. int x = Integer.parseInt(s[1]);
    31. int current = Integer.MAX_VALUE;
    32. for (int[] ints : intss) {
    33. int diffY = ints[0];
    34. int diffX = ints[1];
    35. int newY = y + diffY;
    36. int newX = x + diffX;
    37. if (newX < 0 || newY < 0 || newX > (length - 1) || newY > length - 1) {
    38. continue;
    39. }
    40. if (setB.contains(newY + "_" + newX)) {
    41. continue;
    42. }
    43. int step = grid[newY][newX];
    44. if (step == 0) {
    45. continue;
    46. }
    47. current = Math.min(current, step);
    48. }
    49. minValue = Math.min(current, minValue);
    50. }
    51. return minValue;
    52. }
    53. private void createSet(int[][] grid, Set<String> set, int i, int j) {
    54. if (i < 0 || i >= grid.length || j < 0 || j >= grid.length) {
    55. return;
    56. }
    57. int value = grid[i][j];
    58. String location = i + "_" + j;
    59. if (value == 0 || set.contains(location)) {
    60. return;
    61. }
    62. set.add(location);
    63. for (int[] ints : intss) {
    64. createSet(grid, set, i + ints[0], j + ints[1]);
    65. }
    66. }
    67. private void searchStep(int[][] grid, Set<String> set, int step) {
    68. step++;
    69. int length = grid.length;
    70. Set<String> newSet = new HashSet<>();
    71. for (String location : set) {
    72. String[] s = location.split("_");
    73. int y = Integer.parseInt(s[0]);
    74. int x = Integer.parseInt(s[1]);
    75. for (int[] ints : intss) {
    76. int newY = y + ints[0];
    77. int newX = x + ints[1];
    78. if (newY < 0 || newY > length - 1 || newX < 0 || newX > length - 1) {
    79. continue;
    80. }
    81. if (grid[newY][newX] != 0) {
    82. continue;
    83. }
    84. grid[newY][newX] = step;
    85. newSet.add(newY + "_" + newX);
    86. }
    87. }
    88. if (newSet.size() == 0) {
    89. return;
    90. }
    91. searchStep(grid, newSet, step);
    92. }
    93. }

  • 相关阅读:
    D. Problem with Random Tests(暴力&贪心)
    软件工程国考总结——判断题
    我的创业笔记:困境与思索
    解析‘找不到msvcp140.dll无法继续执行代码’这个问题的解决方法
    20221125使用PR2023自动识别obs-studio录屏生成的MKV视频的字幕
    WebSocket基本使用方法
    草稿纸-Java项目技术历史
    30+的女生学习做软件测试有前景吗?
    如何快速同步第三方平台数据?
    AquilaChat2-34B 主观评测接近GPT3.5水平,最新版本Base和Chat权重已开源!
  • 原文地址:https://blog.csdn.net/AA5279AA/article/details/127510524