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. 并发HashMap的put操作引起死循环

    今天研读Java并发容器和框架时,看到为什么要使用ConcurrentHashMap时,其中有一个原因是:线程不安全的HashMap, HashMap在并发执行put操作时会引起死循环,是因为多线程会 ...

  2. Python3 Tkinter-Radionbutton

    1.创建单选按钮 from tkinter import * root=Tk() Radiobutton(root,text='b1').pack() Radiobutton(root,text='b ...

  3. Python3 数据类型-字典

    字典是一种可变数据类型,且可存储任意类型对象. 字典使用大括号"{}"括起来,由键(key)和值(values)组成,键只能使用不可变类型定义,值可以使用可变类型{'键':'值'} ...

  4. SGU 194 Reactor Cooling(无源无汇上下界可行流)

    Description The terrorist group leaded by a well known international terrorist Ben Bladen is bulidin ...

  5. iOS开发Interface Builder技巧

    1.使view的Size与view中的Content相适应:选中任意的一个view,然后Editor->Size to Fit Content,或者简单的按 ⌘=接着就会按照下面的规则对选中vi ...

  6. TCP系列17—重传—7、SACK下的重传

    我们之前介绍SACK选项的时候说过,SACK可以把接收端系列号空间的洞反映给发送端,因此发送端可以更充分的理解接收端的情况,而进行更好的重传恢复过程.这种过程有时候也叫做advanced loss r ...

  7. Jrebel 工具学习

    Jrebel 可快速实现热部署,节省了大量重启时间,提高了个人开发效率.网上可搜索到破解版. http://baike.baidu.com/link?url=wuzv7Wa7SMUKltJr-dyta ...

  8. C#中pictureBox笔记

    if (File.Exists(productInfo.预览图路径)) this.picPreview.Image = BitmapFactory.Alloc(productInfo.预览图路径, f ...

  9. MVP开发模式的理解

    1.MVP是什么 如果从层次关系来讲,MVP属于Presentation层的设计模式.对于一个UI模块来说,它的所有功能被分割为三个部分,分别通过Model.View和Presenter来承载.Mod ...

  10. matplotlib中什么是后端

    在很多网上文档和邮件列表中提到了"后端",并且很多初学者会对这个术语迷惑.matplotlib把不同使用情形和输出格式作为目标.一些人用matplotlib在python shel ...