Problem Description
Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……

这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方。刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个。魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去。经过若干次的尝试,Ignatius已画出整个地牢的地图。现在请你帮他计算能否再次成功逃亡。只要在魔王下次视察之前走到出口就算离开地牢,如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。
Input
每组测试数据的第一行有三个整数n,m,t(<=n,m<=,t>)。接下来的n行m列为地牢的地图,其中包括:

. 代表路
* 代表墙
@ 代表Ignatius的起始位置
^ 代表地牢的出口
A-J 代表带锁的门,对应的钥匙分别为a-j
a-j 代表钥匙,对应的门分别为A-J 每组测试数据之间有一个空行。
 
Output
针对每组测试数据,如果可以成功逃亡,请输出需要多少分钟才能离开,如果不能则输出-。
 
Sample Input
@A.B.
a*.*.
*..*^
c..b* @A.B.
a*.*.
*..*^
c..b*
 
Sample Output
-
 
Source
 
 

 第46行,if((t2.key&(1<<j))==0){//写成 t2.key&(1<<j)==0竟然一直错,&#%**&*!@#@#@!@! 

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N 26
#define M (1<<10)+6
int n,m,t;
char mp[N][N];
struct Node{
int x,y;
int t;
int key;
}st,ed;
int dirx[]={,,-,};
int diry[]={-,,,};
char keys[]={'a','b','c','d','e','f','g','h','i','j'};
char door[]={'A','B','C','D','E','F','G','H','I','J'};
int vis[N][N][M];
void bfs(){
queue<Node>q;
q.push(st);
vis[st.x][st.y][st.key]=;
Node t1,t2;
while(!q.empty()){
t1=q.front();
//printf("*** %d%d %d\n",t1.x,t1.y,t1.key);
q.pop();
if(t1.x==ed.x && t1.y==ed.y && t1.t<t){
printf("%d\n",t1.t);
return;
}
for(int i=;i<;i++){
t2=t1;
t2.x=t1.x+dirx[i];
t2.y=t1.y+diry[i];
if(mp[t2.x][t2.y]=='*') continue;
if(t2.x< || t2.x>=n || t2.y< || t2.y>=m) continue;
if(vis[t2.x][t2.y][t2.key]) continue;
if(islower(mp[t2.x][t2.y])){
for(int j=;j<;j++){
if(mp[t2.x][t2.y]==keys[j]){ if((t2.key&(<<j))==){//写成 t2.key&(1<<j)==0竟然一直错,&#%**&*!@#@#@!@!
t2.key+=(<<j);
t2.t++;
vis[t2.x][t2.y][t2.key]=;
q.push(t2);
}
else{
//printf("+++%d\n",t2.key);
if(vis[t2.x][t2.y][t2.key]==){
t2.t++;
vis[t2.x][t2.y][t2.key]=;
q.push(t2);
}
}
}
}
}
else if(isupper(mp[t2.x][t2.y])){
for(int j=;j<;j++){
if(mp[t2.x][t2.y]==door[j]){
if(t2.key&(<<j)){
t2.t++;
vis[t2.x][t2.y][t2.key]=;
q.push(t2);
}
}
}
}
else{
t2.t++;
vis[t2.x][t2.y][t2.key]=;
q.push(t2);
} }
}
printf("-1\n");
}
int main()
{
while(scanf("%d%d%d",&n,&m,&t)==){
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<m;j++){
if(mp[i][j]=='@'){
st.x=i;
st.y=j;
st.t=;
st.key=;
}
if(mp[i][j]=='^'){
ed.x=i;
ed.y=j;
}
}
} memset(vis,,sizeof(vis));
bfs();
}
return ;
}

hdu 1429 胜利大逃亡(续)(bfs+状态压缩)的更多相关文章

  1. hdu - 1429 胜利大逃亡(续) (bfs状态压缩)

    http://acm.hdu.edu.cn/showproblem.php?pid=1429 终于开始能够做状态压缩的题了,虽然这只是状态压缩里面一道很简单的题. 状态压缩就是用二进制的思想来表示状态 ...

  2. hdu 1429 胜利大逃亡(续) (bfs+状态压缩)

    又开始刷题了 题意:略过. 分析:主要是确定状态量,除了坐标(x,y)之外,还有一个key状态,就好比手上拿着一串钥匙.状态可以用位运算来表示:key&(x,y)表示判断有没有这扇门的钥匙,k ...

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

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

  4. HDOJ 1429 胜利大逃亡(续) (bfs+状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 思路分析:题目要求找出最短的逃亡路径,但是与一般的问题不同,该问题增加了门与钥匙约束条件: 考虑 ...

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

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

  6. HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)  ...

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

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

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

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

  9. HDU 1429 胜利大逃亡(续)(bfs)

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

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

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

随机推荐

  1. uiautomatorviewer 识别android微信元素报错

    org.xml.sax.SAXParseException; systemId: file:/C:/Users/xxxxxxxxx/AppData/Local/Temp/uiautomatorview ...

  2. (转)iOS Wow体验 - 第二章 - iOS用户体验解析(1)

    本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第二章译文精选的第一部分,其余章节将陆续放出.上一 ...

  3. FZU-1925+几何

    题意简单. 由于没有注意到椭圆不一定是在圆心..贡献无数的wa..... #include<stdio.h> #include<string.h> #include<al ...

  4. Oracle442个应用场景------------基础应用场景

    /////////////////基础知识////////////////// 应用场景178:最简单的select语句 SELECT * FROM Employees; 应用场景179:指定要查询的 ...

  5. QT5 r 加入qwtplot3d 三维库

          qwtplot3d是基于QtOpenGL开发的,也是qwt库的三维库,我使用的是qwtplot3d-0.2.7.zip版本.   步骤跟编译qwt库一样(不明白可以看回前面写的一篇文章“Q ...

  6. Dynamics CRM 常用 C# 方法集合

    Plugin(C#) 分派 AssignRequest assign = new AssignRequest(); assign.Assignee = prEntity["ownerid&q ...

  7. Curl命令使用方法

    Curl是Linux下一个很强大的http命令行工具,其功能十分强大.1) 读取网页$ curl http://www.linuxidc.com2) 保存网页$ curl http://www.lin ...

  8. HTML与CSS入门——第十二章  在网页中使用多媒体

    知识点: 1.如何链接多媒体文件 2.如何嵌入多媒体文件 3.使用多媒体的更多技巧 多媒体文件:音频,视频和动画,以及静态的图像和文本. 这里我就直接讲HTML5了…… 此前都是用ojbect来加载或 ...

  9. ajax例子

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  10. Cocos2d-x 3.0 场景切换

    场景切换要用到导演类Director,一般有两种方式,大多数是用替换场景(replaceScene),也可以用进栈(pushScene)出栈(popScene)的方式进行场景的替换. 场景切换代码: ...