当前位置: 代码迷 >> 综合 >> 题目 2276: [蓝桥杯][2018年第九届真题]全球变暖
  详细解决方案

题目 2276: [蓝桥杯][2018年第九届真题]全球变暖

热度:0   发布时间:2023-12-06 21:39:46.0

 

时间限制: 1Sec 内存限制: 128MB 提交: 1087 解决: 261

题目描述

你有一张某海域NxN像素的照片,"."表示海洋、"#"表示陆地,如下所示:

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

其中"上下左右"四个方向上连在一起的一片陆地组成一座岛屿。例如上图就有2座岛屿。  

由于全球变暖导致了海面上升,科学家预测未来几十年,岛屿边缘一个像素的范围会被海水淹没。具体来说如果一块陆地像素与海洋相邻(上下左右四个相邻像素中有海洋),它就会被淹没。  

例如上图中的海域未来会变成如下样子:

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

请你计算:依照科学家的预测,照片中有多少岛屿会被完全淹没。  

输入

第一行包含一个整数N。  (1 <= N <= 1000)  
以下N行N列代表一张海域照片。  

照片保证第1行、第1列、第N行、第N列的像素都是海洋。 

输出

一个整数表示答案。

样例输入

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

样例输出

1 

 

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;
const int inf=0x3fffffff;
const int maxn=1010;string str[maxn];
int ans,n;
int X[4]={0,0,-1,1};
int Y[4]={1,-1,0,0};
bool inq[maxn][maxn]={false};struct point{int x,y;point(){}point(int _x,int _y) { x=_x,y=_y; } 
};void bfs(int x,int y){ int cnt1=0,cnt2=0;queue<point> q;point top = point(x,y);q.push(top);inq[x][y]=true;while(!q.empty()){//top = q.front(); q.pop();cnt1++;// int f=0;int newX,newY;for(int i=0;i<4;i++){newX = top.x + X[i];newY = top.y + Y[i];if(str[newX][newY]=='.') f=1;if(inq[newX][newY]==false && str[newX][newY]=='#'){q.push(point(newX,newY));inq[newX][newY]=true;}}if(f==1) cnt2++;}if(cnt1==cnt2) ans++;
}int main(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin>>n;for(int i=0;i<n;i++)  cin>>str[i];for(int i=0;i<n;i++){for(int j=0;j<n;j++){if(str[i][j]=='#'&&inq[i][j] == false){bfs(i,j);
//				ans++;}}}cout<<ans<<endl;return 0;
}