HDU1429

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

 

题意:给定起点和终点,路上会遇到大门和钥匙,只有和大门相对应的钥匙才能打开大门,问Ignatius能否逃脱地牢

思路:钥匙最多只有10把(a到j),所以可以用一串二进制位来表示,初始状态为00000000001,例如持有钥匙a时表示为00000000011,每一位代表是否拥有钥匙的状态;遇到对应的大门时进行与操作,遇到钥匙时进行或操作,例如从初始状态出发,遇到钥匙a,为01 | 10=11;遇到大门A,01 & 10 = 0,结果为0说明不能打开大门,反之可以打开。

这题让我纠结的是不知道如何使用bfs(钥匙和门怎么处理的问题,因为要先拿到钥匙才能打开门,钥匙和门在不同的位置),所以关键还是如何处理钥匙、大门、位置的关系。

看了题解之后,发现不用太纠结钥匙和门,简单说一下理解。

设置了一个三维数组int vis[MAX][MAX][1200]:vis[i][j][k]表示在位置(i,j)持有钥匙k,如果值为0表示不能通过,为1表示可以通过

这样,从起始点出发,遇到一个位置判断三种情况:大门,钥匙,通道。

大门:利用与操作,判断能否打开这扇门,可以的话将该点加入队列中,更新该数组,继续bfs,否则就停止

钥匙:拾取钥匙,(也就是进行或操作),更新该数组

通道:更新该数组,继续bfs

 

#include<cstdio>
#include<string.h>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<set>
#include<algorithm>
using namespace std;
#define INF 1<<28
static const int MAX = 25;
int n, m, t;
char G[MAX][MAX];
int vis[MAX][MAX][1200]; //用二进制位表示钥匙的状态(a-j有10把钥匙,至少要1024个)
        //vis[i][j][k]表示在位置(i,j)持有钥匙k,如果值为0表示不能通过,为1表示可以通过
int sx, sy; //起始位置
int ex, ey; //地牢出口

struct node{
    int x, y;
    int step;
    int key;    //用一个二进制数表示得到的钥匙
};

void bfs()
{
    int dx[4] = {-1, 1, 0, 0};
    int dy[4] = {0, 0, -1, 1};
    node now, temp, next;
    now.x = sx; now.y = sy; now.step = 0; now.key = 0;
    vis[now.x][now.y][now.key] = 1;
    queue<node> Q;
    Q.push(now);

    while (!Q.empty())
    {
        temp = Q.front(); Q.pop();

        if (temp.x == ex && temp.y == ey)
        {
            if (temp.step < t)
                printf("%d\n", temp.step);
            else
                printf("-1\n");
            return;
        }

        if (temp.step >= t)
        {
            printf("-1\n");
            return;
        }

        for (int i = 0; i < 4; i++)
        {
            next.x = temp.x + dx[i];
            next.y = temp.y + dy[i];

            if (next.x < 0 || next.x >= n || next.y < 0 || next.y >= m)
                continue;
            if (G[next.x][next.y] == '*')
                continue;

            next.step = temp.step + 1;
            next.key = temp.key;

            if (G[next.x][next.y] >= 'A' && G[next.x][next.y] <= 'Z')
            {
                int k = temp.key & (1 << (G[next.x][next.y] - 'A'));
                if (vis[next.x][next.y][temp.key] == 0 && k)
                {
                    vis[next.x][next.y][temp.key] = 1;
                    Q.push(next);
                }
            }
            else if (G[next.x][next.y] >= 'a' && G[next.x][next.y] <= 'z')
            {
                next.key = next.key | (1 << (G[next.x][next.y] - 'a'));
                if (vis[next.x][next.y][temp.key] == 0)
                {
                    vis[next.x][next.y][temp.key] = 1;
                    Q.push(next);
                }
            }
            else    //G[next.x][next.y] = '.'
            {
                if (vis[next.x][next.y][temp.key] == 0)
                {
                    vis[next.x][next.y][temp.key] = 1;
                    Q.push(next);
                }
            }
        }
    }
    printf("-1\n");
    return;
}

int main()
{
    while (scanf("%d %d %d", &n, &m, &t) != EOF)
    {
        memset(vis, 0, sizeof(vis));

        for (int i = 0; i < n; i++)
            scanf("%s", G[i]);

        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (G[i][j] == '@')
                {
                    sx = i; sy = j;
                }
                else if (G[i][j] == '^')
                {
                    ex = i; ey = j;
                }
        bfs();
    }
    return 0;
}





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值