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 ...
随机推荐
- 5.10学习总结——Activity的跳转和传值
使用sharedpreference是对信息的存储,也可以进行传值,今天通过查找资料,学习了Activity的跳转和传值方法. 跳转 1.显示跳转 4种方法 1 2 3 4 5 6 7 8 9 10 ...
- 30天自制操作系统(二)汇编语言学习和Makefile入门
我们继续学习操作系统的相关内容. ; hello-os ; TAB=4 ORG 0x7c00 ; このプログラムがどこに読み込まれるのか ; 以下は標準的なFAT12フォーマットフロッピーディスクのた ...
- MapReduce原理深入理解(一)
1.MapReduce概念 1)MapReduce是一种分布式计算模型,由Google提出,主要用于搜索领域,解决海量数据的计算问题. 2)MapReduce是分布式运行的,由两个阶段组成:Map和R ...
- win8 连接到OneDrive时出现问题-感叹号
显示最后更新时间是1970年... 还有感叹号,没法同步 解决办法: 管理员运行cmd命令: 输入"netsh int ip reset c: esetlog.txt",按下回车键 ...
- http升级https遇到的问题
1. 功能请求失效: 可能是链接为http请求,导致出现问题 2.浏览器网址左边出现黄色感叹号: 这是由于网页中存在http的图片链接,需要根据实际情况修改; 3.将网页内的http请求变为https ...
- Shell系列(11)- 位置参数变量(4)
作用 往shell脚本里面传递参数 位置参数变量 作用 $n n 为数字,$0 代表命令本身,$1-$9 代表第一到第九个参数,十以上的参数需要用大括号包含,如 ${10} $* 这个变量代表命令行中 ...
- centos 7 & 6 优化脚本
简单优化 ,未涉及安全优化,如有需求请自行修改脚本实现 1 #!/bin/bash 2 SysVer=`cat /etc/redhat-release | awk -F'release' '{prin ...
- Gitee自动化部署python脚本
一.前期准备 1.1 安装环境 1.安装python3 2.打开命令行安装selenium pip install selenium 二.python代码 2.1 源码 #!/usr/bin/pyth ...
- Erase-Remove 惯用法
看到<Effective STL>条款 9 的时候想到了我以前复习的"如何正确使用迭代器删除元素",我面试时使用的也是里面的方法,看面试官的反应好像也没有什么问题,还问 ...
- 常见的==和equals比较
在笔试上碰到很多这样类似的题,全部整理到这里 String a = "Hello"; String b = "Hello"; String c = new St ...