Stealing Harry Potter's Precious

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
  Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know, uncle Vernon never allows such magic things in his house. So Harry has to deposit his precious in the Gringotts Wizarding Bank which is owned by some goblins. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1)..... A 3×4 bank grid is shown below:

  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
  There are several test cases.
  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
  For each test case, print the minimum number of steps Dudely must take. If Dudely can't get all Harry's things, print -1.
 
Sample Input
2 3
##@
#.#
1
2  2
4 4
#@##
....
####
....
2
2 1
2 4
0 0
 
Sample Output
-1
5
           也是暴力的思想吧!
 
#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的更多相关文章

  1. 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 ...

  2. HDU 4771 Stealing Harry Potter's Precious dfs+bfs

    Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...

  3. hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的: ...

  4. 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压

    2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...

  5. hdu 4771 Stealing Harry Potter's Precious (BFS+状压)

    题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...

  6. hdu 4771 Stealing Harry Potter&#39;s Precious(bfs)

    题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...

  7. hdu 4771 Stealing Harry Potter&#39;s Precious

    题目:给出一个二维图,以及一个起点,m个中间点,求出从起点出发,到达每一个中间的最小步数. 思路:由于图的大小最大是100*100,所以要使用bfs求出当中每两个点之间的最小距离.然后依据这些步数,建 ...

  8. 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 ...

  9. Stealing Harry Potter's Precious BFS+DFS

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

随机推荐

  1. linux下openoffice的安装和启动

    下载openoffice的安装包(注意选择合适的安装包): http://www.openoffice.org/download/archive.html 一.安装openOffice1.使用tar ...

  2. [Hibernate] - Query Select

    测试了常用的一些HQL查询方法,具体HQL的强大可以参考: http://docs.jboss.org/hibernate/orm/3.5/reference/zh-CN/html/queryhql. ...

  3. 【shell】多命令执行顺序

    :举例[root@andon ~]# date;dd if=/dev/zero of=/home/1 bs=1k count=10240;date ##统计dd命令消耗时间,其中/dev/zero为空 ...

  4. ssh登录慢,等待输入密码时间长

    有时候在ssh远程登录到其他主机上时发现登录时间太长,要等待很久才会出现输入密码的提示,google了一下,发现主要有两个问题会导致ssh登录慢: 1.使用了dns反查,这样的话当ssh某个IP时,系 ...

  5. Puppet master/agent installation on RHEL7

    ==================================================================================================== ...

  6. 事件日志ID 2511:服务器服务无法重新创建 <sharename> 共享关系,因为 <address> 目录已不再存在

    服务器服务无法重新创建 QQMusicDownload 共享关系,因为 D:\QQMusic\QQMusicDownload 目录已不再存在.请运行 "net share QQMusicDo ...

  7. Mysql分区技术

    注:分区的语法可以看手册中有详细的写法和例子: show plugins; 此命令查看可有partition这个选项,有则mysql支持分区,没有的话,就可以升级一下mysql 实时监控一个命令执行情 ...

  8. Hadoop学习1--解决启动过程中的问题

    方法:http://www.aboutyun.com/thread-12694-1-1.html http://www.linuxidc.com/topicnews.aspx?tid=13 http: ...

  9. java.lang.ClassNotFoundException: org.eclipse.jetty.plus.webapp.EnvConfiguration

    最近刚接触jetty,在myeclipse8.6中加入了一个项目,运行时就出了这个java.lang.ClassNotFoundException: org.eclipse.jetty.plus.we ...

  10. Maven使用-- 编写POM

        就像Make的Makefile.Ant的build.xml一样,Maven项目的核心是pom.xml. POM(Project Object Model,项目对象模型)定义了项目的基本信息,用 ...