Blue Bridge Cup Global Warming (Java)

Title description

You have a picture of a certain sea area with NxN pixels, "." means ocean and "#" means land, as shown below:

7

.##…
.##…
…##.
…####.
…###.

A piece of land connected together in the four directions of "up, down, left, and right" forms an island. For example, there are 2 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. (1 <= N <= 1000) The
following N rows and N columns represent a sea area photo.

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.

【Input sample】

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

[Sample output]

1

【Input sample】

5
. . . . .
. # . # .
. . # . .
. # . # .
. . . . .

[Sample output]

5

Problem-solving ideas:

First, read the topic. We have to split it into two problems to solve. First, how to find all the islands in the ocean. Second, how to judge whether these islands will be submerged by sea water. Let’s look at the first problem first, from the data structure. It can be seen that this is a matrix to find a connection map. We can use the traversal of the map. However, due to the limitation of the data size, in order to avoid memory overflow, we choose the breadth-first traversal. First call the work method to traverse the map ocean and land maps to find the land. When we find a new piece of land, we call bfs to explore and find this new island. Every time we find an island, we will print its marker array to verify the result. Let's judge the second problem. If a piece of land is surrounded by the sea, it will definitely be submerged. Therefore, when we solve the first problem, we can solve the second problem with a piggyback, and then use cn1 to record the island land. Count, use cn2 to record the number of land on the island’s seafront. If the two are equal, the entire island must be submerged. The sum is added in sum.

package 蓝桥杯;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
/**
 * 
 * @author Administrator
 * 矩阵联通图 bfs
 */
public class 全球变暖 {
    
    
	public static void main(String[] args) {
    
    
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		char[][] map = new char[n][n];
		int[][] flag = new int[n][n];

		for(int i = 0;i < n;i++) {
    
    
			String str = in.next();
			for(int j = 0;j < n;j++) {
    
    
				map[i] = str.toCharArray();
			}
		}
		System.out.println("*****************************");
		work(map,flag,n);
	}
	//发现陆地
	public static void work(char[][] map,int[][] flag,int n) {
    
    
		int sum = 0;
		for(int i = 0;i < n;i++) {
    
    
			for(int j = 0;j < n;j++) {
    
    
				if(map[i][j] == '#' && flag[i][j] == 0) {
    
    
					if(bfs(i,j,flag,map)) {
    
    
						sum++;
					}
					//求完陆地便进行打印标记数组校验
					for(int a = 0;a < n;a++) {
    
    
						for(int b = 0;b < n;b++) {
    
    
							System.out.print(flag[a][b] + " ");
						}
						System.out.println();
					}
					System.out.println("*****************************");
				}
			}
		}
		System.out.println(sum);
	}
	//遍历找出某一个岛屿
	public static boolean bfs(int x,int y,int[][] flag,char[][] map) {
    
    
		int[] dx = {
    
    0,1,-1,0};//右,下,上,左
		int[] dy = {
    
    1,0,0,-1};
		int cn1 = 0;
		int cn2 = 0;
		int size = flag[0].length;
		Queue<land> queue = new LinkedList<land>();
		land first = new land(x,y);
		flag[x][y] = 1;
		queue.add(first);
		while(!queue.isEmpty()) {
    
    
			land temp = queue.poll();
			cn1++;
			for(int i = 0;i < 4;i++) {
    
    
				int x1 = temp.x+dx[i];
				int y1 = temp.y+dy[i];
				if(0 <= x1 && x1 < size && 0 <= y1 && y1 < size && map[x1][y1] == '#') {
    
    
					if(check(x1,y1,flag)) {
    
    
						flag[x1][y1] = 1;
						land node = new land(x1,y1);
						queue.add(node);
					}
				}
			}
			if(nearSea(temp.x,temp.y,map))
				cn2++;
		}
		if(cn1 == cn2)
			return true;
		else
			return false;
	}
	public static boolean check(int x,int y,int[][] flag) {
    
    
		if(flag[x][y] == 0)
			return true;
		else
			return false;
	}
	public static boolean nearSea(int x,int y,char[][] map) {
    
    
		int[] dx = {
    
    0,1,-1,0};//右,下,上,左
		int[] dy = {
    
    1,0,0,-1};
		for(int i = 0;i < 4;i++) {
    
    
			int x1 = x+dx[i];
			int y1 = y+dy[i];
			if(map[x1][y1] == '.') 
				return true;
		}
		return false;
	}
}
class land{
    
    
	int x;
	int y;
	public land(int x,int y) {
    
    
		this.x = x;
		this.y = y;
	}
}

Guess you like

Origin blog.csdn.net/baldicoot_/article/details/109478131