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. Siki_Unity_3-13_编程内功修炼-算法

    Unity 3-13 编程内功修炼 -- 算法 任务1&2:课程介绍 主要算法: 分治法 堆排序 二叉树 动态规划 贪心算法 图 任务3:分治算法 -- Divide and Conquer ...

  2. SqlServer的两种插入方式效率对比

    protected void button1_Click(object sender, EventArgs e) { DataTable dtSource = new DataTable(); dtS ...

  3. ubuntu 执行Python脚本出现: /usr/bin/env: ‘python\r’: No such file or directory

    原因: #!/usr/bin/env python 在ubuntu会变成 #!/usr/bin/env python\r 而\r 会被shell 当成参数 所以出现:  /usr/bin/env: ‘ ...

  4. 157. Unique Characters 【LintCode by java】

    Description Implement an algorithm to determine if a string has all unique characters. Example Given ...

  5. gitolite 丢失管理密钥/访问权限 解决办法

    登录到服务器. 使用完整路径克隆管理员仓库: git clone $HOME/repositories/gitolite-admin.git temp cd gitolite-admin/conf v ...

  6. SVM 为什么要从原始问题变为对偶问题来求解

    这个问题困扰了我许久,下面是我搜集整理到的答案 对偶问题将原始问题中的约束转为了对偶问题中的等式约束 方便核函数的引入 改变了问题的复杂度.由求特征向量w转化为求比例系数a,在原始问题下,求解的复杂度 ...

  7. 【转】NodeJS on Nginx: 使用nginx反向代理处理静态页面

    最近OurJS后台已经从纯node.js迁移到了Nginx+NodeJS上来了,感觉性能提升了不少,特与大家分享. Nginx ("engine x") 是一个高性能的 HTTP ...

  8. $http.get(...).success is not a function 错误解决

    $http.get(...).success is not a function 错误解决 1.6 新版本的AngularJs中用then和catch 代替了success和error,用PRomis ...

  9. Phpcms V9导航循环下拉菜单的调用技巧

    这个方法基于PC V9官方模版中的调用方法,然后利用后台的“Phpcms V9菜单是否显示设置”控制菜单是否显示出来. 先看看最后的效果: 调用方法: <div id="navbar& ...

  10. vim编辑器配置及常用命令

    最近工作不安分, 没有了刚入行时候的锐气, 不知道什么时候开始懈怠起来, 周末在电脑旁边看新闻, 搞笑图片, 追美剧, 一坐就是一天, 很是空虚. 我需要摆脱这种状态, 正好想学习一下安卓底层, An ...