全球变暖 Java BFS

你有一张某海域 N×N

像素的照片,”.”表示海洋、”#”表示陆地,如下所示:

.......
.##....
.##....
....##.
..####.
...###.
.......

其中”上下左右”四个方向上连在一起的一片陆地组成一座岛屿,例如上图就有 2座岛屿。

由于全球变暖导致了海面上升,科学家预测未来几十年,岛屿边缘一个像素的范围会被海水淹没。

具体来说如果一块陆地像素与海洋相邻(上下左右四个相邻像素中有海洋),它就会被淹没。

例如上图中的海域未来会变成如下样子:

.......
.......
.......
.......
....#..
.......
.......

请你计算:依照科学家的预测,照片中有多少岛屿会被完全淹没。
输入格式

第一行包含一个整数N。

以下 N行 N 列,包含一个由字符”#”和”.”构成的 N×N

字符矩阵,代表一张海域照片,”#”表示陆地,”.”表示海洋。

照片保证第 1行、第 1 列、第 N 行、第 N列的像素都是海洋。

输出格式

一个整数表示答案。

数据范围

1≤N≤1000

输入样例1:

7
.......
.##....
.##....
....##.
..####.
...###.
.......

``

输出样例1:

1

输入样例2

9
.........
.##.##...
.#####...
.##.##...
.........
.##.#....
.#.###...
.#..#....
.........

输出样例2

1

思路
1.我们先BFS找到所有大岛屿count;
2.判断这个大岛屿是否存在一个或多个四面都碰不到海的小岛屿,如果存在则弄干年后这个大岛屿不会消失,只会变小,如果不存在就会消失。(我们也可以这么想,在确定大岛屿所有的点之后,如果存在四面都碰不到海的小岛屿count不变,如果不存在count--,找完全部大岛屿并且判断后直接输出count),这两者代码不会有太大变化;
全部代码

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
     static int count=0;
     static int index=2;
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int N=input.nextInt();
        String arr[]=new String[N+1];
        char arrmap[][]=new char[N+1][1000];
        int tempmap[][]=new int[N+1][10000];
        int temp[][]=new int[N+1][10000];
        for(int i=0;i<N;i++){
            arr[i]=input.next();
            arrmap[i]=arr[i].toCharArray();
            for(int j=0;j<arrmap[i].length;j++){
                //找陆地,标记海域
                if(arrmap[i][j]=='.'){
                    tempmap[i][j]=1;
                }
            }

        }

        int length=arrmap[1].length;
        for(int i=0;i<N;i++){
            for(int j=0;j<length;j++){
            //没有访问过且是海岛的点BFS,找包含该点的最大岛屿
                if(tempmap[i][j]==0&&arrmap[i][j]=='#') {
                    bfs(i,j,tempmap,arrmap,N,length);
                }
            }
        }

        System.out.println(count);

    }
    static int xx[]={0,-1,1,0,0};
    static int yy[]={0,0,0,-1,1};
    public static void bfs(int x,int y,int tempmap[][],char arrmap[][],int N,int length) {
        Queue<Integer> xxx = new LinkedList<Integer>();
        Queue<Integer> yyy = new LinkedList<Integer>();
        xxx.offer(x);
        yyy.offer(y);
        tempmap[x][y] = 1;
        
        int is=0;  //判断是否找到那个符合条件的点找到变为1,如果为1接下来的点就不需要
        		  //判断减少时间
        		  
        while (!xxx.isEmpty() && !yyy.isEmpty()) {
            int tempx = xxx.poll();
            int tempy = yyy.poll();
            for (int i = 1; i <= 4; i++) {
                int tempxx = tempx + xx[i];
                int tempyy = tempy + yy[i];
                if (tempxx < 0 || tempxx >= N) continue;
                if (tempyy < 0 || tempyy >= length) continue;
                if (tempmap[tempxx][tempyy] != 0) continue;
                if (arrmap[tempxx][tempyy] != '#') continue;
                xxx.offer(tempxx);
                yyy.offer(tempyy);
                tempmap[tempxx][tempyy] = 1;
                //看这个点是否没被海洋包围,也可以理解为找整个岛屿中
                //有哪些小岛屿四面不环海
                if (is == 0) {  //等于零还没找到
                    int flag = 0;//表示没找到,flag和is都是表示是否找到
                    for (int k = 1; k <= 4; k++) {
                        if (arrmap[tempxx + xx[k]][tempyy + yy[k]] != '.')
                            continue;
                        else { 
                            //如果有一个点满足条件,退出循环
                            flag = 1;
                            break;

                        }
                    }
                    if (flag == 0) {
                        is = 1;
                    }
               }
            }

        }	
		//找完包含坐标为x,y 的小岛屿的大岛屿后,大岛屿总数加1
		//再判断是否存在四面不环海的小岛屿
        count++;
        if(is==1)
            count--;
        }
    }

flag和is有点重复的感jio,我就不改了,我懒

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值