bfs+状态压缩
思路:用2进制表示每个钥匙是否已经被找到。、

 /*
bfs+状态压缩
思路:用2进制表示每个钥匙是否已经被找到。
*/
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b))
const int inf = 0x3f3f3f3f;
const double pi=acos(-1.0);
const int dx[]={,-,,};
const int dy[]={,,,-};
const double eps = 1e-;
const int maxm = (<<)+;
const int maxn = ; bool vis[ maxn ][ maxn ][ maxm ];
char mat[ maxn ][ maxn ];
struct Point {
int x,y,ti,key;
};
Point s,e;
queue<Point>q; int bfs( int n,int m,int sumT ){
memset( vis,false,sizeof( vis ) );
while( !q.empty() )
q.pop();
Point cur;
cur = s;
vis[ cur.x ][ cur.y ][ cur.key ] = true;
q.push( cur );
while( !q.empty() ){
cur = q.front();
q.pop();
if( cur.x==e.x && cur.y==e.y ){
e.ti = min( e.ti,cur.ti );
}
if( cur.ti>=sumT ) continue;
for( int i=;i<;i++ ){
Point nxt ;
nxt.x = cur.x + dx[ i ];
nxt.y = cur.y + dy[ i ];
nxt.ti = cur.ti + ;
nxt.key = cur.key;
if( nxt.x<||nxt.x>=n||nxt.y<||nxt.y>=m ) continue;
if( mat[ nxt.x ][ nxt.y ]=='*' ) continue;
if( mat[ nxt.x ][ nxt.y ]>='a' && mat[ nxt.x ][ nxt.y ]<='z' ){
nxt.key = cur.key|(<<(mat[ nxt.x ][ nxt.y ]-'a'));
}//there may be a new key
if( vis[ nxt.x ][ nxt.y ][ nxt.key ]==true )
continue;
vis[ nxt.x ][ nxt.y ][ nxt.key ] = true;
if( mat[ nxt.x ][ nxt.y ]>='A' && mat[ nxt.x ][ nxt.y ]<='Z' ){
if( nxt.key&(<<(mat[ nxt.x ][ nxt.y ]-'A')) ){
q.push( nxt );
}
}
else
q.push( nxt );
}
}
if( e.ti>=sumT )
return -;
else
return e.ti;
} int main(){
int n,m,sumT;
while( scanf("%d%d%d",&n,&m,&sumT)== ){
for( int i=;i<n;i++ ){
scanf("%s",mat[ i ]);
for( int j=;j<m;j++ ){
if( mat[ i ][ j ]=='@' ){
s.x = i;
s.y = j;
s.ti = ;
s.key = ;
mat[ i ][ j ] = '.';
}
if( mat[ i ][ j ]=='^' ){
e.x = i;
e.y = j;
e.ti = sumT+;
mat[ i ][ j ] = '.';
}
}
}
printf("%d\n",bfs( n,m,sumT ));
}
return ;
}

HDU1429+bfs+状态压缩的更多相关文章

  1. BFS+状态压缩 HDU1429

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

  2. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  3. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

  4. 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 ...

  5. hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

    Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Sub ...

  6. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  7. HDU 1885 Key Task (BFS + 状态压缩)

    题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...

  8. hdu 1429(bfs+状态压缩)

    题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...

  9. UVALive 3956 Key Task (bfs+状态压缩)

    Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...

随机推荐

  1. 承接VR/AR内容应用定制需求

    业务范围: 1 承接VR/AR内容应用定制需求: 教育培训.建筑建设.旅游体验.课件教学系统.交通车辆仿真,模拟驾驶系统.游戏等.2 各类最新VR设备,例如GearVR.HTC vive.Oculus ...

  2. C/C++中浮点数格式学习——以IEEE75432位单精度为例

    这是浮点数的通常表示形式,在IEEE754中,单精度浮点数有如下形式: 位单精度 个比特存储. 位长 至23偏正值(实际的指数大小+127) 至0位编号(从右边开始为0) S为符号位,Exp为指数字, ...

  3. new失败判断

    使用 malloc/calloc 等分配内存的函数时,一定要检查其返回值是否为空;但是C++ 里,如果 new 分配内存失败,默认是抛出bad_alloc异常,不会返回空:但是有些编译器对c++标准支 ...

  4. Linux 文件系统 相关

    鸟个讲得很详细啦:http://vbird.dic.ksu.edu.tw/linux_basic/0230filesystem_1.php 重要知识点:磁盘,文件系统,超级区块 (superblock ...

  5. Java多线程(五) Lock接口,ReentranctLock,ReentrantReadWriteLock

    在JDK5里面,提供了一个Lock接口.该接口通过底层框架的形式为设计更面向对象.可更加细粒度控制线程代码.更灵活控制线程通信提供了基础.实现Lock接口且使用得比较多的是可重入锁(Reentrant ...

  6. CentOS 下安装JDK

    前提条件 使用干净的centOS 之前肯定没有装过JDK 所以忽略卸载步骤 <1>从SUN下载jdk-1_5_0_14-linux-i586-rpm.bin或jdk-1_5_0_14-li ...

  7. wireshark总结

    拖延了两个月的总结!下面的很大一部分来自其它博客. wireshark过滤器的区别 捕捉过滤器(CaptureFilters):用于决定将什么样的信息记录在捕捉结果中.需要在开始捕捉前设置.在Capt ...

  8. LCD/LED/OLED/等离子显示器区别

    LCD液晶显示器: LCD(Liquid Crystal Display),其构造是在两片平行的玻璃当中放置液态的晶体(液晶),在玻璃后面,以CCFL冷光灯管(类似日光灯)作背光源.液晶的成像原理可以 ...

  9. scrapy-redis使用详解

    描述: 1.使用两台机器,一台是win10,一台是centos7,分别在两台机器上部署scrapy来进行分布式抓取一个网站 2.centos7的ip地址为192.168.1.112,用来作为redis ...

  10. 1 server - n clients 模型实现(select)

    拓扑结构: 各个客户端创建读写管道,通过“上下线信息管道”向服务器发送上下线信息和读写管道名称.服务器接受信息,修改链表(存储客户端信息).客户端.服务器打开读写管道,服务器通过“W”管道接收从客户端 ...