zoukankan      html  css  js  c++  java
  • HDU 1429 BFS+状态压缩

    胜利大逃亡(续)
    Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 5461 Accepted Submission(s): 1878


    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


    Source

    ACM暑期集训队练习赛(三)

    /*************************************************************************
    	> File Name: 3.cpp
    	> Author:yuan
    	> Mail:
    	> Created Time: 2014年11月26日 星期三 13时09分34秒
     ************************************************************************/
    
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    char mat[25][25];
    int ans;
    struct node
    {
        int x,y;
        int num,key;
    };
    bool vis[20][20][1300];
    node q[1000000];
    int sx,sy,ex,ey;
    int n,m,t,top,base;
    int next[4][2]={1,0,0,1,-1,0,0,-1};
    bool check(int x1,int y1)
    {
        if(x1>=0&&x1<n&&y1>=0&&y1<m)
           return 1;
        return 0;
    }
    void BFS()
    {
        int x1,x2,y1,y2;
        while(base<top){
          x1=q[base].x;y1=q[base].y;
          int key=0;
            if(x1==ex&&y1==ey&&q[base].num<ans){
                ans=q[base].num;
            }
            for(int i=0;i<4;i++)
            {
                x2=x1+next[i][0];y2=y1+next[i][1];
                if(!check(x2,y2)) continue;
                if(mat[x2][y2]=='.'||mat[x2][y2]=='@'||mat[x2][y2]=='^'){
                    key=q[base].key;
                    if(vis[x2][y2][key]==0){
                        q[top].x=x2;q[top].y=y2;q[top].num=q[base].num+1;q[top].key=q[base].key;
                        vis[x2][y2][key]=1;
                        top++;
                    }
                }
                 else if(mat[x2][y2]>='A'&&mat[x2][y2]<='J')
                {
                    key=q[base].key;
                    if(vis[x2][y2][key]==0){
                        int lock=1<<(mat[x2][y2]-'A');
                        if(lock&key){
                            q[top].x=x2;q[top].y=y2;q[top].num=q[base].num+1;q[top].key=q[base].key;
                            vis[x2][y2][key]=1;
                            top++;
                        }
                    }
                }
                else if(mat[x2][y2]>='a'&&mat[x2][y2]<='j')
                {
                    key=q[base].key;
                    int d=1<<(mat[x2][y2]-'a');
                    if(vis[x2][y2][key]==0){
                        q[top].x=x2;q[top].y=y2;q[top].num=q[base].num+1;q[top].key=q[base].key|d;
                        vis[x2][y2][key]=1;
                        top++;
                    }
                }
            }
         base++;
        }
    
    }
    int main()
    {
        while(~scanf("%d%d%d",&n,&m,&t)){
            memset(mat,0,sizeof(mat));
            memset(vis,0,sizeof(vis));
            memset(q,0,sizeof(q));
            top=base=0;
            for(int i=0;i<n;i++) scanf("%s",mat[i]);
            for(int i=0;i<n;i++)
              for(int j=0;j<m;j++)
            {
                if(mat[i][j]=='@') {sx=i,sy=j;}
                else if(mat[i][j]=='^'){ex=i;ey=j;}
            }
            q[top].x=sx;q[top].y=sy;q[top].num=0;
            q[top].key=0;top++;
            vis[sx][sy][0]=1;
            ans=100000;
            BFS();
            if(ans<t) printf("%d
    ",ans);
            else printf("-1
    ");
        }
        return 0;
    }
    
    


  • 相关阅读:
    背景透明的static控件
    MFC应用程序配置不正确解决方案
    #pragma once与 #ifndef的区别
    一种新颖的流程控制方式
    DbgView.exe的应用和使用类
    内存对齐的一点个人理解
    MFC下实现透明位图
    结束已知应用程序名的进程
    基本线程编程
    Linux下PCI设备驱动程序开发的经典文章
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254390.html
Copyright © 2011-2022 走看看