zoukankan      html  css  js  c++  java
  • 胜利大逃亡(续)(状态压缩bfs)

    胜利大逃亡(续)

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7357    Accepted Submission(s): 2552

    Problem Description
    Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……
    这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方。刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个。魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去。经过若干次的尝试,Ignatius已画出整个地牢的地图。现在请你帮他计算能否再次成功逃亡。只要在魔王下次视察之前走到出口就算离开地牢,如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。
     
    Input
    每组测试数据的第一行有三个整数n,m,t(2<=n,m<=20,t>0)。接下来的n行m列为地牢的地图,其中包括:
    . 代表路 * 代表墙 @ 代表Ignatius的起始位置 ^ 代表地牢的出口 A-J 代表带锁的门,对应的钥匙分别为a-j a-j 代表钥匙,对应的门分别为A-J
    每组测试数据之间有一个空行。
     
    Output
    针对每组测试数据,如果可以成功逃亡,请输出需要多少分钟才能离开,如果不能则输出-1。
     
    Sample Input
    4 5 17 @A.B. a*.*. *..*^ c..b* 4 5 16 @A.B. a*.*. *..*^ c..b*
     
    Sample Output
    16 -1
     
    Author
    LL
     

    题解:状态压缩bfs,比赛的时候无限me。。。。心中一万头草泥马奔腾。。。最后大家都说用状态压缩记录钥匙,,,瞬间感觉自己智商负无穷了。。。

    其实也不难想,关键以前没怎么用状态压缩;当时想到了用vis存位置以及钥匙数,但是不知道怎么记录,为什么想不到状态压缩呐。。。要长记性了;

    代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<queue>
     7 using namespace std;
     8 struct Node{
     9     int nx,ny,step;
    10     //int key[11];
    11     int key;
    12 };
    13 char mp[25][25];
    14 int vis[25][25][1025];
    15 int disx[4] = {0,0,1,-1};
    16 int disy[4] = {1,-1,0,0};
    17 int n,m,t;
    18 void bfs(int sx,int sy){
    19     memset(vis,0,sizeof(vis));
    20     queue<Node>q;
    21     Node a,b;
    22     a.nx = sx;a.ny = sy;
    23     a.step = 0;
    24     //for(int i = 0;i < 11;i++)a.key[i] = 0;
    25     a.key = 0;
    26     vis[sx][sy][0] = 1;
    27     q.push(a);
    28     while(!q.empty()){
    29         a = q.front();
    30         q.pop();
    31         for(int i = 0; i < 4;i++){
    32             b.nx = a.nx + disx[i];
    33             b.ny = a.ny + disy[i];
    34             b.step = a.step + 1;
    35             int x = b.nx, y = b.ny, step = b.step;
    36             //for(int i = 0;i < 11;i++)b.key[i] = a.key[i];    
    37             b.key = a.key;
    38             if(x < 0 || y < 0 || x >= n || y >= m)continue;
    39             if(mp[x][y] == '*')continue;
    40             if(step >= t)continue;
    41             if(mp[x][y] == '^'){
    42                 printf("%d
    ",step);
    43                 return;
    44             }
    45     //        if(vis[x][y] > 10)continue;
    46             if(mp[x][y] >= 'a' && mp[x][y] <= 'j'){
    47                 //b.key[mp[x][y] - 'a'] = 1;
    48                 int p = 1 << (mp[x][y] - 'a');
    49                 b.key |= p;
    50                 if(!vis[x][y][b.key]){
    51                     vis[x][y][b.key] = 1;
    52                     q.push(b);
    53                 }
    54                 continue;
    55             }
    56             if(mp[x][y] >= 'A' && mp[x][y] <= 'J'){
    57                 //if(b.key[mp[x][y] - 'A'] == 0)continue;
    58                 int p = 1 << (mp[x][y] - 'A');
    59                 if(b.key & p){
    60                     if(!vis[x][y][b.key]){
    61                         vis[x][y][b.key] = 1;
    62                         q.push(b);
    63                     }
    64                 }
    65                 continue;
    66             }
    67             if(!vis[x][y][b.key]){
    68                 vis[x][y][b.key] = 1;
    69                 q.push(b);
    70             }
    71         }
    72     }
    73     puts("-1");
    74     return;
    75 }
    76 int main(){
    77     while(~scanf("%d%d%d",&n,&m,&t)){
    78         int sx,sy;
    79         for(int i = 0;i < n; i++){
    80             scanf("%s",mp[i]);
    81             for(int j = 0;j < m; j++){
    82                 if(mp[i][j] == '@'){
    83                     sx = i;
    84                     sy = j;
    85                 }
    86             }
    87         }
    88         mp[sx][sy] = '.';
    89         bfs(sx,sy);
    90     }
    91     return 0;
    92 }
  • 相关阅读:
    算法
    jquery-lazyload延迟加载图片
    配置图片服务器
    Flutter开发之dart语言从入门到精通(从入坑到入土)
    阿里淘系技术分享:Flutter 快速上手方法!!!
    如何优雅的处理 Android 重复点击 [建议收藏]
    还谈论Android的前景?根本没什么好问的……
    一个商业级智能家居 Android 开源项目分享
    【前端算法】拼多多技术面试算法题分享
    美团Android岗面试真题:手写红黑树详解
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5357168.html
Copyright © 2011-2022 走看看