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 ...
随机推荐
- [转]为革命保护视力 --- 给 Visual Studio 换颜色
本文转自:http://www.cnblogs.com/stg609/p/3723968.html “为革命,保护视力,预防近视,眼保健操开始......” 这个应该是最老版本的眼保健操了,你听过? ...
- sql server添加用户和给用户授权
--创建用户CREATE LOGIN 用户名 WITH PASSWORD=N'密码', DEFAULT_DATABASE=数据库名, CHECK_EXPIRATION=OFF, CHECK_POLIC ...
- C#_JDBC连接数据库
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- SpringMVC -- 必知必会
SpringMVC基于模型--视图--控制器(Model-View-Controller,MVC)模式实现,属于SpringFrameWork的后续产品,已经融合在SpringWebFlow里面.它通 ...
- poj2991 Crane
思路: 线段树每个节点维护第一条线段起点指向最后一条线段终点的向量,于是每一个操作都是一次区间更新.使用成段更新的线段树即可.实现: #include <cstdio> #include ...
- applicationContext.getBean(“loginEntity”)
<!-- 指定Spring需要扫描的包,并将所有是别的类放到容器中,便于识别被注解的受托管bean --> <context:component-scan base-package= ...
- 关于TREEVIEW的ONSELECTEDNODECHANGED事件
MSDN:http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.treeview.selectednodechanged( ...
- Katalon Studio(二) 进阶战の Jenkins集成 analytics.katalon 集成
本教程只针对Katalon Studio 与CI工具之一Jenkins的集成与脚本集的测试报告可视化简单操作. 1.新建一个job 2.新建一个自由风格的job 3.构建触发器 4.构建Windows ...
- Cygwin, MinGW/MSYS, MinGW-W64/MSYS2
1. Cygwin http://www.cygwin.com/ Cygwin is a large collection of GNU and Open Source tools which pro ...
- C# 获取目录下文件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...