地球温暖化Java BFS

海域N×Nがあります

以下に示すように、ピクセルの写真、「。」は海を意味し、「#」は陸を意味します。

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

そのなかで「上下左右」の4方向につながった土地が島を形成しています。

地球温暖化により海が隆起したため、科学者は今後数十年で島の端にあるピクセル領域が海水に沈むと予測しています。

具体的には、陸上のピクセルが海に隣接している場合(上下左右に隣接する4つのピクセルの中に海がある)、水没します。

たとえば、上の写真の海域は将来的に次のようになります。

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

計算してください:科学者の予測によると、写真にある島の数は完全に水没します。
入力フォーマット

最初の行には整数Nが含まれています。

次のN行とN列には、文字「#」と「。」で構成されるN×Nが含まれています。

文字マトリックスは海域の絵を表し、「#」は陸、「。」は海を表します。

この図は、最初の行、最初の列、N番目の行、およびN番目の列のピクセルがすべて海であることを保証しています。

出力フォーマット

整数は答えを表します。

データ範囲

1≤N≤1000

入力例1:

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

「」

出力例1:

1

入力サンプル2

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

出力サンプル2

1

アイデア
1.最初にBFSがすべての大きな島の数を見つけます;
2.この大きな島の4辺すべてに海に触れることができない1つ以上の小さな島があるかどうかを判断します。存在する場合、大きな島は1年間乾燥させても消えませんが、存在しなければ小さくなり消えます。(ビッグアイランドのすべてのポイントを決定した後、このように考えることもできます如果存在四面都碰不到海的小岛屿count不变,如果不存在count--,找完全部大岛屿并且判断后直接输出count)、2つのコードはあまり変化しません。
すべてのコード

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

フラグとは少し重複していますが、変更しません怠惰です。

おすすめ

転載: blog.csdn.net/qq_44844588/article/details/107347108