题目描述
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.
题目和UVA572一毛一样,就是换了个符号表示,不多说了。
日常安利本题我的博客
思路分析
我光明正大的表示,就是划水,你打我啊~~~
代码
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string>
#include <set>
#include<time.h>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
const double eps = 1e-8;
const int maxn = 110;//须填写
const int inf = 0x3f3f3f3f;
char pic[maxn][maxn];
int vis[maxn][maxn];
int n,m;
void dfs(int x, int y, int s)
{
if(x<0||y<0||x>=m||y>=n)
return ;
for(int i=-1;i<2;i++)
{
for(int j=-1;j<2;j++)
{
if(vis[i+x][j+y] == 0&&pic[i+x][j+y]=='W')
{
vis[i+x][j+y] = s;
dfs(i+x,j+y,s);
}
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("sample.out", "w", stdout);
int results;
scanf("%d%d",&m,&n);
results = 0;
mem(vis);
for(int i=0;i<m;i++)
{
scanf("%s",pic[i]);
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(vis[i][j] == 0&&pic[i][j]=='W')
{
vis[i][j] = ++results;
dfs(i,j,results);
}
}
}
printf("%d\n",results);
return 0;
}