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 ...
随机推荐
- 试用fastJSON
实体类 User.java package com.test.fastjson.entity; import java.util.Date; public class User { private L ...
- 【Spring-AOP-学习笔记-7】@Around增强处理简单示例
阅读目录 简单介绍 章节1:项目结构 章节2:定义切面类.连接点注解类 章节3:为待增强的方法--添加注解声明 章节4:AspectJ配置文件 章节5:测试类xxx 章节6:测试结果 Around 增 ...
- 【转】String.format详解
一.前言 String.format 作为文本处理工具,为我们提供强大而丰富的字符串格式化功能,为了不止步于简单调用 String.format("Hello %s", " ...
- 如何把python最小化安装在客户机上面
因为想尝试在我们的桌面软件中加入python支持,所以想简化python的库,到时候直接放到客户机上面,并且放到我们的目录下,尽量免去不必要的东西,也不要影响机子,不过当写好的程序放到测试机子上后,老 ...
- framework 安装出错 1603
安装frame work 3.5的时候老是出现 1603错误. 百度了一圈,各种方法都试了,仍不行. 像: 1.打开临时目录看安装日志,然后修改注册表Main的权限. 2.停止服务Cryptograp ...
- PHP之单例模式的实现
单例模式: 单例模式又称职责模式:简单的说,一个对象(在学习设计模式之前,需要比较了解面向对象思想)只负责一个特定的任务: 单例类: 1.构造函数需要标记为private(访问控制:防止外部代码使用n ...
- RMAN_学习实验2_RMAN Duplicate复制数据库过程(案例)
待整理 对于基于生产环境下的数据库的版本升级或者测试新的应用程序的性能及其影响,备份恢复等等,我们可以采取从生产环境以克隆的方式将其克隆到本地而不影响生产数据库的正常使用.实现这个功能我们可以借助rm ...
- CF 219D Choosing Capital for Treeland 树形DP 好题
一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) ...
- tcpdump学习
#直接启动tcpdump将监视第一个网络接口上所有流过的数据包 -n不解析地址到nametcpdump -n #监视指定网络接口的数据包,不指定则为 eth0tcpdump -i eth1 #监视指定 ...
- 转-Android 之 使用File类在SD卡中读取数据文件
如果需要在程序中使用sdcard进行数据的存储,那么需要在AndroidMainfset.xml文件中 进行权限的配置: Java代码: <!-- 在sd中创建和删除文件的权限 --> ...