topic
Do you have a sea Zhang N × N . "" N × N pixel photos, represents the ocean, "#" represents the land, as follows:
.......
.##....
.##....
....##.
..####.
...###. .......
The "up and down" on the four directions together a piece of land consisting of an island, for example, the figure had 2 two islands.
Due to the rising sea levels caused by global warming, scientists predict the next few decades, the islands edge a pixel range will be submerged.
Specifically, (four adjacent pixels in the vertical and horizontal marine) If a pixel and adjacent land ocean, it will be submerged.
For example in the figure above sea will become the future looks as follows:
.......
.......
.......
.......
....#..
.......
.......
Please work: According to the scientists predict, the number of photos in the islands will be completely submerged.
Input Format
The first row contains an integer N.
The following N N rows N N column containing a character from the "#" and "." Consisting of N × N represents the character N × N matrix, representing a sea picture, "#" land "." Represents the ocean.
Photos ensure that the first 1 row 1, 1 1, the first N N th, N N columns of pixels are ocean.
Output Format
An integer that represents the answer.
data range
1≤N≤10001≤N≤1000
Sample Input 1:
7
.......
.##....
.##....
....##.
..####.
...###. .......
Output Sample 1:
1
Sample Input 2:
9
.........
.##.##...
.#####...
.##.##...
.........
.##.#....
.#.###... .#..#.... .........
Output Sample 2:
思路
- To know the island where, you first need to know the pixel map that communicates using BFS traversal islands
- Determine whether a pixel is a border island, to see whether there is "·" It's down about the four elements
Need to pay attention to the point
- While traversing the island need to determine: 1) whether the point has been out of bounds 2) whether the point has been visited 3) The point is the "#" or "*"
- Control direction, creating dx [] = {-1,0,1,0}, dy [] = {0, 1, 0, -1} two arrays, four times with a for loop, the upper left to lower right turn order.
Code
#include<iostream> #include<queue> #define MAXN 1001 using namespace std; int n; char img[MAXN][MAXN]; bool vis[MAXN][MAXN]; int dx[] = { -1,0,1,0 }, dy[] = { 0, 1, 0, -1 }; /* 7 ....... .##.... .##.... ....##. ..####. ...###. ....... */ void bfs(int i, int j, int &sum, int &bound) { vis[i][j] = true; queue<pair<int, int>>q; pair<int, int> p(i, j); q.push(p); while (!q.empty()) { auto l = q.front(); q.pop(); sum++; bool is_bound = false; for (int k = 0; k < 4; k++) { int x = l.first + dx[k], y = l.second + dy[k]; if (x > n || y > n || x < 0 || y < 0 || vis[x][y]==true)continue; else if (img[x][y] == '#') { vis[x][y] = true; p = make_pair(x, y); q.push(p); } else if (img[x][y] == '.') { is_bound = true; } } if (is_bound == true)bound++; } } int main() { cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> img[i][j]; } } int count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (img[i][j] == '#' && vis[i][j] == false) { int sum = 0, bound = 0; bfs(i, j, sum, bound); if (sum == bound)count++; } } } cout << count; return 0; }