LeetCode-岛屿数量(Queue+BFS)

基于JAVA代码编译,数据结构Queue+BFS的实现逻辑

public class Island {
    //Queue+BFS的实现方式,得到总的岛屿的数量
    public static int islands(char[][] point) {
        if (point == null || point.length == 0 || point[0].length == 0){
            return 0;
        }
        int row = point.length;// 行
        int column = point[0].length;// 列
        int count = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (point[i][j] == '1') {
                    count++;
                    search(point, i, j);
                }
            }
        }
        return count;
    }

    public  static  void search(char[][] point,int x,int y){
        Queue<TwoTuple> q = new LinkedList();
        point[x][y]=2;
        q.offer(new TwoTuple(x,y));

        while (q.size()!=0){
            final TwoTuple poll = q.poll();
            final Integer first = poll.first;
            final Integer second = poll.second;
            point[first][second]=2;

            if (first > point.length - 1 && second > point[0].length - 1) {
                continue;
            }
            if (first < point.length - 1 && point[first + 1][second] == '1') {// 向右

                q.offer(new TwoTuple(first+1,second));
            }
            if (y < point[0].length - 1 && point[x][y + 1] == '1') {// 向下

                q.offer(new TwoTuple(first,second+1));
            }
            if (x > 0 && point[x - 1][y] == '1') {// 向左

                q.offer(new TwoTuple(first-1,second));
            }
            if (y > 0 && point[x][y - 1] == '1') {// 向上

                q.offer(new TwoTuple(first,second-1));
            }
        }
    }

    public static void main(String[] args){
        char[][] point = new char[10][10];
        int num = 0;
        for(int i = 0; i < point.length; i ++){
            for(int j = 0; j < point[0].length; j ++){
                num = (int)(Math.random()*100);;
                if(num >= 80){
                    point[i][j] = '1';
                }else{
                    point[i][j] = '0';
                }
            }
        }
        System.out.println("---------------------------");

        for(int i = 0; i < point.length; i ++){
            for(int j = 0; j < point[0].length; j ++){
                System.out.print(point[i][j]);
            }
            System.out.println();
        }
        System.out.println("----------------------------");
        System.out.println("岛屿数:" + islands(point));
    }

}

class TwoTuple {

    public final Integer first;

    public final Integer second;

    public TwoTuple(Integer a, Integer b){
        first = a;
        second = b;
    }

    public String toString(){
        return "(" + first + ", " + second + ")";
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值