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. TW实习日记:第29-30天

    这两天挺忙,赶工期,改bug.项目现场的同事说客户火大得不行.可是谁叫你们谈工期谈的这么紧,完全不考虑开发的情况,真的是烦人这种事情.这两天遇到的最有难度的一个点就是附件预览,搞这个改到晚上11点. ...

  2. Qt+opencv:读取、显示图像

    GitHub:点击下载完整代码 本文主要是使用Qt与opencv将图像进行显示在QT界面上. 程序运行后的界面如下所示: (由于只有打开图像之后,才能对图像进行翻转,所以程序设置为读取图像成功之后才能 ...

  3. 【20180808模拟测试】T2 k-斐波那契

    描述 k-斐波拉契数列是这样的 f(0)=k;f(1)=k;f(n)=(f(n-1)+f(n-2))%P(n>=2); 现在我们已经知道了f(n)=1,和P: k的范围是[1,P); 求k的所有 ...

  4. 什么是Spark

    什么是Spark Apache Spark是一个开源集群运算框架, 相对于Hadoop的MapReduce会在运行完工作后将中介数据存放到磁盘中,Spark使用了存储器内运算技术,能在数据尚未写入硬盘 ...

  5. n! 阶乘

    其实1.2.3.4.6.7…都是可以不用考虑的,因此选择以5为迭代步数即可. 首先,这些数字都可以不用进行%5(对5取余数)运算,因此每次循环时可以直接将函数的count变量直接加1.其次,考虑25. ...

  6. 【未完】训练赛20190304:KMP+树状数组+线段树+优先队列

    头炸了啊,只做出L题,前两天刚看的Shawn zhou的博客学习的,幸亏看了啊,否则就爆零了,发现题目都是经典题,线段树,KMP,我都没看过,最近又在复习考研,真后悔大一大二没好好学习啊,得抽时间好好 ...

  7. 【转】Backbone.js学习笔记(一)

    文章转自: http://segmentfault.com/a/1190000002386651 基本概念 前言 昨天开始学Backbone.js,写篇笔记记录一下吧,一直对MVC模式挺好奇的,也对j ...

  8. JS中Document节点总结

    document对象是documentHTML的一个实例,也是window对象的一个属性,因此可以将document对象作为一个全局对象来访问. Document节点的子节点可以是DocumentTy ...

  9. 软件工程-东北师大站-第二次作业psp

    1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 本周PSP饼状图

  10. BluetoothDevice详解

    一. BluetoothDevice简介 1. 继承关系 public static Class BluetoothDevice extends Object implement Parcelable ...