# Global warming java [9th] [provincial competition] [group B] solution to question 9

### Resource constraints

Time limit: 1.0s   Memory limit: 256.0MB

### Problem description

You have a NxN pixel photo of a certain sea area, "." represents the ocean and "#" represents the land, as shown below:

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

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

As the sea level rises due to global warming, scientists predict that a pixel of the edge of the island will be submerged by the sea in the next few decades. Specifically, if a land pixel is adjacent to the ocean (there is an ocean in the four adjacent pixels above, below, left and right), it will be submerged.

For example, the sea area in the above figure will look like the following in the future:

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

Please calculate: according to the prediction of scientists, how many islands in the picture 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 ensures that the pixels in row 1, column 1, row N and column n are oceans.

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

1

### Problem solving ideas:

The number of islands to be submerged is actually the number of connected blocks to be submerged, and the condition of inundation is that the total number of islands in the current connected blocks is equal to the number of islands to be adjacent. The number of connected blocks satisfying the above conditions is the final answer.

Access Unicom blocks can be searched in depth or breadth, because the data scale gives, if there is only one large island in this matrix, deep search needs to be done in time and spaceTherefore, breadth first search is more appropriate at this time.

When searching, you need to use the access array to mark whether a small island has been accessed, and you also need a coordinate class to store the specific information of a coordinate. It should be noted that the access array needs to be modified in time after accessing the connected block for the first time and when you encounter an island that has not been accessed during extensive search.

### java code:

```import java.io.*;
import java.util.*;

public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
char [][]map = new char[n][n];
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
map[i][j] = str.charAt(j);
}
}
Temp118 obj = new Temp118(map, n);
obj.bfs();
System.out.println(obj.ans);
}
}

class Temp118{
char[][] map;//Map information
boolean[][] flag;//Flag access information
int n;//Map side length
int []dx = new int[] {1,-1,0,0};//The matching downward direction represents the right, left, top and bottom of the datum point respectively
int []dy = new int[] {0,0,1,-1};
int ans;//Number of submerged Islands

public Temp118(char[][] map, int n) {
this.map = map;
this.n = n;
flag = new boolean[n][n];
}

public void bfs() {//Overload the bfs with parameters and find the "connected block" that is not accessed
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(flag[i][j] == false && map[i][j] == '#') {//The map information is # numbered and not accessed, indicating a new connected block
bfs(i, j);
}
}
}
}

public void bfs(int x, int y) {//Access each connected block
flag[x][y] = true;//The point that is not accessed is marked as accessed
int count1 = 0;//The connected block contains the number of islands
int count2 = 0;//The number of islands in the connected block adjacent to the ocean, that is, the number of islands adjacent to '.'
qu.add(new Point(x, y));//Queue the first coordinate
while(!qu.isEmpty()) {
Point point = qu.poll();//Pop up one coordinate at a time and judge the information of the coordinate
count1++;
boolean tag = false;//Mark whether the island is adjacent to the sea
for(int i = 0; i < 4; i++) {//Visit the upper, lower, left and right sides of the island
int a = point.x + dx[i];
int b = point.y + dy[i];
if(map[a][b] == '.')tag = true;
if(map[a][b] == '#'& & Flag [a] [b] = = false) {/ / there are unreachable islands in the current connected block
flag[a][b] = true;//Flag as accessed
}
}
if(tag)count2++;//Number of adjacent seas plus one
}
if(count1 == count2)ans++;//If the number of islands in the current connected block is equal to the number of adjacent islands, the connected block will be submerged
}
}

class Point{//Coordinate class, which stores the horizontal and vertical coordinates of a point
int x;
int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}
}```

### Submit screenshot:

Tags: Java search engine bfs

Posted on Mon, 06 Dec 2021 00:18:23 -0500 by dannel77