标签:des style http color java os strong io


2 2 DK HF 3 3 ADC FJK IHE -1 -1
2 3
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n,m,ans;
const int maxn=50+10;
int map[maxn][maxn];
int dX[]={-1,0,1,0};
int dY[]={0,1,0,-1};
int pipe[11][4]={{1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0},{1,0,1,0},{0,1,0,1},{1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1,0},{1,1,1,1}};
int hash[maxn][maxn];
bool check(int row,int col)
{
if(row>=0&&row<n&&col>=0&&col<m&&!hash[row][col])
return true;
return false;
}
void dfs(int row,int col)
{
hash[row][col]=1;
for(int k=0;k<4;k++)
{
int dx=row+dX[k];
int dy=col+dY[k];
if(check(dx,dy)&&pipe[map[row][col]][k]==1&&pipe[map[dx][dy]][(k+2)%4]==1)
dfs(dx,dy);
}
}
void solve()
{
memset(hash,0,sizeof(hash));
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(!hash[i][j])
{
dfs(i,j);
ans++;
}
}
}
int main()
{
char str[maxn];
while(~scanf("%d%d",&n,&m))
{
ans=0;
if(n<=0||m<=0) return 0;
for(int i=0;i<n;i++)
{
scanf("%s",str);
for(int j=0;j<m;j++)
map[i][j]=str[j]-'A';
}
solve();
printf("%d\n",ans);
}
return 0;
}
hdu1198Farm Irrigation(dfs找联通),布布扣,bubuko.com
hdu1198Farm Irrigation(dfs找联通)
标签:des style http color java os strong io
原文地址:http://blog.csdn.net/u014303647/article/details/38373585