HDU1429+bfs+状态压缩
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+状态压缩的更多相关文章
- BFS+状态压缩 HDU1429
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))
求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...
- BFS+状态压缩 hdu-1885-Key Task
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...
- 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 ...
- hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】
Maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others) Total Sub ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- hdu 1429(bfs+状态压缩)
题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...
- UVALive 3956 Key Task (bfs+状态压缩)
Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...
随机推荐
- Linux 命令行技巧
这是一个linux常见命令的列表.那些有• 标记的条目,你可以直接拷贝到终端上而不需要任何修改,因此你最好开一个终端边读边剪切&拷贝.所有的命令已在Fedora和Ubuntu下做了测试 命令 ...
- spring自定义注解
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- OC6-网址分割
// // HtmlManger.h // OC6-网址分割 // // Created by qianfeng on 15/6/17. // Copyright (c) 2015年 qianfeng ...
- Smarty控制foreach输出数量
最近在搞ecshop,ecshop用的是smarty.在首页中想要修改展示的商品数量,但是用的是foreach,让我无从下手,在网上找了好久终于找到解决方案. <!--{foreach name ...
- SOA 的基本概念及设计原则浅议
SOA是英文词语"Service Oriented Architecture"的缩写,中文有多种翻译,如"面向服务的体系结构"."以服务为中心的体系结 ...
- [GeekBand] 面向对象的设计模式(C++)(2)
本篇笔记紧接上篇,继续学习设计模式. 4. 对象创建类设计模式 通过对象创建模式绕开new,来避免对象创建(new)过程中所导致的紧耦合,从而支持对象创建的稳定.它是接口抽象之后的第一步工作. 4.1 ...
- bower——库管理工具
bower了解: 随着网页功能的复杂化,各种网页效果的实现,现在单一的一个或两个库文件或许已经不能够满足我们的需要,但当有很多的库文件的时候,手动编辑已经不能胜任,对于引入的库文件而言,往往都是牵一发 ...
- C# winfrom中的布局 控件Anchor和Dock的区别
c#中的布局问题 http://hi.baidu.com/whzpower/item/57e3179cca21e1cab725317a
- 去掉iphone手机滑动默认行为
/*去掉iphone手机滑动默认行为*/ $('body').on('touchmove', function (event) { event.preventDefault(); });
- phpcms v9 打开网站特别慢 增加数据库缓存方法
SET GLOBAL QUERY_CACHE_SIZE=80000000; 设置好查询缓存的大小就行了.比如设置个20MB.SET GLOBAL QUERY_CACHE_SIZE=20000000; ...