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. [Entity Framework] MySQL @ Entity Framework 6

    原文 [Entity Framework] MySQL @ Entity Framework 6 要让MySQL能够用EF6,我花了一点时间,在此记录一下 安装元件 在设定档加入Provider 安装 ...

  2. html5 canvas 圆形抽奖的实例

    年底没啥,抽空学习了canvas,写了个html5抽奖的代码,造了个轮子,有用的童鞋可以拿走. 其中,canvas.onclick触发抽奖行为,概率可以在core.lottery()函数上添加,美化也 ...

  3. html5笔记

    出处:http://www.cnblogs.com/xiaowei0705/archive/2011/04/19/2021372.html HTML5 LocalStorage 本地存储 HTML5  ...

  4. [zz] pgpool-II load balancing from FAQ

    It seems my pgpool-II does not do load balancing. Why? First of all, pgpool-II' load balancing is &q ...

  5. vs2005_the breakpoint will not currently be hit. The source code is different from the original verison.

    公司开发只能用英文版的VS2005 (雅蠛蝶) Tool-->Options-->Debugging->General: 把Require source files to exact ...

  6. What is a First Chance Exception?

    Refrences: http://blogs.msdn.com/b/davidklinems/archive/2005/07/12/438061.aspx To be continued...

  7. Web性能压力测试工具之Siege详解

    PS:Siege是一款开源的压力测试工具,设计用于评估WEB应用在压力下的承受能力.可以根据配置对一个WEB站点进行多用户的并发访问,记录每个用户所有请求过程的相应时间,并在一定数量的并发访问下重复进 ...

  8. 正则应用—queryURLParameter()

    在项目中,我们做详情页的时候,需要获取到用户从哪里来点击进来,获取到用户的点击地址,根据不同的地址传进的参数向服务器获取不同的数据,然后加载不同的详情页面. 大部分企业都采用字符串截取的方式,quer ...

  9. vhost设定

    vhost设定   http.conf <Directory /> AllowOverride none #Require all denied </Directory>   ...

  10. Review PHP设计模式之——观测模式

    观测模式: <?php class car implements SplSubject{ private $carName; //车的类型 private $carState=0; //车的状态 ...