hdu 2102 A计划bfs

来源:互联网 发布:sql安装进度条不动 编辑:程序博客网 时间:2024/05/17 01:12

传送门:hdu2102

题意中能够在T时刻找到公主就是T时刻以内找到公主,因为大不了你可以在一个格子里多呆几秒嘛。。

典型广搜,book数组和入队条件稍微改动一下即可。

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;int n,m,T;int ef,er,ec;int go[4][2]={0,1,1,0,0,-1,-1,0};char map[3][12][12];int book[3][12][12];struct node{int f,r,c,time;};queue<node>q;int judge(int r,int c){if(r>=n||r<0||c>=m||c<0)return 0;return 1;}int bfs(){int c,r,f;while(!q.empty()){node t=q.front();q.pop();r=t.r,c=t.c,f=t.f;if(f==ef&&r==er&&c==ec)return 1;if(t.time>=T)break;for(int i=0;i<4;i++){node k=t;k.c+=go[i][1];k.r+=go[i][0];k.time++;if(!book[k.f][k.r][k.c]&&judge(k.r,k.c)&&map[k.f][k.r][k.c]!='*'){book[k.f][k.r][k.c]=1;if(map[k.f][k.r][k.c]=='#'){k.f=!k.f; //看题解学会了巧妙转换楼层if(!book[k.f][k.r][k.c]){if(map[k.f][k.r][k.c]!='*'&&map[k.f][k.r][k.c]!='#')//注意这里传送门的对应位置还是{<span style="white-space:pre"></span>//传送门也不能入队,否则会陷入死循环。。book[k.f][k.r][k.c]=1;}elsecontinue;}elsecontinue;}if(k.f==ef&&k.r==er&&k.c==ec)return 1;q.push(k);}}}return 0;}int main(){int u;scanf("%d",&u);while(u--){while(!q.empty())q.pop(); memset(book,0,sizeof(book));scanf("%d%d%d",&n,&m,&T);getchar();for(int x=0;x<2;x++){for(int i=0;i<n;i++){for(int j=0;j<m;j++){scanf("%c",&map[x][i][j]);if(map[x][i][j]=='P'){ef=x;er=i;ec=j;}}getchar();}if(x==0)getchar(); }node a;a.f=0;a.r=0;a.c=0;a.time=0;q.push(a);book[0][0][0]=1;if(bfs())printf("YES\n");elseprintf("NO\n");}}

0 0
原创粉丝点击