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 ...
随机推荐
- 在spring boot 中使用itext和itextrender生成pdf文件
转载请注明出处 https://www.cnblogs.com/majianming/p/9539376.html 项目中需要对订单生成pdf文件,在第一版本其实已经有了比较满意的pdf文档,但是还是 ...
- JS 事件添加onclick写法注意。
自定义函数添加onclick事件写法注意. 错误写法:element.onclick = addclass(className); 正确写法:element.onclick = function(){ ...
- web+ admin template,spa管理应用后台,easyui后台正式发布
演示地址:http://admintemplate.webplus.org.cn/ v1.0 (2016/7/27) 扁平化风格 全屏支持 后台管理不使用iframe,全ajax开发 权限管理 商品管 ...
- SpringBoot 2.x (2):请求和传参
其实请求和传参这些知识属于SpringMVC 不过这也属于必须掌握的知识,巩固基础吧 GET请求: 以第一篇文章自动的方式创建SpringBoot项目: 然后新建Controller: package ...
- WPF 实时绘图的逻辑
实时绘图实际上是两个线程.外部线程直接用thread,只有到绘图那个逻辑才用绘图控件的mycanvas2.Dispatcher.Invoke. 或者说,INVOKE并不是开线程,只是一个绘图的委托而已 ...
- 浅谈CSS中的定位知识
1,静态定位(static) 表示按照正常定位方案,元素盒按照在文档流中出现的顺序依次格式化: 2,相对定位(relative) 将移动元素盒,但是它在文档流中的原始空间会保留下来: 相对定位元素有如 ...
- Laravel Passport认证-多表、多字段解决方案
Laravel Passport认证-多表.多字段解决方案 2018年08月19日 09:31:01 醉卧码场君莫笑 阅读数:1632 1. 概述 API 通常使用令牌(token)进行认证并且在 ...
- JVM参数说明介绍
1.JVM运行时数据区内存参数说明 2.JVM垃圾收集器参数总结 原文:https://blog.csdn.net/bluuusea/article/details/90697340 ======== ...
- OpenMP入门教程(二)
OpenMP API概述 OpenMP由三部分组成: 编译指令(19) 运行时库程序(32) 环境变量(9) 后来的API包含同样的三个组件,只是三者的数量都有所增加. 编译器指令 OpenMP编译器 ...
- Ubuntu16.04 python3.4.3升级到python3.7.1
python有两个版本,一个2版本,使用的是python:另一个是3版本,使用的是python3. 简易安装python后得到的3版本的版本号是python3.4.3. 可以使用下面的命令查看py版本 ...