hdu 1429(bfs+状态压缩)
题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。因为这里我贡献了一次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+状态压缩)的更多相关文章
- hdu 1429 (bfs+状态压缩) 胜利大逃亡续
http://acm.hdu.edu.cn/showproblem.php?pid=1429 典型的状压搜索,在普通的搜索基础上,利用二进制的特性记录钥匙与门, 二进制的每一位代表一把钥匙,比如说拿到 ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- BFS+状态压缩 hdu-1885-Key Task
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...
- ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))
求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
- 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 ...
- BFS+状态压缩 HDU1429
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 5724 SG+状态压缩
Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
随机推荐
- java jms
这篇博文我们主要介绍J2EE中的一个重要规范JMS,因为这个规范在企业中的应用十分的广泛,也比较重要,我们主要介绍JMS的基本概念和它的模式,消息的消费以及JMS编程步骤. 基本概念 JMS是java ...
- React属性的3种设置方式
一. 不推荐用setProps,因为以React的设计思想相悖,推荐以父组件向子组件传递属性的方式 二.3种用法的代码 1.键值对 <!DOCTYPE html> <html lan ...
- C编译器剖析PDF文档及UCC编译器162.3
http://blog.csdn.net/sheisc/article/details/42387857 http://blog.csdn.net/sheisc/article/details/455 ...
- Android 获取屏幕高度,宽度,状态栏高度
背景介绍: 到目前为止,android已经从1.5发展到目前的3.2,我们在写一个应用的时候,最常用到得就是获取屏幕高度,宽度,以及status bar的高度. 然而android系统变化太快了,从开 ...
- 51nod 1092 回文字符串 (dp)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1092 这个题是poj-3280的简化版,这里只可以增加字符,设 dp[i ...
- LCS记录路径
还想用hash记录……果然是天真.lcs转移比较简单,每次增加1.每次找是当前-1的就行了. #include <algorithm> #include <iostream> ...
- URAL1118. Nontrivial Numbers
1118 优化 1.枚举到sqrt(n)2.区间有质数直接输出最大质数3.a=1 直接输出1 4.边+边与最小值比较 #include <iostream> #include<cst ...
- hdu3333(线段树)
区间更新,单点查询. hdu3333 #include <iostream> #include <stdio.h> #include <string.h> #inc ...
- jsp之jstl标签
常用jstl标签 一.<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> ...
- centos 6.5 minimal 安装及vm-tools安装
安装vm-->注册vm-->新建一个虚拟机(选择等会安装系统)-->加载iso-->配置桥接-->启动 这里可能碰到一个cpu的虚拟化配置,需要在bios里的配置设置为e ...