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 ...
随机推荐
- 学习PHP中的国际化日期格式化操作
对于国际化功能来说,日期相关的格式化操作也是一块重头戏,毕竟不同的时区,不同的国家对于日期的表示方式都会有些不同.今天我们主要来学习的就是国际化地表示日期相关的信息内容. 日期格式化 首先就是最直接的 ...
- MapReduce原理深入理解(二)
1.Mapreduce操作不需要reduce阶段 1 import org.apache.hadoop.conf.Configuration; 2 import org.apache.hadoop.f ...
- Java基础系列(19)- Switch结构
package struct; public class SwitchDemo01 { //case穿透 //switch 匹配一个具体的值 public static void main(Strin ...
- Jenkins持续集成体系 | 最完整的介绍及资料
这篇文章是来给大家普及Jenkins知识的, Jenkins能解决什么问题, 有哪些应用场景, 为何要掌握Jenkins, 掌握Jenkins后有哪些好处, 弄懂Jenkins需要掌握哪些知识 不知道 ...
- 推荐一个pycharm验证xpath表达式的插件XPathView + XSLT
使用Appium进行自动化测试,使用xpath元素定位,想验证xpath定位是否正确,可以使用在线的xpath验证网站,也可以使用这次推荐的插件XPathView + XSLT.
- python序列类型及一些操作
序列分类 1.按存放的数据类型分类: 容器类型(能存放不同类型的数据):list.tuple.coolections.deque 扁平序列(只能存放一种类型的数据):str.bytes.bytearr ...
- nginx 配置文件(支持thnkphp3.2~5)
server { listen 8080 ; server_name localhost; set $root /var/www/myweb; #listen 443 ssl; #ssl_certif ...
- Vue使用axios post方法发送json数据报415Unsupported Media Type
1.Vue使用axios post方法发送json数据 <template> <el-aside> <el-form ref="form" :mode ...
- mimikatz使用笔记
一.获取密码# privilege::debug sekurlsa::logonpasswords mimikatz.exe "sekurlsa::debug" "sek ...
- 多项目如何高效协同合作 | springcloud系列之bus消息总线
前言 在springcloud config章节中我们完成了配种中心的搭建,以及通过配置中心完成配置的抽离通过springcloud config模块我们将配置抽离到git仓库中我们不必要每次为了改配 ...