版权声明: https://blog.csdn.net/qq_40829288/article/details/80647907
Lake Counting
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 42735 | Accepted: 21161 |
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.
题意:
有一个大小为M*N的园子,雨后积起了水。八连通的积水被认为是连接在一起的。请求出园子里总共有多少水洼?(八连通指的是下图中相对W的*的部分)
限制条件:N,M<=100;
#include<iostream>
#include<cstring>
using namespace std;
char maze[100][101];
int cnt,N,M;
//只要临近的满足条件就一直搜下去,并将满足条件的置为'.',直到所有的临近全为'.'时才返回;
void dfs(int x,int y)
{
maze[x][y]='.';//将满足条件的位置全部置为'.';
//用这两个for循环循环遍历8个方向;
for(int i=-1;i<=1;i++)
for(int j=-1;j<=1;j++)
{
int nx=x+i;
int ny=y+j;
if(nx>=0&&nx<N&&ny>=0&&ny<M&&maze[nx][ny]=='W')
dfs(nx,ny);
}
return ;
}
int main()
{
while(cin>>N>>M&&N>=1&&N<=100&&M>=1&&M<=100)
{
cnt=0;
/*在读取字符数组的时候有两种方法,第一种如程序中所示,注意别忘了加getchar();
第二种:用两个for循环实现;
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
cin>>maze[i][j];
*/
getchar();
for(int i=0;i<N;i++)
gets(maze[i]);
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
{
if(maze[i][j]=='W')//找到第一个时'W'的位置;
{
cnt++;//进行dfs的次数就是满足条件的次数;
dfs(i,j);
}
}
cout<<cnt<<endl;
}
return 0;
}
还有一种DFS:
#include<iostream>
#include<cstring>
using namespace std;
const int MAX=110;
char arry[MAX][MAX];
int vist[MAX][MAX];
int num=0;
void DFS(int x,int y)
{
if(arry[x][y]=='.'||vist[x][y]||arry[x][y]==0)
return;
vist[x][y]=1;
DFS(x-1,y-1); DFS(x-1,y); DFS(x-1,y+1);
DFS(x,y-1); DFS(x,y+1);
DFS(x+1,y-1); DFS(x+1,y); DFS(x+1,y+1);
}
int main()
{
int N,M;
cin>>N>>M;
num=0;
memset(arry,0,sizeof(arry));
memset(vist,0,sizeof(vist));
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
cin>>arry[i][j];
for(int i=1;i<=N;++i)
{
for(int j=1;j<=M;++j)
if(!vist[i][j]&&arry[i][j]=='W')
{
++num;
DFS(i,j);
}
}
cout<<num<<endl;
return 0;
}