Global Warming Java BFS

You have a piece of sea area N×N

Pixel photos, "." means ocean and "#" means land, as shown below:

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

Among them, a piece of land connected in the four directions of "up, down, left, and right" forms an island. For example, there are two islands in the picture above.

As global warming has caused the sea to rise, scientists predict that in the next few decades, a pixel area on the edge of the island will be submerged by sea water.

Specifically, if a land pixel is adjacent to the ocean (there is an ocean among the four adjacent pixels up, down, left, and right), it will be submerged.

For example, the sea area in the above picture will become as follows in the future:

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

Please calculate: According to the prediction of scientists, how many islands in the photo will be completely submerged.
Input format

The first line contains an integer N.

The following N rows and N columns contain an N×N composed of the characters "#" and "."

The character matrix represents a picture of the sea area, "#" means land, and "." means ocean.

The picture guarantees that the pixels in the first row, the first column, the Nth row, and the Nth column are all oceans.

Output format

An integer represents the answer.

data range

1≤N≤1000

Input example 1:

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

``

Sample output 1:

1

Input sample 2 :

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

Output sample 2 :

1

Idea
1. We first BFS find all the big island count;
2. Determine whether there are one or more small islands that cannot touch the sea on all four sides of this big island. If it exists, the big island will not disappear after drying up for a year, but only It becomes smaller and disappears if it does not exist. (We can also think of it this way, after determining all the points of the big island, 如果存在四面都碰不到海的小岛屿count不变,如果不存在count--,找完全部大岛屿并且判断后直接输出count), the two codes will not change much;
all codes

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--;
        }
    }

The flag and is are a bit duplicated, I won't change it, I'm lazy .

Guess you like

Origin blog.csdn.net/qq_44844588/article/details/107347108