HDU 4771 Stealing Harry Potter's Precious
Stealing Harry Potter's Precious
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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.
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
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#define Min(a ,b ) ((a)<(b)?(a):(b))
using namespace std;
const int Max_N= ;
const int inf= ;
int N , M ,K ;
char str[Max_N][Max_N] ;
int d[][]={ {,}, {,} ,{- ,} ,{ ,-} } ; int cango(int x ,int y){
return <=x&&x<=N&&<=y&&y<=M ;
} struct Node{
int X ;
int Y ;
int step ;
Node(){} ;
Node(int x ,int y ,int s):X(x),Y(y),step(s){} ;
friend bool operator <(const Node A ,const Node B){
return A.step>B.step ;
}
} ; int dist[Max_N] [Max_N] ; void spfa(int x ,int y ){
priority_queue<Node>que ;
que.push(Node(x,y,)) ;
for(int i= ; i<=N ; i++)
for(int j= ; j<=M ; j++)
dist[i][j]=inf ;
dist[x][y]= ;
while(!que.empty() ){
Node now = que.top() ;
que.pop() ;
for(int i= ; i< ; i++){
int x=now.X + d[i][] ;
int y=now.Y + d[i][] ;
if(!cango(x,y))
continue ;
if(str[x][y] != '.')
continue ;
if( now.step+<dist[x][y] ){
dist[x][y] = now.step + ;
que.push( Node(x,y,dist[x][y]) ) ;
}
}
}
} struct Point {
int X ;
int Y ;
} ; Point p[] ;
int grid[][] ;
int sele[] ;
bool use[] ;
int ans ; void dfs(int id){
if(id>K){
int sum = ;
for(int i = ; i <= K ; i++)
sum+=grid[ sele[i-] ][ sele[i] ] ;
ans=Min(ans,sum) ;
}
for(int i = ; i <= K ; i++ ){
if(use[i]){
sele[id] = i ;
use[i]= ;
dfs(id+) ;
use[i] = ;
}
}
} int main() {
while(cin>>N>>M){
if(N==&&M==)
break ;
for(int i= ; i<=N ; i++)
scanf("%s",str[i]+) ;
for(int i= ; i<=N ; i++)
for(int j= ; j<=M ; j++){
if(str[i][j]=='@'){
p[].X = i ;
p[].Y = j ;
str[i][j] = '.' ;
}
}
cin>>K ;
for(int i = ; i<=K ; i++)
scanf("%d%d",&p[i].X,&p[i].Y) ;
for(int i = ; i <= K ; i++){
spfa(p[i].X , p[i].Y ) ;
for(int j = ; j <= K ; j++){
grid[i][j] = dist[ p[j].X ][ p[j].Y ] ;
}
}
memset(use,,sizeof(use)) ;
sele[]= ;
ans=inf ;
dfs() ;
if(ans==inf)
puts("-1") ;
else
cout<<ans<<endl ;
}
return ;
}
HDU 4771 Stealing Harry Potter's Precious的更多相关文章
- 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 (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@' 表示的是起点,'#' 表示的是障碍物不能通过,'.' 表示的是路能通过的: ...
- 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压
2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...
- hdu 4771 Stealing Harry Potter's Precious (BFS+状压)
题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...
- hdu 4771 Stealing Harry Potter's Precious(bfs)
题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...
- hdu 4771 Stealing Harry Potter's Precious
题目:给出一个二维图,以及一个起点,m个中间点,求出从起点出发,到达每一个中间的最小步数. 思路:由于图的大小最大是100*100,所以要使用bfs求出当中每两个点之间的最小距离.然后依据这些步数,建 ...
- hdu 4771 13 杭州 现场 B - Stealing Harry Potter's Precious 暴力bfs 难度:0
Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. W ...
- Stealing Harry Potter's Precious BFS+DFS
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
随机推荐
- ef 高级操作
一:动态拼接条件查询 var expression = PredicateBuilder.True<OQC_MES_INF_UL_QMS_OFFLINE>(); SYS_ROLES_CON ...
- ADB工具 获取ROOT权限及复制文件方法
adb push d:\tm3_sqlit.db data/zouhao/tm3_sqlit.dbadb pull data/zouhao/tm3_sqlit.db d:\tm3_sqlit.db a ...
- VB中的+与&符号的区别
字符串运算符和字符串表达式 字符串运算符的作用是将两个字符串连接成一个字符串,经常形象的叫做连接符. 在VB中就提供了两种连接用的字符串运算符 “&”.“+” 例如 “VB”+“编程入门” ...
- MySQL优化—工欲善其事,必先利其器之EXPLAIN
最近慢慢接触MySQL,了解如何优化它也迫在眉睫了,话说工欲善其事,必先利其器.最近我就打算了解下几个优化MySQL中经常用到的工具.今天就简单介绍下EXPLAIN. 内容导航 id select_t ...
- sealed修饰符
sealed(C# 参考) 当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承. 在下面的示例中,类 B 从类 A 继承,但是任何类都不能从类 B 继承. class A {} s ...
- C# 如何将字符串形式的” \\u1234 “ 为 “ \u1234” 的unicode编码解码为中文
using System.Text.RegularExpressions; decodedStr = Regex.Unescape(escapeUnicodeStr);
- hdu 1536 S-Nim(sg函数模板)
转载自:http://blog.csdn.net/sr_19930829/article/details/23446173 解题思路: 这个题折腾了两三天,参考了两个模板,在这之间折腾过来折腾过去,终 ...
- 8.4c#递归
一.概念conception: 函数体内调用本函数自身,直到符合某一条件不再继续调用. 二.应满足条件factor: (1)有反复执行的过程(调用自身): (2)有跳出反复执行过程的条件(函数出口) ...
- [MySQL] 常用SQL的优化--18.4
这里介绍下,Insert.Group By等SQL语句的优化方法: 1.大批量数据插入 当load命令导入数据的时候,可以进行适当的设置提高导入速度. 1.1 对于MyISAM表,可以先禁用非唯一索引 ...
- studio--如何将Eclipse中的项目迁移到Android Studio 中
如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的是生成Build Gradle的文件.因为Android Studi ...