ZOJ——2412(深搜)

题目链接: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; 
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值