胜利大逃亡(续)

Time Limit: 2000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 1429
64-bit integer IO format: %I64d      Java class name: Main

 
 
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

Source

 
解题:状态压缩搜索。。。。高级搜索?
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
struct node{
int state,step;
node(int x = ,int y = ):state(x),step(y){}
};
char mp[][];
int n,m,t,sx,sy,ex,ey;
const int dir[][] = {,-,,,-,,,};
queue<node>q;
set<int>s;
int compress(int x,int y,int state){
int temp = x;
temp = (temp<<)|y;
temp = (temp<<)|state;
return temp;
}
void decompress(int temp,int &x,int &y,int &state){
int t = (<<)-;
state = temp&t;
temp >>= ;
t = (<<)-;
y = temp&t;
temp >>= ;
x = temp&t;
}
void addkey(int &state,int t){
state |= (<<t);
}
int haskey(int state,int t){
return state&(<<t);
}
int bfs(){
while(!q.empty()) q.pop();
s.clear();
int i,j,x,y,tx,ty,tmpstate,tmp;
tmp = compress(sx,sy,);
node temp2 = node(tmp,);
q.push(temp2);
s.insert(tmp);
while(!q.empty()){
node temp2 = q.front();
q.pop();
if(temp2.step > t) return INF;//必须要的优化
for(i = ; i < ; i++){
decompress(temp2.state,x,y,tmpstate);
if(x == ex && y == ey) return temp2.step;
tx = x+dir[i][];
ty = y+dir[i][];
if(mp[tx][ty] == '*') continue;
if(mp[tx][ty] >= 'a' && mp[tx][ty] <= 'j'){
addkey(tmpstate,mp[tx][ty]-'a');
}else if(mp[tx][ty] >= 'A' && mp[tx][ty] <= 'J'){
if(!haskey(tmpstate,mp[tx][ty]-'A')) continue;
}
tmp = compress(tx,ty,tmpstate);
if(s.count(tmp)) continue;
s.insert(tmp);
q.push(node(tmp,temp2.step+));
}
}
return INF;
}
int main(){
while(~scanf("%d %d %d",&n,&m,&t)){
memset(mp,'*',sizeof(mp));
getchar();
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
mp[i][j] = getchar();
if(mp[i][j] == '@') {sx = i;sy = j;}
if(mp[i][j] == '^') {ex = i;ey = j;}
}
getchar();
}
int tmp = bfs();
printf("%d\n",tmp < t?tmp:-);
}
return ;
}

BNUOJ 5629 胜利大逃亡(续)的更多相关文章

  1. hdu.1429.胜利大逃亡(续)(bfs + 0101011110)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  2. hdu 1429 胜利大逃亡(续)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王 ...

  3. Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  4. HDOJ 1429 胜利大逃亡(续)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  5. hdu 1429 胜利大逃亡(续)(bfs+位压缩)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  6. HDU 1429 胜利大逃亡(续)(DP + 状态压缩)

    胜利大逃亡(续) Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢 ...

  7. 胜利大逃亡(续)(状态压缩bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  8. 胜利大逃亡(续)(bfs+状态压缩)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  9. 胜利大逃亡(续)hdu1429(bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. 数据结构 - 链队列的实行(C语言)

    数据结构-链队列的实现 1 链队列的定义 队列的链式存储结构,其实就是线性表的单链表,只不过它只能尾进头出而已, 我们把它简称为链队列.为了操作上的方便,我们将队头指针指向链队列的头结点,而队尾指针指 ...

  2. 数据结构 - 顺序队列的实行(C语言)

    数据结构-顺序队列的实现 1 顺序队列的定义 线性表有顺序存储和链式存储,队列作为一种特殊的线性表,也同样存在这两种存储方式.我们先来看队列的顺序存储结构. 队列的顺序储存结构:用数组存储队列,为了避 ...

  3. Ubuntu install and uinstall

    一.Ubuntu中软件安装方法 1.APT方式 (1)普通安装:apt-get install softname1 softname2 -; (2)修复安装:apt-get -f install so ...

  4. 科普 eclipse中的Java build

    在刚学eclipse的时候,build path是经常会用到的,但经常就是跟着教程走,额就不太懂这是干嘛的,然后今天看见极客视频里有相关的讲解,来记录一下. Build Path 是指定Java工程所 ...

  5. 230 Kth Smallest Element in a BST 二叉搜索树中第K小的元素

    给定一个二叉搜索树,编写一个函数kthSmallest来查找其中第k个最小的元素. 注意:你可以假设k总是有效的,1≤ k ≤二叉搜索树元素个数. 进阶:如果经常修改二叉搜索树(插入/删除操作)并且你 ...

  6. Oracle本地动态 SQL

    本地动态 SQL 首先我们应该了解什么是动态 SQL,在 Oracle数据库开发 PL/SQL块中我们使用的 SQL 分为:静态 SQL语句和动态 SQL语句.所谓静态 SQL指在 PL/SQL块中使 ...

  7. P1603 斯诺登的密码

    题目背景 根据斯诺登事件出的一道水题 题目描述 题目描述 2013年X月X日,俄罗斯办理了斯诺登的护照,于是他混迹于一架开往委内瑞拉的飞机.但是,这件事情太不周密了,因为FBI的间谍早已获悉他的具体位 ...

  8. HTML标签,简单归纳

    列表标签 有序列表: <ol><li></li></ol> 无序列表: <ul><li></li></ul&g ...

  9. Spring data jpa中Query和@Query分别返回map结果集

    引用: http://blog.csdn.net/yingxiake/article/details/51016234 http://blog.csdn.net/yingxiake/article/d ...

  10. SQL 索引自动维护计划脚本

    脚本功能: 1,查询数据库中,碎片率在5%以上(官方推荐),有一定数据里的表的索引. 2.如果碎片率在5%<碎片率<=30%  执行重新组织索引.如果在30%以上,执行重建索引 建议在执行 ...