搜索专题: HDU1429胜利大逃亡

胜利大逃亡(续)

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


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
Problem : 1429 ( 胜利大逃亡(续) )     Judge Status : Accepted
RunId : 21338472    Language : C++    Author : hnustwanghe
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>

using namespace std;
const int N = 20 + 5;
typedef struct node{
    int x,y,step,key;
    bool operator < (const node x)const {
        return step > x.step;
    }
}Node;
char mat[N][N];
bool visit[N][N][1030];
Node start;
int n,m,k;
const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int BFS(){
    priority_queue<Node> Q;
    memset(visit,0,sizeof(visit));
    Node s;
    visit[start.x][start.y][start.key] = true;
    Q.push(start);
    while(!Q.empty()){
        Node t = Q.top();Q.pop();
        if(mat[t.x][t.y]=='^' && t.step < k) return t.step;
        for(int d=0;d<4;d++){
            s = t;
            s.x = s.x + dir[d][0];
            s.y = s.y + dir[d][1];
            if(s.x >= 0 && s.x < n && s.y >= 0 && s.y < m && mat[s.x][s.y]!='*'&& !visit[s.x][s.y][s.key]){
                if(mat[s.x][s.y]>='A'&&mat[s.x][s.y]<='J'){
                    int key = 1<<(mat[s.x][s.y]-'A');
                    if(s.key & key){
                        s.step+=1;
                        visit[s.x][s.y][s.key] = true;
                        if(s.step < k)
                        Q.push(s);
                    }
                }else if(mat[s.x][s.y]>='a' && mat[s.x][s.y]<='j'){
                    int key = 1<<(mat[s.x][s.y]-'a');
                    s.key=s.key|key;
                    s.step+=1;
                    visit[s.x][s.y][s.key] = true;
                    if(s.step < k)
                    Q.push(s);
                }else {
                    s.step+=1;
                    visit[s.x][s.y][s.key] = true;
                    if(s.step < k)
                    Q.push(s);
                }
            }
        }
    }
    return -1;
}
void Input_data(){
    for(int i=0;i<n;i++){
        scanf("%s",mat[i]);
        for(int j=0;j<m;j++)
            if(mat[i][j] == '@')
            start.x =i,start.y = j,start.step =start.key = 0;
    }

}
int main(){
    while(scanf("%d %d %d",&n,&m,&k)==3){
        Input_data();
        printf("%d
",BFS());
    }
}

 

原文地址:https://www.cnblogs.com/Pretty9/p/7347697.html