胜利大逃亡(续)

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7346    Accepted Submission(s): 2546

Problem Description
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

结构体里开一个key变量,用于记录是否找到钥匙,这里用到位压缩,节省时间和内存,当遇到钥匙是用|运算即可记录下找到的是第几把钥匙(例如第一把钥匙a 字符a减去'a'时值为0,用|运算将1左移0位,此时二进制位0001表示有第一把钥匙,(1110表示有第四、三、二把钥匙)),当遇到门时,再与门&此时遇到对应门时&之后不为0,否则为0

#include<stdio.h>
#include<string.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD double
#define MAX 21
#define mod 10003
#define dian 1.000000011
#define INF 0x3f3f3f
using namespace std;
int head[MAX],ans;
int n,m,t;
char s[MAX][MAX];
int vis[MAX][MAX][1<<10];
struct node
{
int x,y,key,time;
// friend bool operator< (node a,node b)
// {
// return a.time>b.time;
// }
};
int Move[4][2]={1,0,-1,0,0,1,0,-1};
int judge(int a,int b)
{
if(a>=0 && a<n && b>=0 && b<m && s[a][b]!='*')
return 1;
return 0;
}
int bfs(int x1,int y1)
{
int i;
//priority_queue<node>q;
queue<node>q;
memset(vis,0,sizeof(vis));
while(!q.empty())
q.pop();
node beg,end;
beg.x=x1;
beg.y=y1;
beg.time=0;
beg.key=0;
q.push(beg);
vis[beg.x][beg.y][beg.key]=1;//标记当前位置当前钥匙是否用过
while(!q.empty())
{
//beg=q.top();
beg=q.front();
q.pop();
if(s[beg.x][beg.y]=='^')
{
ans=beg.time;
return 1;
}
if(ans>t) continue;//超过时间了就要找新的路径
for(i=0;i<4;i++)
{
end.x=beg.x+Move[i][0];
end.y=beg.y+Move[i][1];
end.time=beg.time+1;
if(judge(end.x,end.y))
{
if(s[end.x][end.y]>='a'&&s[end.x][end.y]<='j')//找到钥匙
{
end.key=beg.key|(1<<(s[end.x][end.y]-'a'));//记录钥匙
if(!vis[end.x][end.y][end.key])//此状态之前是否走过
{
vis[end.x][end.y][end.key]=1;
q.push(end);
}
}
else if(s[end.x][end.y]>='A'&&s[end.x][end.y]<='J')//找到门
{
end.key=beg.key;
if(end.key&(1<<(s[end.x][end.y]-'A')))
{
if(!vis[end.x][end.y][end.key])
{
vis[end.x][end.y][end.key]=1;
q.push(end);
}
}
}
else//找到路
{
end.key=beg.key;
if(!vis[end.x][end.y][end.key])
{
vis[end.x][end.y][end.key]=1;
q.push(end);
}
}
}
}
}
return 0;
}
int main()
{
int j,i;
int x1,y1;
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
ans=0;
for(i=0;i<n;i++)
scanf("%s",s[i]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(s[i][j]=='@')
{
x1=i;
y1=j;
}
}
}
if(bfs(x1,y1)&&ans<t)
printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}

  

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 + 0101011110)

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

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

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

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

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

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

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

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

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

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

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

  9. 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 ...

随机推荐

  1. 1641. Duties

    1641 枚举 #include <iostream> #include<cstdio> #include<cstring> #include<algorit ...

  2. WebActivatorEx

    using System; using NLog; using System.Web.Optimization; [assembly: WebActivatorEx.PreApplicationSta ...

  3. How to delete a team project from Team Foundation Service (tfs.visualstudio.com)

    C:\project>tfsdeleteproject /collection:https://buckh-test2.visualstudio.com/DefaultCollection Te ...

  4. POJ:最长上升子序列

    Title: http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2  ...

  5. 如何合并IP网段

    1. 安装IPy pip3 install IPy 2. 写脚本: yuyue@workplace:~ $ cat combine_ip.pyfrom IPy import IPSet, IPimpo ...

  6. PIG的配置

    Pig是一个客户端应用程序,就算你要在Hadoop集群上运行Pig,也不需要在集群上装额外的东西.Pig的配置非常简单: 1.下载pig,网址http://pig.apache.org/ 2.在机器上 ...

  7. 《零成本实现Web自动化测试--基于Selenium》第二章 Selenium简介和基础

    第一部分 Selenium简介 1.Selenium 组建 1.1 Selenium-IDE Selenium-IDC是开发Selenium测试案例的集成开发环境.它像FireFox插件一样的工作,支 ...

  8. JMeter重要知识点汇总

    1)现在对于JMeter来说,一个测试计划只能有一个cookie管理器.因为当多个manager存在时,JMeter目前还没有方法来指定使用哪个manager.同时,一个cookie manager中 ...

  9. mapreduce优化总结

    集群的优化 1.合理分配map和reduce任务的数量(单个节点上map任务.reduce任务的最大数量) 2.其他配置 io.file.buffer.size hadoop访问文件的IO操作都需要通 ...

  10. bzoj 2595 [Wc2008]游览计划(斯坦纳树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2595 [题意] 给定N*M的长方形,选最少权值和的格子使得要求的K个点连通. [科普] ...