Blue Bridge Cup [Global Warming] Java

global warming

. "" Do you have a photo of Zhang waters NxN pixels, expressed ocean, "#" represents the land, as follows:
●●●●●●●
● ## ●●●●
● ## ●●●●
●●● ●##●
●●####●
●●●###●
●●●●●●●
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 work: According to the scientists predict, the number of photos in the islands 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 row 1, column 1, row N, and column N are all oceans.
Output format:
An integer represents the answer.

Input sample:

7
●●●●●●●
●##●●●●
●##●●●●
●●●●##●
●●####●
●●●###●
●●●●●●●

Sample output:

1

Thinking analysis:

Because the problem guarantees that the outer circle is the ocean, I start to search from the (1,1) coordinates, and the number of islands that are submerged by "#" will increase by one, that is, num will increase by itself. Then search the surrounding area of ​​the'#', if there is no "." up, down, left, or right, indicating that the area is surrounded by land on all sides, the island must not be submerged, and the number of submerged islands num is decremented. Then call the change() function to replace the surrounding land area with the ocean "." (it can be understood that we manually submerge the entire island── _ ───✧) to prevent the island from being searched multiple times later; if you search If the "#" is not all surrounded by ".", then the "#" will be replaced with "0" to indicate that it has been searched to prevent multiple searches. Then call the find() function to repeat the connected "#" The above operations until it is determined whether the island will be submerged.

Code:

import java.io.*;
import java.util.Stack;

public class Main {
    
    

	static char[][] arr;
	static int num = 0;
	static void find(int x, int y) {
    
    
		arr[x][y] = '0';
		if (arr[x + 1][y] != '.' && arr[x - 1][y] != '.' && arr[x][y + 1] != '.' && arr[x][y - 1] != '.') {
    
    
			num--;
			change(x, y);
			return;
		}

		if (arr[x + 1][y] == '#')
			find(x + 1, y);
		if (arr[x - 1][y] == '#')
			find(x - 1, y);
		if (arr[x][y + 1] == '#')
			find(x, y + 1);
		if (arr[x][y - 1] == '#')
			find(x, y - 1);
	}

	static void change(int x, int y) {
    
    
		Stack<Integer> stack = new Stack<>();
		stack.add(x);
		stack.add(y);
		while (!stack.isEmpty()) {
    
    
			y = stack.pop();
			x = stack.pop();
			arr[x][y] = '.';
			if (arr[x + 1][y] != '.') {
    
    
				stack.add(x + 1);
				stack.add(y);
			}
			if (arr[x - 1][y] != '.') {
    
    
				stack.add(x - 1);
				stack.add(y);
			}
			if (arr[x][y + 1] != '.') {
    
    
				stack.add(x);
				stack.add(y + 1);
			}
			if (arr[x][y - 1] != '.') {
    
    
				stack.add(x);
				stack.add(y - 1);
			}
		}
	}

	public static void main(String[] args) throws IOException {
    
    
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		arr = new char[n][n];
		for (int i = 0; i < n; ++i) {
    
    
			arr[i] = br.readLine().toCharArray();
		}
		for (int i = 1; i < n - 1; ++i) {
    
    
			for (int j = 1; j < n - 1; ++j) {
    
    
				if (arr[i][j] == '#') {
    
    
					num++;
					find(i, j);
				}
			}
		}
		System.out.println(num);
	}

}

operation result

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_50816725/article/details/109035701