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 ...
随机推荐
- 用httpPost对JSON发送和接收的例子
HTTPPost发送JSON: private static final String APPLICATION_JSON = "application/json"; ...
- Android笔记——JDK实现WebService服务
以天气预报为例,国家气象局计算的天气信息后在其服务器Server通过WebService对外暴露天气信息数据,客户端无论是java平台,ios平台,c#平台都可以通过WebService获取数据.体现 ...
- iOS sqlite3数据库解析
看来从版本3.3.1基本上已经支持线程句柄的传递功能.具体限制我标记了一下.(6) Is SQLite threadsafe?SQLite is threadsafe. We make this co ...
- MAVEN ERROR: unable to find valid certification path to requested target 解决办法
第一次使用MAVEN编译项目,出现如下错误 解决办法:Maven的setting.xml中添加如下代码 <mirrors> <mirror> <id>Central ...
- 纯js实现瀑布流布局及ajax动态新增数据
本文用纯js代码手写一个瀑布流网页效果,初步实现一个基本的瀑布流布局,以及滚动到底部后模拟ajax数据加载新图片功能. 缺点: 1. 程序不是响应式,不能实时调整页面宽度: 2. 程序中当新增ajax ...
- MVC+EF+Spring.Net代码生成器
最近研究学习了MVC.EF等相关技术,写了一套项目架构.只要更改EF模型,生成数据库并转换T4模版.数据层和业务层就可以自动生成了. 主要用到的技术: 1.EF实体框架. 2.Spring.Net依赖 ...
- ColorDescriptor software v4.0 一个提取颜色特征描述子的软件包
ColorDescriptor software v4.0 Created by Koen van de Sande, (c) University of Amsterdam Note: Any co ...
- mysql高可用方案比较
详见:High Availability Database Tools http://www.acquia.com/blog/high-availability-database-tools
- mtk android lcm调试
参考MTK 文档LCM_Customer_document_MT6575.pdf The following shows the steps to add a new LCM driver: (1) ...
- Create Entity Data Model
http://www.entityframeworktutorial.net/EntityFramework5/create-dbcontext-in-entity-framework5.aspx 官 ...