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状态压缩)的更多相关文章

  1. hdu 1429 胜利大逃亡(续) (bfs+状态压缩)

    又开始刷题了 题意:略过. 分析:主要是确定状态量,除了坐标(x,y)之外,还有一个key状态,就好比手上拿着一串钥匙.状态可以用位运算来表示:key&(x,y)表示判断有没有这扇门的钥匙,k ...

  2. hdu 1429 胜利大逃亡(续)(bfs+位压缩)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. HDOJ 1429 胜利大逃亡(续) (bfs+状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 思路分析:题目要求找出最短的逃亡路径,但是与一般的问题不同,该问题增加了门与钥匙约束条件: 考虑 ...

  4. hdu.1429.胜利大逃亡(续)(bfs + 0101011110)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  5. HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)  ...

  6. hdu 1429 胜利大逃亡(续)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王 ...

  7. HDU 1429 胜利大逃亡(续)(DP + 状态压缩)

    胜利大逃亡(续) Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢 ...

  8. HDU 1429 胜利大逃亡(续)(bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  9. 胜利大逃亡(续)(状态压缩bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. .net 发送邮件验证码

    using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Ma ...

  2. 深度神经网络简述与Capsule介绍

    本人最近初学Hinton大神的论文<Dynamic Routing Between Capsules >,对深度神经网络的内容进行了简要总结,将观看“从传统神经网络的角度解读Capsule ...

  3. 迅为iTOP-4418嵌入式开发板初体验

    iTOP-4418开发板预装 Android4.4.4 系统, 支持9.7 寸.7 寸.4.3 寸屏幕. 参数:核心板参数 尺寸 50mm*60mm高度 核心板连接器为1.5mmCPU ARM Cor ...

  4. codeforces_C. Sequence Transformation

    http://codeforces.com/contest/1059/problem/C 题意: 最初给一个1.2.3.…….n的序列,每次操作先将所有元素的最大公约数加入答案序列,然后在序列中任意删 ...

  5. C# 获取目录下文件

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. CentOS 初体验十四:阿里云安装Gitlab

    网址:https://about.gitlab.com/install/#centos-7 https://blog.csdn.net/zhaoyanjun6/article/details/7914 ...

  7. 第1节 yarn:14、yarn集群当中的三种调度器

    yarn当中的调度器介绍: 第一种调度器:FIFO Scheduler  (队列调度器) 把应用按提交的顺序排成一个队列,这是一个先进先出队列,在进行资源分配的时候,先给队列中最头上的应用进行分配资源 ...

  8. SQLServer锁的概述

    SQLServer锁的概述   锁的概述 一. 为什么要引入锁 多个用户同时对数据库的并发操作时会带来以下数据不一致的问题: 丢失更新 A,B两个用户读同一数据并进行修改,其中一个用户的修改结果破坏了 ...

  9. PowerPoint幻灯片手动翻页设置技巧

    步骤: 幻灯片放映>设置幻灯片放映>手动

  10. Linux从入门到适应(三):Ubuntu16.04将python从3.5升级到3.6

    1 将python从默认的python2.7更换为python3.5 : sudo update-alternatives --install /usr/bin/python python /usr/ ...