HDU 2102 BFS A计划

Problem Description
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
 

Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。
 

Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
 

Sample Input
  
  
1 5 5 14 S*#*. .#... ..... ****. ...#. ..*.P #.*.. ***.. ...*. *.#..
 

Sample Output
  
  
YES
 
   AC代码
  1. #include<cstdio>
  2. #include<queue>
  3. #include<cstring>
  4. using namespace std;
  5. int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
  6. int n,m,T;
  7. char first[15][15],second[15][15];          //分别存储两层楼
  8. int vis[2][15][15];
  9. struct point
  10. {
  11.     int x,y,step,type;                                   //type为0表示第0层,为1表示第一层
  12. }p[105];
  13. queue<point> que;
  14. bool inside(int x,int y)
  15. {
  16.     if(x>=n||x<0) return false;
  17.     if(y>=m||y<0) return false;
  18.     return true;
  19. }
  20. bool bfs()
  21. {
  22.     while(!que.empty()) que.pop();            // 初始化
  23.     memset(vis,0,sizeof(vis));
  24.     point t;
  25.     t.x=0;t.y=0;t.step=0;t.type=0;
  26.     vis[0][0][0]=1;
  27.     que.push(t);                                             //骑士位置入队
  28.     while(!que.empty()){
  29.         point u=que.front();
  30.         if(u.type==0){
  31.             if(first[u.x][u.y]=='P')                          //找到公主
  32.                 if(u.step<=T)
  33.                     return true;
  34.                 else
  35.                     return false;
  36.         }
  37.         else{
  38.             if(second[u.x][u.y]=='P')
  39.                 if(u.step<=T)
  40.                     return true;
  41.                 else
  42.                     return false;
  43.         }
  44.         que.pop();
  45.         int cx=u.x,cy=u.y;
  46.         for(int i=0;i<4;i++){
  47.             point temp;
  48.             temp.x=cx+dx[i];
  49.             temp.y=cy+dy[i];
  50.             temp.step=u.step+1;
  51.             if(inside(temp.x,temp.y)&&!vis[u.type][temp.x][temp.y]){
  52.                 if(u.type==0){                                         //在第0层
  53.                     if(first[temp.x][temp.y]=='.'){
  54.                         temp.type=u.type;
  55.                         vis[u.type][temp.x][temp.y]=1;
  56.                         que.push(temp);                             //可行则入队
  57.                     }
  58.                     else if(first[temp.x][temp.y]=='#'){
  59.                         vis[u.type][temp.x][temp.y]=1;
  60.                         temp.type=!u.type;
  61.                         que.push(temp);                              //传送门,另一层相应位置入队
  62.                     }
  63.                     else if(first[temp.x][temp.y]=='P'){       //找到公主
  64.                         if(temp.step<=T) return true;
  65.                         else return false;
  66.                     }
  67.                 }
  68.                 else{                                                             //在第一层
  69.                     if(second[temp.x][temp.y]=='.'){             //操作同在第0层是类似
  70.                         temp.type=u.type;
  71.                         vis[u.type][temp.x][temp.y]=1;
  72.                         que.push(temp);
  73.                     }
  74.                     else if(second[temp.x][temp.y]=='#'){
  75.                         temp.type=!u.type;
  76.                         vis[u.type][temp.x][temp.y]=1;
  77.                         que.push(temp);
  78.                     }
  79.                     else if(second[temp.x][temp.y]=='P'){
  80.                         if(temp.step<=T) return true;
  81.                         else return false;
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.     }
  87.     return false;//找不到公主
  88. }
  89. int main()
  90. {
  91.     int t;
  92.     char temp[15];
  93.     scanf("%d",&t);
  94.     while(t--){
  95.         scanf("%d%d%d",&n,&m,&T);
  96.         for(int i=0;i<n;i++){
  97.             scanf("%s",&temp);
  98.             for(int j=0;j<m;j++){
  99.                 first[i][j]=temp[j];
  100.             }
  101.         }
  102.         for(int i=0;i<n;i++){
  103.             scanf("%s",&temp);
  104.             for(int j=0;j<m;j++){
  105.                 second[i][j]=temp[j];
  106.                 if(first[i][j]=='*'&&second[i][j]=='#')    //避免到达传送门时判断是否会撞墙,注意若另一层也是传送门则也是不可行的
  107.                     second[i][j]='*';
  108.                 if(first[i][j]=='#'&&second[i][j]=='*')
  109.                     first[i][j]='*';
  110.                 if(first[i][j]=='#'&&second[i][j]=='#')    
  111.                     first[i][j]=second[i][j]='*';
  112.             }
  113.         }
  114.         if(bfs()) printf("YES\n");
  115.         else printf("NO\n");
  116.     }
  117.     return 0;
  118. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值