HDU-5025 Saving Tang Monk 广度搜索 状态压缩
题目链接:https://cn.vjudge.net/problem/HDU-5025
题意
救唐僧,路上有m(<=9)把钥匙,最多5条蛇和一个唐僧。
目标是前往唐僧的地方,用全部钥匙打开全部的锁,来就唐僧。
钥匙必须要按顺序拿,只有拿过第三个钥匙才可以拿第四个钥匙。
蛇必须得打一个单位时间,打过的蛇就不要再打了。
问最少多长时间可以救出唐僧?没的救输出-1。
思路
又是状压搜索,注意细节即可。
二进制存打过的蛇的集合,注意有可能step大的在队前,所以咱得用优先队列。
如果用普通队列,还得注意vis数组的问题,有可能较大的元素在相同状态上打下标记,
这里就像队列dijkstra一样操作即可,改为int类型记录step。
提交过程
| MLE | 数组大小写错了,注意<<的低优先级 |
| WA | 注意打过的蛇就不要再打了,被打怕了好么 |
代码
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=100+2, dir[4][2]={1,0, 0,1, -1,0, 0,-1}, INF=0x3f3f3f3f;
struct State{
int x, y, step;
char keys, snake;
State(int x=0, int y=0, int step=0, char keys=0, char snake=0):
x(x), y(y), step(step), snake(snake), keys(keys) {}
bool operator < (const State &a) const{
return step>a.step;
}
};
bool vis[maxn][maxn][10+2][(1<<5)+1]; // attend this!!!!!
char map[maxn][maxn];
int n, m;
int bfs(int sx, int sy){
priority_queue<State> que;
que.push(State(sx, sy, 0, 0, 0));
vis[sy][sx][0][0]=true;
while (que.size()){
State st=que.top(); que.pop();
for (int i=0; i<4; i++){
int xx=st.x+dir[i][0], yy=st.y+dir[i][1];
char snake=st.snake, keys=st.keys;
int step=st.step+1;
if (xx<0 || yy<0 || xx>=n || yy>=n) continue;
if (map[yy][xx]=='#') continue;
else if (map[yy][xx]<0){
int sid=(map[yy][xx]*-1)-1;
if (!(snake & (1<<sid))) step++;
snake|=(1<<sid);
}else if (map[yy][xx]<='9' && map[yy][xx]>='1'){
int kid=map[yy][xx]-'1';
if (keys==kid) keys++;
}else if (map[yy][xx]=='T'){
if (keys==m) return step;
}
if (vis[yy][xx][keys][snake]) continue;
vis[yy][xx][keys][snake]=true;
que.push(State(xx, yy, step, keys, snake));
}
}
return -1;
}
int main(void){
while (scanf("%d%d", &n, &m)==2 && n){
int sx, sy, sid=-1;
for (int y=0; y<n; y++){
scanf("%s", map[y]);
for (int x=0; x<n; x++)
if (map[y][x]=='K') sx=x, sy=y, map[y][x]='.';
else if (map[y][x]=='S') map[y][x]=sid--;
}
memset(vis, false, sizeof(vis));
int ans=bfs(sx, sy);
if (ans<0) printf("impossible\n");
else printf("%d\n", ans);
}
return 0;
}
| Time | Memory | Length | Lang | Submitted |
|---|---|---|---|---|
| 468ms | 5856kB | 1700 | C++ | 2018-08-15 00:27:41 |
HDU-5025 Saving Tang Monk 广度搜索 状态压缩的更多相关文章
- hdu 5025 Saving Tang Monk(bfs+状态压缩)
Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Nove ...
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- HDU 5025 Saving Tang Monk 【状态压缩BFS】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Time Limit: 2000/1000 MS (Java/O ...
- [ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)
Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)
Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...
- HDU 5025 Saving Tang Monk
Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...
- 2014 网选 广州赛区 hdu 5025 Saving Tang Monk(bfs+四维数组记录状态)
/* 这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态! 以前做过的都是用二维的!自己的四维还是太狭隘了..... 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找到! 每种钥匙有 k ...
- HDU 5025 Saving Tang Monk(状态转移, 广搜)
#include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN], snake[maxN][maxN]; ]; int ...
- HDU 5025 Saving Tang Monk --BFS
题意:给一个地图,孙悟空(K)救唐僧(T),地图中'S'表示蛇,第一次到这要杀死蛇(蛇最多5条),多花费一分钟,'1'~'m'表示m个钥匙(m<=9),孙悟空要依次拿到这m个钥匙,然后才能去救唐 ...
随机推荐
- mysql中redo和binlog的区别
影响MySQL中redo的配置参数: innodb_log_file_size:指定每个redo日志大小,默认值48MB innodb_log_files_in_group:指定日志文件组中redo日 ...
- pytorch实战(2)-----回归例子
一.回归任务介绍: 拟合一个二元函数 y = x ^ 2. 二.步骤: 导入包 创建数据 构建网络 设置优化器和损失函数 前向和后向传播训练网络 画图 三.代码: 导入包: import torch ...
- (转)C++引用
前言:引用是C++一个很重要的特性,最近看了很多有关引用的资料和博客,故在此对引用的相关知识进行总结 一.什么是引用 引用,顾名思义是某一个变量或对象的别名,对引用的操作与对其所绑定的变量或对象的操作 ...
- java类的属性
类的嵌套!!!!!!!!!! 首先我们创建一个学生卡卡号的一个类,这个类有两个属性,校园卡号和银行卡号 package cuteSnow; public class StudentCard { pub ...
- spring boot学习(转)
玩转Spring Boot 前言 首先在这里对Spring Boot做个简单的介绍,对Spring Boot也关注了挺久了,Spring Boot是由Pivotal团队提供的全新框架, ...
- bzoj3626【LNOI2014】LCA
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1266 Solved: 448 [Submit][Stat ...
- hdu2546 饭卡 01-背包问题
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 Problem ...
- python抓取新浪微博评论并分析
1,实现效果 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb2xhbnphbw==/font/5a6L5L2T/fontsize/400/fill ...
- 51nod-1253: Kundu and Tree
[传送门:51nod-1253] 简要题意: 给出一棵n个点的树,树上的边要么为黑,要么为红 求出所有的三元组(a,b,c)的数量,满足a到b,b到c,c到a三条路径上分别有至少一条红边 题解: 显然 ...
- JavaScript常用的api
打印日志 console.log 类型判断 第一种方式var type = Object.prototype.toString.call(list);console.log(type);第二种方式ty ...