您的位置:首页 > 其它

hdu 2102 A计划(BFS,基础)

2014-01-21 22:42 399 查看
题目

//要仔细写的BFS,着重对#穿越的处理哦;

//花了几个小时终于把这道简单的BFS给弄好了,我果然还需要增加熟练度,需要再仔细一些;

//代码有点乱,但我不想改了,,,,,

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct tt
{
int x,y,step,floor;
};
int visit[20][20][2],n,m,t;
char map1[20][20],map2[20][20];
queue <tt> q;
int xx[4]={-1,0,1,0};
int yy[4]={0,1,0,-1};

int bfs()
{
int i,flag;
tt front,temp;
temp.x=0;temp.y=0;temp.step=0;temp.floor=1;
q.push(temp);
visit[0][0][0]=1;
while(!q.empty())
{
front=q.front();
q.pop();
for(i=0;i<4;i++)
{

temp.x=front.x+xx[i];
temp.y=front.y+yy[i];
temp.step=front.step+1;
temp.floor=front.floor;
if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m)
{
if(temp.floor==1&&visit[temp.x][temp.y][0]==0&&map1[temp.x][temp.y]!='*')
{
flag=0;
visit[temp.x][temp.y][0]=1;
if(map1[temp.x][temp.y]=='P')
return temp.step;

if(map1[temp.x][temp.y]=='#')
{
flag=1;
if(map2[temp.x][temp.y]=='P')
return temp.step;
else if(map2[temp.x][temp.y]=='.'&&visit[temp.x][temp.y][1]==0)
{
temp.floor=2;
q.push(temp);

}
visit[temp.x][temp.y][1]=1;
}
if(flag==0)
q.push(temp);
}
else if(temp.floor==2&&visit[temp.x][temp.y][1]==0&&map2[temp.x][temp.y]!='*')
{
flag=0;

if(map2[temp.x][temp.y]=='P')
return temp.step;

if(map2[temp.x][temp.y]=='#')
{
flag=1;
if(map1[temp.x][temp.y]=='P')
return temp.step;
else if(map1[temp.x][temp.y]=='.'&&visit[temp.x][temp.y][0]==0)
{
temp.floor=1;
q.push(temp);

}
visit[temp.x][temp.y][0]=1;
}

visit[temp.x][temp.y][1]=1;
if(flag==0)
q.push(temp);
}

}
}
}
return t+1;
}

int main()
{
int T,i,ans;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&t);
for(i=0;i<n;i++)
{
scanf("%s",map1[i]);
}
for(i=0;i<n;i++)
{
scanf("%s",map2[i]);
}
memset(visit,0,sizeof(visit));
while(!q.empty())
q.pop();
ans=bfs();
if(ans<=t)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: