HDU - 1429 胜利大逃亡(续)

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

这题主要是就到达一个门要确定有没有钥匙,用一个二进制数表示当前这个点有哪几把钥匙,比如0011表示有1,2两把钥匙(a,b),如果当前这个点是门,则用&,比如0011&0010=0010说明当前这扇门能打开,能走。
如果当前这个点是钥匙,则用|,比如0011|1000=1011 说明到当前这个点找到了1,2,4这几把钥匙
1<<s[i][j]-'a',表示将1左移x位,对应第x把钥匙

记录vis的时候也就要对应的进行改变,到达一个位置拥有不同数量种类的钥匙是不同的状态。

#include <iostream>
#include <string>
#include <string.h>
#include <queue>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn=25;
const int INF=0x3f3f3f3f;
int n,m,t;
int sx,sy,ex,ey;
char s[maxn][maxn];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int vis[maxn][maxn][2000];
struct Node
{
    int x,y,step;
    int k;
};
Node now,temp,nextt;
queue<Node>q;

void bfs()
{
    now.x=sx;now.y=sy;now.step=0;now.k=0;
    vis[now.x][now.y][now.k]=1;
    q.push(now);
    while(!q.empty())
    {
        temp=q.front();
        q.pop();
        if(temp.x==ex&&temp.y==ey)
        {
            if(temp.step<t)
            {
                cout<<temp.step<<endl;
                return ;
            }
            else
            {
                cout<<"-1"<<endl;
                return ;
            }
        }
        if(temp.step>=t)
        {
            cout<<"-1"<<endl;
            return ;
        }
        for(int i=0;i<4;i++)
        {
            nextt.x=temp.x+dx[i];
            nextt.y=temp.y+dy[i];
            if(nextt.x>=1&&nextt.x<=n&&nextt.y>=1&&nextt.y<=m&&s[nextt.x][nextt.y]!='*')
            {
                nextt.step=temp.step+1;
                nextt.k=temp.k;
                if(s[nextt.x][nextt.y]>='a'&&s[nextt.x][nextt.y]<='z')
                {
                    nextt.k=temp.k|(1<<(s[nextt.x][nextt.y]-'a'));
                    if(vis[nextt.x][nextt.y][nextt.k]==0)
                    {
                        vis[nextt.x][nextt.y][nextt.k]=1;
                        q.push(nextt);
                    }
                }
                else if(s[nextt.x][nextt.y]>='A'&&s[nextt.x][nextt.y]<='Z')
                {
                    int kk=temp.k&(1<<(s[nextt.x][nextt.y]-'A'));
                    if(vis[nextt.x][nextt.y][temp.k]==0&&kk)
                    {
                        vis[nextt.x][nextt.y][temp.k]=1;
                        q.push(nextt);
                    }
                }
                else
                {
                    if(vis[nextt.x][nextt.y][nextt.k]==0)
                    {
                        vis[nextt.x][nextt.y][nextt.k]=1;
                        q.push(nextt);
                    }
                }
            }
        }
    }
    cout<<"-1"<<endl;
}

int main()
{
    while(cin>>n>>m>>t)
    {
        while(!q.empty())
            q.pop();
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>s[i][j];
                if(s[i][j]=='@')
                {
                    sx=i;sy=j;
                }
                if(s[i][j]=='^')
                {
                    ex=i;ey=j;
                }
            }
        }
        bfs();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Abandoninged/article/details/81288588