HDU 2952 Counting Sheep
2016-04-21 18:55
357 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2952和油区那个一样,就是改了个搜索方向,用dfs标记#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;
}
#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;
}
相关文章推荐
- hiho #1015 : KMP算法
- UVa11729 Commando War
- JZOJ4446【HNOI模拟】B
- Qt(QLabel)实现滚动字幕
- UVa-11292 -Dragon of Loowater
- UVa100 The 3n + 1 problem
- #261B. Pashmak and Flowers
- #274A. Expression
- 选前5 名的链表问题
- #274B. Towers
- CF 261A. Pashmak and Garden
- HDU 1181 变形课
- 使用grunt实现css压缩
- #267B. Fedor and New Game-----每日两道水(1)
- PCA的数学原理
- HDU1241 Oil Deposits
- 英语演讲
- ACM牡丹江区域赛
- HDU 5059 help him
- HDU4802 GPA