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 ...
随机推荐
- Notepad++前端开发常用插件介绍 - BorisHuai前端修炼 - 博客频道 - CSDN
Notepad++前端开发常用插件介绍 - BorisHuai前端修炼 - 博客频道 - CSDN.NET http://blog.csdn.net/borishuai/article/details ...
- [mysql] linux 下mysql 5.7.12 安装
1.下载mysql wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar ...
- (转帖)BootStrap入门教程 (一)
2011年,twitter的“一小撮”工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端工具集--BootStrap.Bootstrap由MAR ...
- 如何解决WebkitBrowser使用出错“Failed to initialize activation context”
本文转载自:http://www.cnblogs.com/supjia/p/4695671.html 本篇文章主要介绍了"如何解决WebkitBrowser使用出错“Failed to in ...
- Python输入和输出
在很多时候,你会想要让你的程序与用户(可能是你自己)交互.你会从用户那里得到输入,然后打印一些结果.我们可以分别使用raw_input和print语句来完成这些功能.对于输出,你也可以使用多种多样的s ...
- Env:Gvim开发环境配置笔记--Windows篇
转自:http://www.cnblogs.com/xiekeli/archive/2012/08/13/2637176.html 加班的时候,听同事提到gvim在windows下的使用,然后突然想起 ...
- java命令行HPROF Profiler
The HPROF Profiler The Heap and CPU Profiling Agent (HPROF)是JAVA2 SDK自带的一个简单的profiler代理,它通过与Java Vir ...
- [Serializable]的应用--注册码的生成,加密和验证
1.首先定义注册类RegisterEntity [Serializable] public class RegisterEntity { public string RegisterKey; publ ...
- IGS_学习笔记10_IREP监控SOA Integration和日志设定(案例)
20150506 Created By BaoXinjian
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...