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 ...
随机推荐
- iOS开发--UITableView
-.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; [Data ...
- try catch语句在VC下的处理
使用VC编译QT程序碰到一个问题: 我在.h文件里定义: LoadingWidget* w;然后.cpp文件里定义: void MyClass::ModifyTask(){ // w = ...
- POJ1088滑雪
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- 312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
- java读取package中的properties文件java.util.MissingResourceException
文件结构: /build/classes/d914/Hello.class /build/classes/d914/mess.properties /build/classes/d914/mess_z ...
- Pycharm连接gitlab
一.从gitlab上clone代码到本地pycharm (一).gitlab上找到创建项目的连接地址,分两种: 1. http连接方式: http://10.22.1.72/derekchen/cxg ...
- C# 将字符串转化成流,将流转换成字符串
using System; using System.IO; using System.Text; namespace CSharpConvertString2Stream { class Progr ...
- .net类库中和数据库相关的
System.Data.SqlTypes SqlDbType 枚举类型 Specifies SQL Server-specific data type of a field, property, fo ...
- BroadcastService的测试用例
程序运行起来之后,需要用多个telnet客户端来进行测试 连接的命令为conn deviceNumber 广播命令为broa message 需要注意的是:示例代码有bug, broadcastDic ...
- 二、理解over()函数
1.1.两个order by的执行时机分析函数是在整个sql查询结束后(sql语句中的order by的执行比较特殊)再进行的操作, 也就是说sql语句中的order by也会影响分析函数的执行结果: ...