Lake Counting
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 32140 | Accepted: 16061 |
Description
Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.
Given a diagram of Farmer John's field, determine how many ponds he has.
Given a diagram of Farmer John's field, determine how many ponds he has.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
Output
* Line 1: The number of ponds in Farmer John's field.
Sample Input
10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W.W......W. ..W.......W.
Sample Output
3
Hint
OUTPUT DETAILS:
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
Source
思路:
题意不多说。。不明白为什么在输入nm的时候用~scanf 就不行,按理说不应该啊,加上~scanf 也不执行函数体 就一直处于待输入状态,去掉就出结果了。想不明白。
另外一个就是如果出现 [Error] ld returned 1 exit status。就说明系统exe文件还在运行,建议强制关闭之后重启dev。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[105][105];
int sum;
int n,m;
void dfs(int i,int j)
{
int dir[8][2]={{0,1},{0,-1},{-1,0},{1,0},{1,-1},{-1,1},{1,1},{-1,-1}};
a[i][j]=0;
for(int w=0;w<8;w++){
int dx=i+dir[w][0];
int dy=j+dir[w][1];
if(dx>=0&&dx<n&&dy>=0&&dy<m&&a[dx][dy]==1)dfs(dx,dy);
}
return;
}
int main()
{
cin>>n>>m;
sum=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
char x;
cin>>x;
if(x=='.')a[i][j]=0;
if(x=='W')a[i][j]=1;
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(a[i][j]==1)
{
dfs(i,j);
sum++;
}
}
// for(int i=0;i<n;i++)
// for(int j=0;j<m;j++)
// {
// cout<<a[i][j]<<" ";
// if(j==m-1)cout<<endl;
// }
printf("%d\n",sum);
return 0;
}