Stealing Harry Potter's Precious

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
 
Source
 
题意:给你一个@起点,n*m的图,再给你q个宝石位置,求集齐所有宝石的最短路
题解:可以求出所有宝石,起点之间的最短路,再暴力求最佳方案;
///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define inf 100000007
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//****************************************
#define maxn 105
int dis[maxn][maxn],vis[maxn][maxn],v[maxn],n,m,q,st,ed;
int mp[maxn][maxn],mps[maxn][maxn],a[maxn],b[maxn],ans;
int ss[][]={-,,,-,,,,};
bool check(int x,int y){
if(x<=||y<=||x>n||y>m)return ;return ;
}
void bfs(int x,int y){
mem(vis);memset(dis,/,sizeof(dis));
queue<pair<int ,int > >q;
q.push(make_pair(x,y));
vis[x][y]=;dis[x][y]=;
while(!q.empty()){
pair<int ,int >k;
k=q.front();q.pop();
for(int i=;i<;i++){
int xx=k.first+ss[i][];
int yy=k.second+ss[i][];
if(check(xx,yy)||mp[xx][yy]=='#'||vis[xx][yy])continue;
dis[xx][yy]=dis[k.first][k.second]+;
vis[xx][yy]=;q.push(make_pair(xx,yy));
}
}
}
void floyd(int x,int sum)
{
v[x]=;
bool flag=;
for(int i=;i<=q;i++){
if(!v[i])flag=;
}
if(flag)ans=min(ans,sum);
int tmp=inf*;
for(int i=;i<=q;i++)
{
if(!v[i]&&mp[x][i]!=){
floyd(i,sum+mps[x][i]);
}
}
v[x]=;
}
void test(){
for(int i=;i<=q;i++){
for(int j=;j<=q;j++){
cout<<mps[i][j]<<" ";
}
cout<<endl;
}
}
int main()
{ while(scanf("%d%d",&n,&m)&&n&&m){
for(int i=;i<=n;i++){
getchar();
for(int j=;j<=m;j++){
scanf("%c",&mp[i][j]);
if(mp[i][j]=='@'){st=i;ed=j;}
}
}
q=read();
for(int i=;i<=q;i++){
scanf("%d%d",&a[i],&b[i]);
}q++;a[q]=st;b[q]=ed;mem(mps);
for(int i=;i<=q;i++){
bfs(a[i],b[i]);//test();//return 0;
for(int j=;j<=q;j++){
if(j!=i){
mps[i][j]=dis[a[j]][b[j]];
}
}
}mem(v);//test();//cout<<q<<endl;
ans=inf;floyd(q,);if(ans>=inf)ans=-;
printf("%d\n",ans);
}
return ;
}

代码

HDU 4771 Stealing Harry Potter's Precious dfs+bfs的更多相关文章

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

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

  2. HDU 4771 Stealing Harry Potter's Precious

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

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

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

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

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

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

  6. hdu4771 Stealing Harry Potter's Precious(DFS,BFS)

    练习dfs和bfs的好题. #include<iostream> #include<cstdio> #include<cstdlib> #include<cs ...

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

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

  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. hdu 4771 Stealing Harry Potter&#39;s Precious

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

随机推荐

  1. tomcat 访问IP直接访问项目

    apache-tomcat-7.0.52\conf下server.xml文件 <Connector connectionTimeout="20000" port=" ...

  2. js延时加载的方法

    js的延迟加载有助与提高页面的加载速度,以下是延迟加载的几种方法: 1.使用setTimeout延迟方法的加载时间 延迟加载js代码,给网页加载留出更多时间 <script type=" ...

  3. VMware vCenter 6.5 安装及群集配置介绍

    一.介绍 VMware vCenter Server 提供了一个可伸缩.可扩展的平台,为虚拟化管理奠定了基础.可集中管理VMware vSphere环境,与其他管理平台相比,极大地提高了 IT 管理员 ...

  4. 04Microsoft SQL Server 数据库创建,查看,使用,修改及删除

    Microsoft SQL Server 数据库创建,查看,使用,修改及删除 创建数据库 创建普通数据库 USE [master] GO CREATE DATABASE [MyDataBase] -- ...

  5. 字符、散列、模拟--P1055 ISBN号码

    题目描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如x-xxx-xxxxx-x,其中符号-就是分隔符(键盘上的减号),最后一位是识别 ...

  6. MySQL Docker方式安装

    以5.7版本为例 1 配置mysql配置文件编辑/etc/my.cnf,添加以下内容: [mysqld] skip-host-cache skip-name-resolve datadir=/var/ ...

  7. Laravel学习:请求到响应的生命周期

    Laravel请求到响应的整个执行过程,主要可以归纳为四个阶段,即程序启动准备阶段.请求实例化阶段.请求处理阶段.响应发送和程序终止阶段. 程序启动准备阶段 服务容器实例化 服务容器的实例化和基本注册 ...

  8. mac下Redis安装和使用

    前言 本篇文章主要讲述了Mac下Redis的安装和使用的经验,并将python如何操作Redis做了简单介绍. 1. redis 安装 和启动 1.1 用brew安装 查看系统是否已经安装了Redis ...

  9. vue列表渲染,以及鼠标点击改变样式的问题

    在实际项目中,我们进场会遇到鼠标点击该表某个DOM元素的样式,在原生的js或者jquery中,我们会比较熟练的实现这个需求,但是在vue中怎么实现呢? 直接操作DOM?NO!NO! 既然我们的项目使用 ...

  10. CF2B The least round way

    [题解] 可以发现10的因数除了1和10之外只有2和5了,那么走过的路径上各个数字的2的因数个数之和.5的因数个数之和中较小的一个即是答案.这样的话DP即可.同时需要注意有0的情况,有0的时候有一个答 ...