胜利大逃亡(续)

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. 为HttpClient和HttpURLConnection添加中国移动代理

    转自: http://www.2cto.com/kf/201111/112100.html 在android中,一般需要联网的时候前,都要做一次网络的判断,判断当前的网络状态!然后开始请求网络 当我们 ...

  2. GUID的学习

    GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成GUID的API.生成算法很有意思,用到了以太网卡地址.纳秒级时间.芯片ID码和许多可 ...

  3. iOS 实现复选框 checkbox --转

    转自:http://www.cnblogs.com/ygm900/p/3468891.html  -(void)checkboxClick:(UIButton *)btn{    btn.select ...

  4. E. Comments dfs模拟

    http://codeforces.com/contest/747/problem/E 首先,把字符串变成这个样子. hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0 ...

  5. sdut2784&cf 126b Good Luck!(next数组)

    链接 next数组的巧妙应用  学弟出给学弟的学弟的题.. 求最长的 是前缀也是后缀同时也是中缀的串  next的数组求的就是最长的前后缀 但是却不能求得中缀 所以这里 就把尾部去掉之后再求 这样就可 ...

  6. 窗体WINFORM

    窗体的事件:删除事件:先将事件页面里面的挂好的事件删除,再删后台代码里面的事件 Panel是一个容器 1.Label -- 文本显示工具Text:显示的文字取值.赋值:lable1.Text 2.Te ...

  7. ssm基础配置

    1.导包 <dependencies> <dependency> <groupId>org.springframework</groupId> < ...

  8. leetcode:single-number-ii(Java位运算)

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  9. iOS---iOS中SQLite的使用

    一.SQLite的使用 采用SQLite数据库来存储数据.SQLite作为一中小型数据库,应用ios中,跟前三种保存方式相比,相对比较复杂一些.还是一步步来吧! 第一步:导入头文件 需要添加SQLit ...

  10. 调试SQL Server的存储过程及用户定义函数

    分类: 数据库管理 2005-06-03 13:57 9837人阅读 评论(5) 收藏 举报 sql server存储vb.net服务器sql语言 1.在查询分析器中调试 查询分析器中调试的步骤如下: ...