题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。因为这里我贡献了一次wa。

分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口,对钥匙进行状态压缩,具体看代码实现!

代码实现:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std; int n,m,time,visited[][][];
int sx,sy,ex,ey,res;
int b[][]={{,},{,-},{,},{-,}};
char map[][]; struct node{
int x;
int y;
int t;
int st;
}; int check(int x,int y)
{
if(x>=&&x<=n&&y>=&&y<=m&&map[x][y]!='*')
return ;
else
return ;
} void bfs()
{
queue<node>Q;
struct node p,temp;
int i,j,add;
memset(visited,,sizeof(visited));
p.x=sx;p.y=sy;
p.t=;p.st=;
visited[sx][sy][]=;
Q.push(p);
while(!Q.empty())
{
p=Q.front();
Q.pop();
if(p.x==ex&&p.y==ey&&p.t<time)//这里要注意下,为此贡献了一次wa
{
res=p.t;
return ;
}
if(p.t>=time)
return ;
for(i=;i<;i++)
{
temp.x=p.x+b[i][];
temp.y=p.y+b[i][];
temp.t=p.t+;
temp.st=p.st;
if(check(temp.x,temp.y))
{
if(map[temp.x][temp.y]>='a'&&map[temp.x][temp.y]<='j')
{
add=<<(map[temp.x][temp.y]-'a');
temp.st=temp.st|add;
if(visited[temp.x][temp.y][temp.st]==)
{
visited[temp.x][temp.y][temp.st]=;
Q.push(temp);
}
}
else if(map[temp.x][temp.y]>='A'&&map[temp.x][temp.y]<='J')
{
add=<<(map[temp.x][temp.y]-'A');
if((temp.st&add)&&visited[temp.x][temp.y][temp.st]==)
{
visited[temp.x][temp.y][temp.st]=;
Q.push(temp);
}
}
else if(visited[temp.x][temp.y][temp.st]==)
{
visited[temp.x][temp.y][temp.st]=;
Q.push(temp);
}
}
}
}
} int main()
{
int i,j;
while(scanf("%d%d%d",&n,&m,&time)!=EOF)
{
res=-;
for(i=;i<=n;i++)
{
getchar();
for(j=;j<=m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='@')
{
sx=i;
sy=j;
}
else if(map[i][j]=='^')
{
ex=i;
ey=j;
}
}
}
bfs();
printf("%d\n",res);
}
return ;
}

hdu 1429(bfs+状态压缩)的更多相关文章

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1429 典型的状压搜索,在普通的搜索基础上,利用二进制的特性记录钥匙与门, 二进制的每一位代表一把钥匙,比如说拿到 ...

  2. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  3. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

  4. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  5. HDU1429+bfs+状态压缩

    bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...

  6. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  7. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  8. BFS+状态压缩 HDU1429

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

  9. hdu 5724 SG+状态压缩

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

随机推荐

  1. POJ 1704 Staircase Nim 阶梯博弈

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int ...

  2. linux下tomcat下部署项目如何打包压缩备份

    范例一:将整个 /etc 目录下的文件全部打包成为 /tmp/etc.tar[root@linux ~]# tar -cvf /tmp/etc.tar /etc <==仅打包,不压缩![root ...

  3. How a woman's glance can tell a man if she's interested

    How a woman's glance can tell a man if she's interested揭秘:女人是否喜欢你?看眼神就知道The female mind has always b ...

  4. Android核心分析之十六Android电话系统-概述篇

    Android电话系统之概述篇 首先抛开Android的一切概念来研究一下电话系统的最基本的描述.我们的手机首先用来打电话的,随后是需要一个电话本,随后是PIM,随后是网络应用,随后是云计算,随后是想 ...

  5. CentOS系统安装tomcat

    按照以下步骤操作即可wget http://mirrors.noc.im/apache/tomcat/tomcat-9/v9.0.0.M6/bin/apache-tomcat-9.0.0.M6.tar ...

  6. 笔者带你剖析淘宝TDDL(TAOBAO DISTRIBUTE DATA LAYER)

    注:本文部分内容引用本人博客http://gao-xianglong.iteye.com/blog/1973591   前言 在开始讲解淘宝的TDDL(Taobao Distribute Data L ...

  7. 关于Netty4.x中文教程系列更新进度的说明和道歉

    最近一些事情.貌似发现很久没更新教程了.这里和大家说一声对不起.教程5的前半部分差不多年前就写好了.但是由于年前我在的项目组项目进度比较紧张.一直在加班.教程的后半部分就一直没有写.年后由于一些公司人 ...

  8. Bootstrap plugin编写

    滚动demo: <!doctype html> <html lang="en"> <head> <meta charset="U ...

  9. USACO Section 2.3: Controlling Companies

    这题的dp里的check里的函数要考虑k control i control j和i control j control k的情况 /* ID: yingzho1 LANG: C++ TASK: co ...

  10. redis 2.4异常

    最近公司redis服务出现了异常,记录下教训: redis异常后:观察redis服务,可以看到redis cpu占用100% 用strace命令查看redis进程,显示如下: open("/ ...