您的位置:首页 > 其它

ZOJ——2412(深搜)

2012-08-03 11:41 197 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2412

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
int m,n;
char a[52][52];
int flag[52][52];
int X[5]={-1,0,1,0};
int Y[5]={0,1,0,-1};
int dir[11][4]={{1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1},{1,0,1},{0,1,0,1},{1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1},{1,1,1,1}};
int check(int x,int y)
{
if(x<0||y<0||x>=m||y>=n)
return 0;
return 1;
}

void dfs(int x,int y)
{
flag[x][y]=1;
int xx,yy;
for(int i=0;i<4;i++)
{
xx=x+X[i];
yy=y+Y[i];
//cout<< " XX "<<xx<<" YY "<<yy<<endl;
if(!check(xx,yy)) continue;

if(flag[xx][yy]==1) continue;
//cout<<"enter 1 "<<endl;
if(dir[a[x][y]-65][i]==1)
{
//cout<<"enter 2 "<<endl;
if(i==1&&dir[a[xx][yy]-65][3]==1) dfs(xx,yy);
if(i==2&&dir[a[xx][yy]-65][0]==1) dfs(xx,yy);
if(i==3&&dir[a[xx][yy]-65][1]==1) dfs(xx,yy);
if(i==0&&dir[a[xx][yy]-65][2]==1) dfs(xx,yy);
}
}//end for
}
int main()
{
int i,j,k;
int count;
while(scanf("%d%d",&m,&n)==2&&m>=0&&n>=0)
{
memset(flag,0,sizeof(flag));
count=0;
for(i=0;i<m;i++)
scanf("%s",a[i]);

for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(!flag[i][j])
{
dfs(i,j);
}
else continue;
count++;
//cout<<" count "<<count<<" I "<<i<<" J "<<j<<endl;
}
printf("%d\n",count);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: