hdu 4771 Stealing Harry Potter's Precious (BFS+状压)
题意:
n*m的迷宫,有一些格能走(“.”),有一些格不能走(“#”)。起始点为“@”。
有K个物体。(K<=4),每个物体都是放在“.”上。
问最少花多少步可以取完所有物体。
思路:
BFS+状压,看代码。
代码:
struct node{
int x,s;
node(int _x,int _s){
x=_x, s=_s;
}
};
int n,m,k,sx,sy;
char graph[105][105];
int px[5],py[5];
int dp[105][105][35];
int bfs(){
queue<node> q;
mem(dp,-1);
int s=0;
rep(i,1,k) if(px[i]==sx&&py[i]==sy) s=(1<<(i-1));
dp[sx][sy][s]=0;
q.push(node(sx*1000+sy,s));
if(s==((1<<k)-1))
return dp[sx][sy][s];
while(!q.empty()){
node f=q.front(); q.pop();
rep(i,0,3){
int nx=f.x/1000+uu[i], ny=f.x%1000+vv[i];
int s=f.s;
if(nx<0||ny<0||nx>=n||ny>=m) continue;
if(graph[nx][ny]=='#') continue;
rep(j,1,k) if(nx==px[j]&&ny==py[j]){
s|=(1<<(j-1));
}
if(dp[nx][ny][s]!=-1) continue;
dp[nx][ny][s]=dp[f.x/1000][f.x%1000][f.s]+1;
q.push(node(nx*1000+ny,s));
if(s==((1<<k)-1))
return dp[nx][ny][s];
}
}
return -1;
}
int main(){
//freopen("test.in","r", stdin);
while(scanf("%d%d",&n,&m),n||m){
rep(i,0,n-1){
scanf("%s",graph[i]);
rep(j,0,m-1) if(graph[i][j]=='@'){
sx=i, sy=j;
}
}
scanf("%d",&k);
rep(i,1,k) { scanf("%d%d",&px[i],&py[i]); --px[i], --py[i]; }
printf("%d\n",bfs());
}
//fclose(stdin);
}
hdu 4771 Stealing Harry Potter's Precious (BFS+状压)的更多相关文章
- HDU 4771 Stealing Harry Potter's Precious (2013杭州赛区1002题,bfs,状态压缩)
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDU 4771 Stealing Harry Potter's Precious dfs+bfs
Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...
- HDU 4771 Stealing Harry Potter's Precious
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压
2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...
- HDU Stealing Harry Potter's Precious(状压BFS)
状压BFS 注意在用二维字符数组时,要把空格.换行处理好. #include<stdio.h> #include<algorithm> #include<string.h ...
- hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@' 表示的是起点,'#' 表示的是障碍物不能通过,'.' 表示的是路能通过的: ...
- hdu 4771 Stealing Harry Potter's Precious(bfs)
题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...
- Stealing Harry Potter's Precious BFS+DFS
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
- HDU 5025:Saving Tang Monk(BFS + 状压)
http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description <Journey to ...
随机推荐
- 洛谷P1125——笨小猴(简易模拟)
https://www.luogu.org/problem/show?pid=1125 题目描述 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去 ...
- Java技术开发专题系列之【Guava RateLimiter】针对于限流器的入门到精通(针对于源码分析介绍)
Guava包中限流实现分析 RateLimiter 之前的文章中已经介绍了常用的限流算法,而google在Java领域中使用Guava包中的限流工具进行服务限流. 回顾使用案例 Google开源工具包 ...
- sql查询字段语句
SELECT * from jd_content where 景点简介 like '%东城%'
- Mongodb的基本使用及对接多数据源
mongodb介绍 MongoDB(来自于英文单词"Humongous",中文含义为"庞大")是可以应用于各种规模的企业.各个行业以及各类应用程序的开源数据库. ...
- IOS 安装网页
一般使用企业签名的iOS安装包 都需要一个下载地址,像蒲公英什么的 都有下载限制,所以求人不如求己. 内容也是复制别人的 :https://lzw.me/a/itms-services-ios-ins ...
- django安装xadmin
环境:pycharm django1.11.20 python2.7(根据网络上的资料,自己整理实现) 下载:https://github.com/sshwsfc/xadmin/tree/mast ...
- 『GoLang』string及其相关操作
目录 1. 字符串简介 2. 字符串的拼接 3. 有关 string 的常用处理 3.1 strings 包 3.1.1 判断两个 utf-8 编码字符串是否相同 3.1.2 判断字符串 str 是否 ...
- disruptor笔记之五:事件消费实战
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- ❤️❤️新生代农民工爆肝8万字,整理Python编程从入门到实践(建议收藏)已码:8万字❤️❤️
@ 目录 开发环境搭建 安装 Python 验证是否安装成功 安装Pycharm 配置pycharm 编码规范 基本语法规则 保留字 单行注释 多行注释 行与缩进 多行语句 数据类型 空行 等待用户输 ...
- Jmeter压测学习6---登录参数CSV
前言 我们在压测登录接口的时候,如果只用一个账号去设置并发压测,这样的结果很显然是不合理的,一个用户并发无法模拟真实的情况.如果要压测登录接口,肯定得准备几百,甚至上千的账号去登录,测试的结果才具有可 ...