hdu - 1429 胜利大逃亡(续) (bfs状态压缩)
http://acm.hdu.edu.cn/showproblem.php?pid=1429
终于开始能够做状态压缩的题了,虽然这只是状态压缩里面一道很简单的题.
状态压缩就是用二进制的思想来表示状态.
总共有10种钥匙,那么开一个(1<<10 的数组) 那么每次遇到一把钥匙我就用当前状态 | 钥匙转化为2进制的数值,然后遇到门的时候判断是否有对应的钥匙,只要用当前状态 & 门转化为2进制的值就可以。初始为0时,state=0,表示10个状态位都是0.那么每次遇到钥匙就改变相应的状态位。
比如当前state ==0 遇到 'a' 那么 state | 0 ==1 (1),遇上 'b'那么state | 1 ==3 (11)
这样每遇到一把钥匙,当前状态对应的位就变成1.
其余的就跟普通bfs没什么两样了。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 2510
#define mod 1000000000
using namespace std; struct point
{
int x,y,step,state;
}; int n,m,t;
int dir[][]={-,,,,,,,-};
char maze[][];
int vis[][][<<];
int ex,ey; int get(char c)
{
return c-(islower(c)?'a':'A'); //转化为 数字 'a'为0
}
void bfs(int sx,int sy)
{
point s;
s.x=sx,s.y=sy,s.step=,s.state=;
memset(vis,,sizeof(vis));
vis[s.x][s.y][]=;
queue<point>que;
que.push(s);
while(!que.empty())
{
point e=que.front(); que.pop();
// printf("%d %d %d %d\n",e.x,e.y,e.step,e.state);
if(e.x==ex&&e.y==ey) {printf("%d\n",e.step);return;}
for(int i=;i<;i++)
{
s.x=e.x+dir[i][];
s.y=e.y+dir[i][];
s.state=e.state;
if(s.x>=&&s.x<n&&s.y>=&&s.y<m&&maze[s.x][s.y]!='*')
{
//判断是否有相应钥匙
if(isupper(maze[s.x][s.y])&&(!(e.state&(<<get(maze[s.x][s.y]))))) continue;
if(islower(maze[s.x][s.y])) //遇到钥匙
{
//printf("%d\n",1<<get(maze[s.x][s.y]));
s.state|=(<<get(maze[s.x][s.y]));
}
s.step=e.step+;
if(!vis[s.x][s.y][s.state]&&s.step<=t)
{
vis[s.x][s.y][s.state]=;
que.push(s);
}
}
}
}
printf("-1\n");
}
int main()
{
//freopen("a.txt","r",stdin);
int sx,sy;
while(~scanf("%d%d%d",&n,&m,&t))
{
t--;
for(int i=;i<n;i++)
{
scanf("%s",maze[i]);
for(int j=;j<m;j++)
if(maze[i][j]=='@')
{
sx=i,sy=j;
}
else if(maze[i][j]=='^')
{
ex=i,ey=j;
}
}
// printf("%d %d\n",sx,sy);
bfs(sx,sy);
}
return ;
}
hdu - 1429 胜利大逃亡(续) (bfs状态压缩)的更多相关文章
- hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
又开始刷题了 题意:略过. 分析:主要是确定状态量,除了坐标(x,y)之外,还有一个key状态,就好比手上拿着一串钥匙.状态可以用位运算来表示:key&(x,y)表示判断有没有这扇门的钥匙,k ...
- hdu 1429 胜利大逃亡(续)(bfs+位压缩)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDOJ 1429 胜利大逃亡(续) (bfs+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 思路分析:题目要求找出最短的逃亡路径,但是与一般的问题不同,该问题增加了门与钥匙约束条件: 考虑 ...
- hdu.1429.胜利大逃亡(续)(bfs + 0101011110)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 1429 胜利大逃亡(续)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王 ...
- HDU 1429 胜利大逃亡(续)(DP + 状态压缩)
胜利大逃亡(续) Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢 ...
- HDU 1429 胜利大逃亡(续)(bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 胜利大逃亡(续)(状态压缩bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- Android开发学习——ButterKnife使用
为了码代码的效率,我们有了ButterKnife;其基本使用如下步骤: 1.在Android Studio的Setting中,下载plugin 2.在整个Project的build.gradle中添加 ...
- Abp Framework中文文档上线
感谢 ABP框架中国小组 给我们带来的ABP中文翻译,Web+为方便广大学习爱好者随时查阅,现推出了Gitbook风格的在线阅读文档:http://www.webplus.org.cn/documen ...
- php数组转为字符串,数据库存储
php对象转字符存储数据库的方法. 总所周知对象是不能直接存储到数据库的.那么我们用什么样的方法能够存储到数据库中能? 方法一:序列化serialize和unserialize 序列化对象serial ...
- AJPFX:学习JAVA程序员两个必会的冒泡和选择排序
* 数组排序(冒泡排序)* * 冒泡排序: 相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处* * 选择排序 : 从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现 ...
- 如何在tomcat部署项目(用ip访问)
找了好长时间的错误,server.xml中一点错误也没有,但就是访问不到,最终发现就是服务器没有开放80端口的缘故. 服务器是Windows系统 1.控制面板=>系统和安全=>Window ...
- (转载)Sql注入的分类:数字型+字符型
Sql注入: 就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.通过构造恶意的输入,使数据库执行恶意命令,造成数据泄露或者修改内容等,以 ...
- t470安装win7
终于把win7安装好了,写了个文档 https://files.cnblogs.com/files/cookies9/t470%E5%AE%89%E8%A3%85win7%E6%96%B9%E6%B3 ...
- adobe开发软件激活
稳定支持至2017版本系列的adobe开发软件破解激活 本内容属原创,转载请注明出处! 以激活AE CC2017为例演示: 第一步打开软件第二步在产品列表中选择你所安装的产品(注意区分 32 位和 ...
- (转) 淘淘商城系列——解决KindEditor上传图片浏览器兼容性问题
http://blog.csdn.net/yerenyuan_pku/article/details/72808229 上文我们已实现了图片上传功能,但是有个问题,那就是对浏览器兼容性不够,因为Map ...
- 为什么java String是固定的 为什么字符串是不可变的
String类不可变的好处 String是所有语言中最常用的一个类.我们知道在Java中,String是不可变的.final的.Java在运行时也保存了一个字符串池(String pool),这使得S ...