您的位置:首页 > 其它

poj 2386 Lake Counting

2015-03-12 15:01 423 查看
代码不写就手生啊。。。

注意输入, 二维数组

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=100+5;
char field[maxn][maxn];
int n,m;
void input()
{
scanf("%d%d", &n, &m);
for(int i=0;i<n;i++)
scanf("%s", field[i]);
}

void dfs(int x, int y)
{
field[x][y]='.';
for(int dx=-1; dx<=1; dx++)
for(int dy=-1; dy<=1; dy++)
{
int nx=x+dx,  ny=y+dy;
if(nx<n && nx>=0 && ny<m && ny>=0 && field[nx][ny]=='W')
dfs(nx, ny);
}
}

int solve()
{
int ans=0;
for(int x=0; x<n; x++)
for(int y=0; y<m; y++) if(field[x][y]=='W')
{
dfs(x, y);
ans++;
}
return ans;
}

int main()
{
input();
printf("%d\n", solve());

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: