POJ-2386 Lake Counting
阿新 • • 發佈:2018-11-25
Lake Counting
Given a diagram of Farmer John's field, determine how many ponds he has.
* 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.
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
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.
Input
* 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.
題意:
有一個大小為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;
}