zoukankan      html  css  js  c++  java
  • Java 图的遍历-LeetCode200

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

    Example 1:

    11110
    11010
    11000
    00000

    Answer: 1

    Example 2:

    11000
    11000
    00100
    00011

    Answer: 3

    大意就是给个图,找寻所有的子图。注意两点:1.该二维数组并不一定是方阵;2.数组有可能是空的情况

    代码如下:

     1 public class Solution {
     2     class pos {
     3         public int x;
     4         public int y;
     5 
     6         public pos(int x, int y) {
     7             this.x = x;
     8             this.y = y;
     9         }
    10     }
    11 
    12     public int numIslands(char[][] grid) {
    13         if (grid.length == 0)
    14             return 0;
    15         int count = 0;
    16         int xl = grid.length, yl = grid[0].length;
    17         int[][] visit = new int[xl][yl];
    18         LinkedList<pos> lp = new LinkedList<Solution.pos>();
    19         pos cur;
    20         for (int i = 0; i < xl; i++) {
    21             for (int j = 0; j < yl; j++) {
    22                 if (grid[i][j] == '0')
    23                     continue;
    24                 if (grid[i][j] == '1' && visit[i][j] == 1)
    25                     continue;
    26                 cur = new pos(i, j);
    27                 lp.push(cur);
    28                 while (lp.size() > 0) {
    29                     cur = lp.poll();
    30                     visit[cur.x][cur.y] = 1;
    31                     if (cur.x + 1 < xl && grid[cur.x + 1][cur.y] == '1'
    32                             && visit[cur.x + 1][cur.y] != 1) {
    33                         lp.push(new pos(cur.x + 1, cur.y));
    34                     }
    35                     if (cur.x - 1 >= 0 && grid[cur.x - 1][cur.y] == '1'
    36                             && visit[cur.x - 1][cur.y] != 1) {
    37                         lp.push(new pos(cur.x - 1, cur.y));
    38                     }
    39                     if (cur.y + 1 < yl && grid[cur.x][cur.y + 1] == '1'
    40                             && visit[cur.x][cur.y + 1] != 1) {
    41                         lp.push(new pos(cur.x, cur.y + 1));
    42                     }
    43                     if (cur.y - 1 >= 0 && grid[cur.x][cur.y - 1] == '1'
    44                             && visit[cur.x][cur.y - 1] != 1) {
    45                         lp.push(new pos(cur.x, cur.y - 1));
    46                     }
    47                 }
    48                 ++count;
    49             }
    50         }
    51 
    52         return count;
    53     }
    54 
    55     public static void main(String[] args) {
    56         char[][] grid1 = { { '1', '1', '1', '1', '0' },
    57                 { '1', '1', '0', '1', '0' }, { '1', '1', '0', '0', '0' },
    58                 { '0', '0', '0', '0', '0' } }, grid2 = {
    59                 { '1', '1', '0', '0', '0' }, { '1', '1', '0', '0', '0' },
    60                 { '0', '0', '1', '0', '0' }, { '0', '0', '0', '1', '1' } }, grid3 = {
    61                 { '1', '1', '1' }, { '0', '1', '0' }, { '1', '1', '1' } };
    62         Solution s = new Solution();
    63         System.out.println(s.numIslands(grid1));
    64         System.out.println(s.numIslands(grid2));
    65         System.out.println(s.numIslands(grid3));
    66     }
    67 }

    我利用的是广度优先遍历,也可尝试深度优先遍历

  • 相关阅读:
    FileSystemWatcher用法详解【转】
    关于TransactionScope事务的TransactionScopeOption的实例
    TransactionScope IsolationLevel 事务隔离级别
    C#中TransactionScope的使用方法和原理
    关于Qt 静态成员函数调用信号
    【Qt编程】基于QWT的曲线绘制及图例显示操作——有样点的实现功能
    使用qwt作曲线图——有网格线背景的画法
    Qt程序app添加图标复制到其它电脑后不显示的解决方法
    QUrl的使用,特别是对含特殊字符的字符串进行 URL 格式化编码
    QDateTime 本地时间和UTC时间转换问题
  • 原文地址:https://www.cnblogs.com/xlturing/p/4412932.html
Copyright © 2011-2022 走看看