hdu 4771 13 杭州 现场 B - Stealing Harry Potter's Precious 暴力bfs 难度:0
Description
Some rooms are indestructible and some rooms are vulnerable. Goblins always care more about their own safety than their customers' properties, so they live in the indestructible rooms and put customers' properties in vulnerable rooms. Harry Potter's precious are also put in some vulnerable rooms. Dudely wants to steal Harry's things this holiday. He gets the most advanced drilling machine from his father, uncle Vernon, and drills into the bank. But he can only pass though the vulnerable rooms. He can't access the indestructible rooms. He starts from a certain vulnerable room, and then moves in four directions: north, east, south and west. Dudely knows where Harry's precious are. He wants to collect all Harry's precious by as less steps as possible. Moving from one room to another adjacent room is called a 'step'. Dudely doesn't want to get out of the bank before he collects all Harry's things. Dudely is stupid.He pay you $1,000,000 to figure out at least how many steps he must take to get all Harry's precious.
Input
In each test cases:
The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 100).
Then a N×M matrix follows. Each element is a letter standing for a room. '#' means a indestructible room, '.' means a vulnerable room, and the only '@' means the vulnerable room from which Dudely starts to move.
The next line is an integer K ( 0 < K <= 4), indicating there are K Harry Potter's precious in the bank.
In next K lines, each line describes the position of a Harry Potter's precious by two integers X and Y, meaning that there is a precious in room (X,Y).
The input ends with N = 0 and M = 0
Output
Sample Input
##@
#.#
1
2 2
4 4
#@##
....
####
....
2
2 1
2 4
0 0
Sample Output
5
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
typedef pair<int,int> P;
const int inf=0x7ffffff;
int n,m,k;
int px[],py[];//记录珍宝坐标,德拉克坐标设为第0个
int d[][];//记录珍宝间最短距离
char maz[][];//记录迷宫状态
int ind[][];//记录珍宝编号
bool vis[][];//bfs用
int dis[][];//bfs用,最短距
const int dx[]={,-,,};const int dy[]={,,-,};
bool ok(int x,int y){
return x>=&&x<n&&y>=&&y<m&&maz[x][y]!='#';
}
void bfs(int index){
int sx=px[index],sy=py[index];
for(int i=;i<n;i++)for(int j=;j<m;j++)dis[i][j]=inf;
memset(vis,,sizeof(vis));
vis[sx][sy]=true;
d[index][index]=;
dis[sx][sy]=;
queue<P>que;
que.push(P(sx,sy));
while(!que.empty()){
sx=que.front().first;sy=que.front().second;que.pop();
for(int i=;i<;i++){
int tx=sx+dx[i],ty=sy+dy[i];
if(ok(tx,ty)&&!vis[tx][ty]){
vis[tx][ty]=true;dis[tx][ty]=dis[sx][sy]+;
if(ind[tx][ty]!=||maz[tx][ty]=='@'){
d[index][ind[tx][ty]]=dis[tx][ty];}
que.push(P(tx,ty));
}
}
}
}
int getlength(int a[]){
int ans=;
for(int i=;i<k;i++)ans+=d[a[i]][a[i+]];
return ans;
}
int pre(){
int a[];//暴力枚举所有可能排列
for(int i=;i<=k;i++)a[i]=i;//需注意第一个排列
int ans=getlength(a);
while(next_permutation(a+,a+k+)){//注意不要直接排列k
ans=min(ans,getlength(a));
}
return ans;
}
int main(){
while(scanf("%d%d",&n,&m)){
if(n==&&m==)break;
for(int i=;i<n;i++){
scanf("%s",maz[i]);
}
scanf("%d",&k);
memset(ind,,sizeof(ind));
for(int i=;i<=k;i++){scanf("%d%d",px+i,py+i);px[i]--;py[i]--;ind[px[i]][py[i]]=i;}
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(maz[i][j]=='@'){
px[]=i;
py[]=j;
ind[i][j]=;
break;
}
}
}
for(int i=;i<;i++)for(int j=;j<;j++)d[i][j]=inf;
for(int i=;i<=k;i++)bfs(i);
int ans=pre();
printf("%d\n",ans==inf?-:ans);
}
return ;
}
hdu 4771 13 杭州 现场 B - Stealing Harry Potter's Precious 暴力bfs 难度:0的更多相关文章
- hdu 3682 10 杭州 现场 C To Be an Dream Architect 容斥 难度:0
C - To Be an Dream Architect Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- 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 4770 13 杭州 现场 A - Lights Against Dudely 暴力 bfs 状态压缩DP 难度:1
Description Harry: "But Hagrid. How am I going to pay for all of this? I haven't any money.&quo ...
- 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压
2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...
- hdu4771 Stealing Harry Potter's Precious(DFS,BFS)
练习dfs和bfs的好题. #include<iostream> #include<cstdio> #include<cstdlib> #include<cs ...
- 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
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@' 表示的是起点,'#' 表示的是障碍物不能通过,'.' 表示的是路能通过的: ...
- hdu 3685 10 杭州 现场 F - Rotational Painting 重心 计算几何 难度:1
F - Rotational Painting Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
随机推荐
- Java 对比Hashtable、Hashmap、Treemap有什么不同?
①基本理解 Hashtable.Hashmap.Treemap都是最常见的一些Map实现,是以键值对的形式存储和操作数据的容器类型. Hashtable是Java类库提供的一个哈希实现,本身是同步的, ...
- selenium 难定位元素,时间插件,下拉框定位,string
1.元素定位 ID定位元素: findElement(By.id(“”)); 通过元素的名称定位元素: findElement(By.name(“”)); 通过元素的html中的位置定位元素: fin ...
- 【Python】模块
廖雪峰教程笔记. [使用模块有什么好处?] 1.使用模块可以避免函数名和变量名冲突. 2.大大提高了代码的可维护性. [使用模块的注意点] 1.每一个.py文件就是一个模块. 2.每一个包目录下面必须 ...
- mybatis v jpa
mybatis的优势在于SQL的自由度上,SQL优化和返回对象的大小都是可控的.spring-data-JPA则在开发效率上有优势.
- 20145302张薇《Java程序设计》第十周学习总结
20145302 <Java程序设计>第十周学习总结 客户端和服务器端功能 客户端程序功能列表: 接收用户控制台输入 判断输入内容是否合法 按照协议格式发送数据 根据服务器端的反馈给出相应 ...
- 20145313张雪纯 《Java程序设计》第7周学习总结
20145313张雪纯 <Java程序设计>7周学习总结 教材学习内容总结 1967年定义的国际原子时,将秒的国际单位定义为铯原子辐射振动9192631170周耗费的时间. 为了简化日后对 ...
- CSS 属性的推荐书写顺序
- 使用BusyBox制作根文件系统
1.BusyBox简介 BusyBox 是很多标准 Linux 工具的一个单个可执行实现.BusyBox 包含了一些简单的工具,例如 cat 和 echo,还包含了一些更大.更复杂的工具,例如 gre ...
- CORE MVC 自定义认证
微软的认证体系,集成了EF,和它的表结构,对于我们已有的系统,或者想高度自定义的人,该怎么办呢? 答案在: https://docs.microsoft.com/en-us/aspnet/core/s ...
- bootstrap && bagging && 决策树 && 随机森林
看了一篇介绍这几个概念的文章,整理一点点笔记在这里,原文链接: https://machinelearningmastery.com/bagging-and-random-forest-ensembl ...