#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int n,m,i,j;
char mp[110][110];
int vis[110][110];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
void dfs(int x,int y)
{
vis[x][y]=1;
for(int i=0;i<4;i++)
{
int nx=x+dir[i][0];
int ny=y+dir[i][1];
if(!vis[nx][ny]&&nx<=n&&nx>=1&&ny>=1&&ny<=m&&mp[nx][ny]=='#')
{
vis[nx][ny]=1;
dfs(nx,ny);
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t,ans;
cin>>t;
while(t--)
{
cle(vis);
cin>>n>>m;
ans=0;
reP(i,n)
reP(j,m){cin>>mp[i][j];}
reP(i,n)
reP(j,m)
{
if(!vis[i][j]&&mp[i][j]=='#')
{
dfs(i,j);
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
HDU 2952 Counting Sheep
最新推荐文章于 2017-07-18 19:26:25 发布
http://acm.hdu.edu.cn/showproblem.php?pid=2952
和油区那个一样,就是改了个搜索方向,用dfs标记