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. 使用openssl搭建CA并颁发服务器证书

    本来整理了一份执行脚本,但是没有找到附件功能.只好直接贴当时自己看过的链接了. 文章标题:Openssl Certificate Authority 转载链接:https://jamielinux.c ...

  2. 【Linux】CentOS安装Jenkins

    sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo sudo rpm -- ...

  3. sort 排序 自定义排序算法的使用

    // struct sort_by_pt// {// bool operator()(const std::pair<CString, AcGePoint3d> a, const std: ...

  4. 梦想MxWeb3D协同设计平台 2019.01.24更新

    SDK开发包下载地址:http://www.mxdraw.com/ndetail_10124.html1.  编写快速入门教程2.  重构前端代码,支持一个页面多个三维控件同时加载,或二维和三维同时加 ...

  5. find命令查找和替换

    find命令查找和替换 语法: find -name '要查找的文件名' | xargs perl -pi -e 's|被替换的字符串|替换后的字符串|g' #查找替换当前目录下包含字符串并进行替换 ...

  6. BeanFactory和ApplicationContext

    BeanFactory是一个类的通用工厂,可以创建并管理各种类的对象 Bean工厂是Spring框架最核心的接口,它提供了高级Ioc的配置机制.BeanFeactory使管理不同类的Java对象成为可 ...

  7. 04StringBuffer相关知识、Arrays类、类型互换、正则、Date相关

    04StringBuffer相关知识.Arrays类.类型互换.正则.Date相关-2018.7.12 1.StringBuffer A:StringBuffer的构造方法: public Strin ...

  8. zabbix登录密码重置方法

    注:由于账号较多,难免忘记账号,下面是找回zabbix登录密码的过程. 一.登录zabbix数据库 [root@123 ~]# mysql -uroot -p密码 二.修改zabbix密码 mysql ...

  9. buf.writeInt8()函数详解

    buf.writeInt8(value, offset[, noAssert]) value {Number} 需要被写入到 Buffer 的字节 offset {Number} 0 <= of ...

  10. 【Codeforces 1036C】Classy Numbers

    [链接] 我是链接,点我呀:) [题意] 让你求出只由3个非0数字组成的数字在[li,ri]这个区间里面有多少个. [题解] 只由3个非0数字组成的数字在1~10^18中只有60W个 dfs处理出来之 ...