HDU 1429 胜利大逃亡(续)

胜利大逃亡(续)

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


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
linle
 
思路:BFS + 位压缩;BFS 部分很简单,但是由于加入了钥匙
所以在搜索的过程中间,有些情况可以使搜索路径返回,就是当
找到一个钥匙的时候,这里最多有十把钥匙,所以使用位压缩;
所谓位压缩:就是用一个整数来保存几个事物的有无状态。我们
知道每个整数对应一个二进制,二进制就是01串,而0对应的就是无,
1对应有,这样几个事物的有无就可以确定一个二进制串,进而可
以确定一个整数。故用整数保存几个事物的有无状态。假设
已知一个整数m,要判断事物n(n是编号,从1开始)是否存在,
可以进行&运算:m&1<<n-1,如果运算结果为0,则n不存在,
如果运算结果非0,则n已存在。如果n不存在,要把n加入,可
以进行 |  运算:m | 1<<n-1,可以用运算结果来记录这个新
状态。
这题好坑啊
 
代码;
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
int n,m,t_monster;
int hash[25][25][2050];
char map[25][25];
int the_first_x,the_first_y;
int the_end_x,the_end_y;
int the_last_time;
int the_last_flag;
int move[4][2] = {1,0,-1,0,0,1,0,-1};
struct Node
{
    int x,y,time;
    int key;
};
void BFS()
{
    queue <Node> q;
    Node top;top.x = the_first_x;top.y = the_first_y;
    top.time = 0;top.key = 0;
    q.push(top);
    hash[0][0][0] = 1;
    while(!q.empty())
    {
        Node temp = q.front();q.pop();
        if(temp.x == the_end_x && temp.y == the_end_y)
        {
            the_last_time = temp.time;
            the_last_flag = 1;
            return ;
        }
        for(int i = 0;i < 4;i ++)
        {
            int x = temp.x + move[i][0];int y = temp.y + move[i][1];
            int time = temp.time + 1;int key = temp.key;
            if(x >= 0 && x < n && y >= 0 && y < m &&
               hash[x][y][key] == 0 && map[x][y] != '*')
             {
                    if((map[x][y] == '.' || map[x][y] == '^'))
                    {
                        hash[x][y][key] = 1;
                        Node xin;xin.x = x;xin.y = y;
                        xin.time = time;xin.key = key;
                        q.push(xin);
                    }
                    else if(map[x][y] <= 'j' && map[x][y] >= 'a')
                    {
                        hash[x][y][key] = 1;
                        key = key | 1 << map[x][y] - 'a';
                        Node xin;xin.x = x;xin.y = y;
                        xin.time = time;xin.key = key;
                        q.push(xin);
                        hash[x][y][key] = 1;
                    }
                    else if(map[x][y] >= 'A' && map[x][y] <= 'J' && (key & 1 << map[x][y] - 'A'))
                    {
                        hash[x][y][key] = 1;
                        Node xin;xin.x = x;xin.y = y;
                        xin.time = time;
                        xin.key = key;
                        q.push(xin);
                    }
             }
        }
    }
    return ;
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&t_monster))
    {
        memset(map,0,sizeof(map));
        memset(hash,0,sizeof(hash));
        for(int i = 0;i < n;i ++)
        {
            scanf("%s",map[i]);
            for(int j = 0;j < m;j ++)
            {
                if(map[i][j] == '@')
                {
                    the_first_x = i;
                    the_first_y = j;
                    map[i][j] = '.';
                }
                if(map[i][j] == '^')
                {
                    the_end_x = i;
                    the_end_y = j;
                }
            }
        }
        the_last_flag = 0;the_last_time = 0;
        BFS();
        if(the_last_flag == 0 || the_last_time >= t_monster)
            printf("-1\n");
        else
            printf("%d\n",the_last_time);
        //printf("\n");
    }
    return 0;
}

 

posted on 2013-10-07 20:45  天使是一个善良的神  阅读(159)  评论(0编辑  收藏  举报

导航