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): 7895    Accepted Submission(s): 2795


    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
     
    Recommend
     
    将钥匙压缩,枚举所有状态。。一个小错误差点要了我老命
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    using namespace std;
    const int N = 22;
    int n,m,t;
    int vis[N][N][1<<10]; ///第三维存的钥匙的状态
    char graph[N][N];
    struct Node{
        int x,y,step,state;
    }s;
    int dir[][2] = {{-1,0},{1,0},{0,1},{0,-1}};
    bool check(int x,int y){
        if(x<1||y<1||x>n||y>m||graph[x][y]=='*') return false;
        return true;
    }
    int bfs(){
        memset(vis,false,sizeof(vis));
        queue<Node> q;
        q.push(s);
        vis[s.x][s.y][0] = 0;
        while(!q.empty()){
            Node now = q.front();
            q.pop();
            if(now.step>=t) return -1;
            if(graph[now.x][now.y]=='^') return now.step;
            Node next;
            for(int i=0;i<4;i++){
                next.x = now.x+dir[i][0];
                next.y = now.y+dir[i][1];
                next.step = now.step+1;
                next.state = now.state;
                if(!check(next.x,next.y)||vis[next.x][next.y][next.state]) continue;
                char c = graph[next.x][next.y];
                if(isupper(c)){
                    int t = c-'A';
                    if(!((1<<t)&next.state)) continue;
                }
                if(islower(c)){
                    int t = c-'a';
                    next.state = (1<<t)|next.state;
                }
                if(!vis[next.x][next.y][next.state]){
                    vis[next.x][next.y][next.state] = true;
                    q.push(next);
                }
            }
        }
        return -1;  ///没加这句wa无数发....因为它有可能在前面两个出口都没出去..以后写程序一定要严谨点
    }
    int main(){
        while(scanf("%d%d%d",&n,&m,&t)!=EOF){
            for(int i=1;i<=n;i++){
                scanf("%s",graph[i]+1);
                for(int j=1;j<=m;j++){
                    if(graph[i][j]=='@'){
                        graph[i][j]='.';
                        s.x = i;
                        s.y = j;
                        s.step = 0;
                        s.state = 0;
                    }
                }
            }
            printf("%d
    ",bfs());
        }
    }
  • 相关阅读:
    dota监测
    R0:前瞻
    Python基础
    c++成员函数
    异步IO简介
    使用自定义类型做qmap,qhash的key
    c++ primer 7 函数
    c++ primer 6 语句
    c++ primer 5 表达式
    c++ primer 4 数组和指针
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5747556.html
Copyright © 2011-2022 走看看