1249:Lake Counting
时间限制: 1000 ms 内存限制: 65536 KB
提交数: 12161 通过数: 6424
【题目描述】
题意:有一块N×M的土地,雨后积起了水,有水标记为‘W’,干燥为‘.’。八连通的积水被认为是连接在一起的。请求出院子里共有多少水洼?
【输入】
第一行为N,M(1≤N,M≤110)。
下面为N*M的土地示意图。
【输出】
一行,共有的水洼数。
【输入样例】
10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
【输出样例】
3
【AC代码】
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
inline int read()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0'||ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
void write(int n)
{
if(n>9)write(n/10);
putchar(n%10+'0');
}
int n,m,ans,dir[8][2]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
struct point
{
int px,py;
};
queue<point> que;
char w[120][120];
void bfs(int tx,int ty)
{
w[tx][ty]='.';
que.push((point){tx,ty});
while(!que.empty())
{
point p=que.front();
que.pop();
for(int i=0;i<8;i++)
{
int nx=p.px+dir[i][0],ny=p.py+dir[i][1];
if(nx>0&&nx<=n&&ny>0&&ny<=m&&w[nx][ny]=='W')
{
w[nx][ny]='.';
que.push((point){nx,ny});
}
}
}
}
int main(int argc,char **argv)
{
n=read(),m=read();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)cin>>w[i][j];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(w[i][j]=='W')
{
bfs(i,j);
ans++;
}
}
}
write(ans);
return 0;
}