胜利大逃亡(续)

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. poj 2506 Tiling 递推

    题目链接: http://poj.org/problem?id=2506 题目描述: 有2*1和2*2两种瓷片,问铺成2*n的图形有多少种方法? 解题思路: 利用递推思想,2*n可以由2*(n-1)的 ...

  3. Chtholly Tree (珂朵莉树) ODT

    ODT,OldDriverTree,又名ChthollyTree" role="presentation" style="position: relative; ...

  4. 字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles

    题目传送门 /* 题意:给出一系列名字变化,问最后初始的名字变成了什么 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 在每一次更新时都把初始pos加上 ...

  5. dubbo服务端响应超时错误一例记录

    错误描述: Portlet J2AppsPortlet::QuickStartPortlet not available: Waiting server-side response timeout. ...

  6. JSON(2)JSONObject解析Josn和创建Jsonf示例

    1.解析Json /* * test.josn内容如下: { "languages":[ {"id":"1","name" ...

  7. C# 基础知识和VS2010的小技巧总汇

    看了一些基础视频,才发现自己的基础比较薄弱,有很多基础知识都不知道.这里总汇一些基础知识. 1: foreach不仅可以作用于list类的索引集合,还可以遍历dictionary类,这一点比for更简 ...

  8. SpringCloud+MyBatis+Redis整合—— 超详细实例(二)

    2.SpringCloud+MyBatis+Redis redis①是一种nosql数据库,以键值对<key,value>的形式存储数据,其速度相比于MySQL之类的数据库,相当于内存读写 ...

  9. python_数据类型基本操作(2)

    概览: 第1章 基础数据类型宏观的初识第2章 int 第3章 bool 第4章 str 4.1 python体现形式 4.2 引号用法 4.3 字符串运算 4.3.1 字符串相加 4.3.2 字符串相 ...

  10. yum 安装报错:*epel: mirrors.aliyun.comError: xzcompressionnot available

    环境背景:epel源下载地址: http://mirrors.aliyun.com/Centos内核内核版本[root@nfs01 ~]# uname -r2.6.32-642.el6.x86_64= ...