hdu 5025 bfs+状压
http://acm.hdu.edu.cn/showproblem.php?pid=5025
N*N矩阵 M个钥匙
K起点,T终点,S点需多花费1点且只需要一次,1-9表示9把钥匙,只有当前有I号钥匙才能拿I+1号钥匙,可以不拿钥匙只从上面走过
BFS+优先队列。蛇最多只有5条,状压即可。
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
typedef pair<int,int> pii;
const int INF = 2000000007;
map <pii,int> hash;
int n,m,sx,sy,tx,ty,cnt_snake;
char s[105][105];
int dx[] = {1,-1,0,0},
dy[] = {0,0,1,-1};
int _key[105][105][10];
int anss;
bool in(int x,int y)
{
return 0 <= x && x < n && 0 <= y && y < n;
}
struct node{
int x,y,key,snak,t;
bool operator < (const node b )const
{
return t > b.t;
}
};
//class cmp
//{
//public:
// cmp(){};
// bool operator ()( const node a, const node b )
// {
// if(a.x!=b.x)
// return a.x<b.x;
// if(a.y!=b.y)
// return a.y<b.y;
// if(a.key != b.key)
// return a.key < b.key;
// if(a.snak != b.snak)
// return a.snak < b.snak;
// return false;
// }
//};
void bfs()
{
//map <node,int,cmp> vis;
priority_queue<node> q;
q.push((node){sx,sy,0,0,0});
while(!q.empty()){
node now = q.top(),to;
int x = now.x,y = now.y,key = now.key,snak = now.snak,t = now.t;
q.pop();
//vis[now] = 0;
if(s[x][y] == 'T' && key == m){//cout<<snak;
anss = min(anss,now.t);
return ;
}
for(int i = 0;i < 4;++i){
int mx = x+dx[i],my = y + dy[i];
if(!in(mx,my) || s[mx][my] == '#')continue;
if(s[mx][my] == 'S'){
pii tt = make_pair(mx,my);
if(snak & (1<<(hash[tt] - 1))){
to = (node){mx,my,key,snak,t+1};
}
else{
int tosnak = snak | (1<<(hash[tt] - 1));
to = (node){mx,my,key,tosnak,t+2};
}
}
else if(s[mx][my] - '0' == key + 1){
to = (node){mx,my,key+1,snak,t+1};
}
else{
to = (node){mx,my,key,snak,t+1};
}
if(to.t < _key[mx][my][to.key]){
q.push(to);
_key[mx][my][to.key] = to.t;
}
}
}
}
int main(){
while(~scanf("%d%d",&n,&m),n|m){
hash.clear();
cnt_snake = 0;
for(int i = 0;i < n;++i){
scanf("%s",s[i]);
for(int j = 0;j < n;++j){
if(s[i][j] == 'K')
sx = i,sy = j;
else if(s[i][j] == 'T')
tx = i,ty = j;
else if(s[i][j] == 'S'){
pii tt = make_pair(i,j);
hash[tt] = ++cnt_snake;
}
for(int k = 0;k <= m;++k)
_key[i][j][k] = INF;
}
}
//int mm = 1<<cnt_snake;
//memset(dp,0x3f,sizeof(dp));
_key[sx][sy][0] = 0;
anss = INF;
bfs();
if(anss >= INF)
puts("impossible");
else
printf("%d\n",anss);
}
return 0;
}
hdu 5025 bfs+状压的更多相关文章
- hdu 2209 bfs+状压
http://acm.hdu.edu.cn/showproblem.php?pid=2209 不知为啥有种直觉.会出状压+搜索的题,刷几道先 简单的BFS.状压表示牌的状态, //#pragma co ...
- hdu 1429 bfs+状压
题意:这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开始 Ignatius被关在(sx,sy)的位置,离开地牢的门 ...
- HDU 4771 BFS + 状压
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- C - 小明系列故事――捉迷藏 HDU - 4528 bfs +状压 旅游-- 最短路+状压
C - 小明系列故事――捉迷藏 HDU - 4528 这个题目看了一下题解,感觉没有很难,应该是可以自己敲出来的,感觉自己好蠢... 这个是一个bfs 用bfs就很好写了,首先可以预处理出大明和二明能 ...
- hdu 1044(bfs+状压)
非常经典的一类题型 没有多个出口.这里题目没有说清楚 Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- HDU 5025 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...
- hdu 5094 Maze (BFS+状压)
题意: n*m的迷宫.多多要从(1,1)到达(n,m).每移动一步消耗1秒.有P种钥匙. 有K个门或墙.给出K个信息:x1,y1,x2,y2,gi 含义是(x1,y1)与(x2,y2)之间有gi ...
- hdu 4771 Stealing Harry Potter's Precious (BFS+状压)
题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...
- 孤岛营救问题 (BFS+状压)
https://loj.ac/problem/6121 BFS + 状压 写过就好想,注意细节debug #include <bits/stdc++.h> #define read rea ...
随机推荐
- IE下设置body{overflow:hidden;}失效Bug
问题重现: <p>There are no scrollbars on this page in sane browsers</p> html, body, p { margi ...
- smartos介绍
https://wiki.smartos.org A Little History 2005年,Sun Microsystems开源了其著名的Unix操作系统Solaris,最终被发布为一个名为Ope ...
- nat 类型及打洞原理
nat 类型分4种 1.全锥形 full cone A 与 主机B交互,nat转换 A的内部地址及端口为 ip1 port1,ip1和port1为对外地址,任何机器能访问. 2.ip 受限制(对B而 ...
- linux 下 php 安装 pthreads
1.下载pthreads的源码包 https://pecl.php.net/package/pthreads 如:pthreads-3.1.6.tgz 2.解压 > tar zxvf pthre ...
- Thread.currentThread().getName() ,对象实例.getName() 和 this.getName()区别
原文链接:http://www.cnblogs.com/signheart/p/922dcf75dd5fe6b418d4475af89c4664.html 使用Thread.currentThread ...
- Legendre多项式
Legendre多项式 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Legendre多项式的递归公式
- 那些你不知道的PS大片摄影
你以为这张照片是P出来的? 才不是! 人家是开个起重机吊着拍的! 而且没有任何保护措施! 这样的照片战斗民族的摄影师才敢这样拍…要是在天朝这么做估计都找不到模特… 这张照片出自乌兹别克斯坦的摄影师Ra ...
- aspx导出文件
System.IO.StringWriter sw = new System.IO.StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw ...
- PetaPoco与SQLite
PetaPoco与SQLite. 对于精简版本的ORM,PetaPoco确实短小精悍,想做个WPF的Demo,然后将PetaPoco与SQLite集成一起使用,简单易用,是不错的选择. ()==数据库 ...
- XE4 for ios 谨慎处理字符串
由于xe4 for ios 里面的字符串处理有变化,具体可以参考官方文档,这两天帮一个朋友调试ios 的 应用,由于没有注意这一块,折腾了很长时间.特此记录下来,希望其他人不要走弯路. 以下面代码为 ...