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
 
 
最近一直再做搜索的题目  姿势不知道有没有涨了许多  可以可以
这题 还是坑了很久的 刚开始是想不断bfs 找到最近的一个点 记录步数 然后更新起点 继续bfs 但是 有bug 有反例 gg
 
    先找到k处宝藏与出发点之间的最短路 bfs处理  然后dfs 找到最短连接路
     这个地方刚开始还想用并查集 但是题目的要求的联通是首尾相接的 gg
dfs+bfs
 
 
 
 
#include<bits/stdc++.h>
using namespace std;
char a[][];
int mp[][];
map<int,int>flag;
int mpp[][];
int shorpath[][];
int dis[][]={{,},{-,},{,},{,-}};
int n,m;
int k;
int s_x,s_y;
int parent[];
int jishu=;
int A[][];
int re=;
int sum;
struct node
{
int x;
int y;
int step;
};
int Find(int n)
{
if(n!=parent[n])
n=Find(parent[n]);
return n;
}
void unio( int ss,int bb)
{
ss=Find(ss);
bb=Find(bb);
if(ss!=bb)
parent[ss]=bb;
}
queue<node>q;
node N,now;
void init_()
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
mpp[i][j]=mp[i][j];
}
int bfs(int a,int b,int c,int d)
{
init_();
while(!q.empty())
{
q.pop();
}
N.x=a;
N.y=b;
N.step=;
q.push(N);
mpp[a][b]=;
while(!q.empty())
{
now=q.front();
q.pop();
if(now.x==c&&now.y==d)
return now.step;
for(int i=;i<;i++)
{
int aa=now.x+dis[i][];
int bb=now.y+dis[i][];
if(aa>&&aa<=n&&bb>&&bb<=m&&mpp[aa][bb])
{
mpp[aa][bb]=;
N.x=aa;
N.y=bb;
N.step=now.step+;
q.push(N);
}
}
}
return -;
}
void init()
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
mp[i][j]=;
for(int i=;i<=;i++)
parent[i]=i;
re=;
}
/*void kruskal()
{
int re=0;
for(int i=0;i<jishu;i++)
{
int qq=A[i].s;
int ww=A[i].e;
//cout<<re<<endl;
if(Find(qq)!=Find(ww))
{
unio(qq,ww);
re+=A[i].x;
}
}
printf("%d\n",re);
}*/
void dfs(int n,int ce)
{
if(ce==k)
{
if(sum<re)
{
re=sum;
}
//printf("%d\n",re);
return ;
}
for(int i=;i<=k;i++)
{
if(i!=n&&flag[i]==)
{
sum+=A[n][i];
flag[i]=;
dfs(i,ce+);
sum-=A[n][i];
flag[i]=;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==)
break;
init();
getchar();
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%c",&a[i][j]);
if(a[i][j]=='@')
{
s_x=i;
s_y=j;
}
if(a[i][j]=='.'||a[i][j]=='@')
mp[i][j]=;
}
getchar();
}
scanf("%d",&k);
shorpath[][]=s_x;
shorpath[][]=s_y;
for(int i=;i<=k;i++)
scanf("%d%d",&shorpath[i][],&shorpath[i][]);
jishu=;
for(int i=;i<=k;i++)
for(int j=;j<=k;j++)
{
A[i][j]=bfs(shorpath[i][],shorpath[i][],shorpath[j][],shorpath[j][]);
}
sum=;
flag[]=;
dfs(,);
printf("%d\n",re); } return ;
}
 

HDU 4771 (DFS+BFS)的更多相关文章

  1. hdu 1242 dfs/bfs

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

  2. hdu 1241(DFS/BFS)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

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

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

  4. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  5. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  6. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

  7. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  8. HDU 5143 DFS

    分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...

  9. DFS/BFS视频讲解

    视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...

随机推荐

  1. 【springmvc+mybatis项目实战】杰信商贸-4.maven依赖+PO对+映射文件

    上一篇我们附件的增删改查功能全部完成.但是我们的附件有一个字段叫做“类型”(ctype),这里我们要使用数据字典,所以对于这一块我们要进行修改. 首先介绍一下数据字典 数据字典它是一个通用结构,跟业务 ...

  2. 爬虫1.1-基础知识+requests库

    目录 爬虫-基础知识+requests库 1. 状态返回码 2. URL各个字段解释 2. requests库 3. requests库爬虫的基本流程 爬虫-基础知识+requests库 关于html ...

  3. win7下本地运行spark以及spark.sql.warehouse.dir设置

    SparkSession spark = SparkSession .builder() .master("local[*]") .enableHiveSupport() .con ...

  4. TensorFlow入门之MNIST最佳实践-深度学习

    在上一篇<TensorFlow入门之MNIST样例代码分析>中,我们讲解了如果来用一个三层全连接网络实现手写数字识别.但是在实际运用中我们需要更有效率,更加灵活的代码.在TensorFlo ...

  5. 十六:The YARN Service Registry

    yarn 服务注册功能是让长期运行的程序注册为服务一直运行. yarn中运行的程序分为两类,一类是短程序,一类一直运行的长程序.第二种也称为服务.yarn服务注册就是让应用程序能把自己注册为服务,如h ...

  6. 五:ResourceManager High Availability RM 高可用

    RM有单点失败的风险,但是可以做HA.  RMs HA通过master/standby这种结构实现,一个master是active的,其它standby是inactive的.可能通过命令行切换主备节点 ...

  7. POJ 1921 Paper Cut(计算几何の折纸问题)

    Description Still remember those games we played in our childhood? Folding and cutting paper must be ...

  8. cp的使用

    一.形式 cp [options] source1 source2 source3 .... directory 参数意义: 参数 意义 -i 当目标文件已存在时,会询问是否覆盖 -p 连同文件的属性 ...

  9. FZU.Software Engineering1816 · First Homework -Preparation

    Introduction 041602204 : 我是喜欢狗狗(particularly Corgi & Shiba Inu.)的丁水源 : 我的爱好是音乐.电影.英语(100%!!!!).吉 ...

  10. DAY4敏捷冲刺

    站立式会议 工作安排 (1)服务器配置 已完成对微信小程序登录凭证储存至云端数据库,计划使用微信接口返回的session_id进行转化返回本地,以保持登录态. (2)数据库配置 单词学习记录+用户信息 ...