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 ...
随机推荐
- ubuntu设置环境变量
sudo gedit /etc/environment path结尾处追加 路径,如::/opt/EmbedSky/4.3.3/bin source /etc/environment,或者重启电脑?? ...
- Neutron LBaaS Service(1)—— Neutron LBaaS Service基本知识
在OpenStack Grizzly版本中,Quantum组件引入了一个新的网络服务:LoadBalancer(LBaaS),服务的架构遵从Service Insertion框架.LoadBalanc ...
- bootstrap的datetimepicker只选择月份
本文转载自:http://blog.csdn.net/feng1603/article/details/41869523 直接上代码: //选择年月日的 startView: 2, minView: ...
- WinForm窗体拖动代码
本文转载自:http://www.cnblogs.com/ap0606122/archive/2012/10/23/2734964.html using System; using System.Co ...
- 【Struts2学习笔记-3】常量配置
Struts2常量 配置Struts2常量值有3个地方,1)在struts.properties文件中配置常量:2)在web.xml文件中配置FileterDispatcher指定初始化参数来配置常量 ...
- 可以返回执行结果的system函数加强版本
在GNU Linux C编程中,要想进行系统命令的执行的话,只提供了system接口,但是此接口并不能得到命令执行后所输出的值,而只能够得到命令是否执行成功的结果.仅仅这样的功能还是不够的,有的时候是 ...
- [tty与uart]理解线路规程的作用
转自:http://biancheng.dnbcw.info/linux/336240.html Linux OS的设备驱动有相当经典的抽象思想以及分层思想.与通信世界里面的思想相一致. 一.在Lin ...
- Html4与Html5的关键区别
HTML5是下一代HTML标准版本,4与5有很多相同之处,有HTML从头构建,比4升级到5要方便. 以下是10个关键区别: 1.HTML5最近很火,但是标准还在制定,4则十年之多了,不会6变: 2.简 ...
- java 对象传递 是 值传递 还是 引用传递?
这个问题说实话我感觉没有太大的意义. 按第一印象和c++的一些思想去理解的话对象传递是引用传递,因为传递过去的对象的值能被改变. 但是又有很多人,不知道从哪里扣出来一句,java中只有值传递,没有引用 ...
- memwatch
一.简介 memwatch可以跟踪程序中的内存泄漏和错误,能检测双重释放(double-free).错误释放(erroneous free).没有释放的内存(unfreed memory).溢出(Ov ...