BNUOJ 5629 胜利大逃亡(续)
胜利大逃亡(续)
This problem will be judged on HDU. Original ID: 1429
64-bit integer IO format: %I64d Java class name: Main
这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方。刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个。魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去。经过若干次的尝试,Ignatius已画出整个地牢的地图。现在请你帮他计算能否再次成功逃亡。只要在魔王下次视察之前走到出口就算离开地牢,如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。
Input
. 代表路
* 代表墙
@ 代表Ignatius的起始位置
^ 代表地牢的出口
A-J 代表带锁的门,对应的钥匙分别为a-j
a-j 代表钥匙,对应的门分别为A-J
每组测试数据之间有一个空行。
Output
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 胜利大逃亡(续)的更多相关文章
- hdu.1429.胜利大逃亡(续)(bfs + 0101011110)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 1429 胜利大逃亡(续)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王 ...
- 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 ...
- HDOJ 1429 胜利大逃亡(续)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu 1429 胜利大逃亡(续)(bfs+位压缩)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU 1429 胜利大逃亡(续)(DP + 状态压缩)
胜利大逃亡(续) Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢 ...
- 胜利大逃亡(续)(状态压缩bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 胜利大逃亡(续)(bfs+状态压缩)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 胜利大逃亡(续)hdu1429(bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- Hdu 5379 Mahjong tree (dfs + 组合数)
题目链接: Hdu 5379 Mahjong tree 题目描述: 给出一个有n个节点的树,以节点1为根节点.问在满足兄弟节点连续 以及 子树包含节点连续 的条件下,有多少种编号方案给树上的n个点编号 ...
- Looper、MessageQueue、Message、Handler的关系
1.快速复习 1.1 基本装置 类 装置名 作用 线程中数量 Looper 消息分发装置 从消息队列中取出一个消息,交给对应的Handler处理消息. 1 MessageQueue 消息队列 保存所有 ...
- [转]查询表达式 (F#)
本文转自:http://msdn.microsoft.com/zh-cn/library/hh225374.aspx 查询表达式可以查询数据源并将数据是一种预期形式. 查询表达 ...
- Toast解析
课程Demo public class MainActivity extends AppCompatActivity { private Button bt1; private Button bt2; ...
- Sql Server 2012 事务复制遇到的问题及解决方式
1.订阅服务器提示:作业失败.无法确定所有者 WIN-01Q6JB46CHV\Administrator(拥有作业XXX)是否有服务器访问权限(原因:无法获取有关 Windows NT 组/用户'WI ...
- Node.js——优先从缓存加载
main中执行require操作,目的是获取接口对象,所以多次引用b,并不会重复执行模块内部的输入输出,因为缓存中已经存在
- Friday Q&A 2015-11-20:协变与逆变
作者:Mike Ash,原文链接,原文日期:2015-11-20译者:Cee:校对:千叶知风:定稿:numbbbbb 在现代的编程语言中,子类型(Subtypes)和超类型(Supertypes)已经 ...
- 正则表达式,匹配查找函数(preg_match_all)flags参数对比
格式: int preg_match_all ( string pattern, string subject, array matches [, int flags] ) 参数 flags 选项有以 ...
- CAD使用GetxDataDouble读数据(com接口)
主要用到函数说明: MxDrawEntity::GetxDataDouble2 读取一个Double扩展数据,详细说明如下: 参数 说明 [in] LONG lItem 该值所在位置 [out, re ...
- 散列的键值对没初始化时不要用print打印此值,不要用 . 操作符去连接打印 这个值。
31 delete $vertical_alignment{$anonymous}; 32 print $vertical_alignment{$anonymous}."\n&quo ...