HDU 4771 (DFS+BFS)

时间:2022-09-07 08:13:40
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:HDU 4771 (DFS+BFS)  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&sol;bfs

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

  2. hdu 1241&lpar;DFS&sol;BFS&rpar;

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

  3. hdu 4771 Stealing Harry Potter&amp&semi;&num;39&semi;s Precious&lpar;bfs&rpar;

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

  4. ID(dfs&plus;bfs&rpar;-hdu-4127-Flood-it&excl;

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

  5. DFS&sol;BFS&plus;思维 HDOJ 5325 Crazy Bobo

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

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

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

  7. &lbrack;LeetCode&rsqb; 130&period; Surrounded Regions&lowbar;Medium tag&colon; DFS&sol;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&sol;BFS视频讲解

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

随机推荐

  1. Linux tree命令

    Linux tree命令用于以树状图列出目录的内容. 执行tree指令,它会列出指定目录下的所有文件,包括子目录里的文件. 语法 tree [-aACdDfFgilnNpqstux][-I <范 ...

  2. a标签的target指向iframe

    <html> <head> <meta charset="utf-8" /> </head> <body> <ta ...

  3. MySQL解决插入emoji表情失败的问题

    普通的字符串或者表情都是占位3个字节,所以utf8足够用了,但是移动端的表情符号占位是4个字节,普通的utf8就不够用了,为了应对无线互联网的机遇和挑战.避免 emoji 表情符号带来的问题.涉及无线 ...

  4. &lbrack;转&rsqb;World Wind学习总结一

    WW的纹理,DEM数据,及LOD模型 以earth为例 1. 地形数据: 默认浏览器纹理数据存放在/Cache/Earth/Images/NASA Landsat Imagery/NLT Landsa ...

  5. nginx作反向代理,实现负载均衡

    nginx作反向代理,实现负载均衡按正常的方法安装好 ngixn,方法可参考http://www.cnblogs.com/lin3615/p/4376224.html其中作了反向代理的服务器的配置如下 ...

  6. JAVA中的栈和堆

    JAVA在程序运行时,在内存中划分5片空间进行数据的存储.分别是:1:寄存器.2:本地方法区.3:方法区.4:栈.5:堆. 基本,栈stack和堆heap这两个概念很重要,不了解清楚,后面就不用学了. ...

  7. 微信开发之获取jsapi&lowbar;ticket

    一.获取流程 1.获取 access_token 2.通过access_token换取 jsapi_ticket 3.签名算法 签名生成规则如下:参与签名的字段包括noncestr(随机字符串), 有 ...

  8. JavaScript解析机制与闭包原理实例详解

    js代码解析机制: js代码解析之前会创建一个如下的词法环境对象(仓库):LexicalEnvironment{ } 在扫描js代码时会把: 1.用声明的方式创建的函数的名字; 2.用var定义的变量 ...

  9. 20165304《JAVA程序设计》第四周学习总结

    教材内容总结 第五章 子类与继承 1.子类声明中通常用关键字extend来定义一个子类(class 子类名 extend 父类名{}) 2.子类和父类在同一包中的继承性,继承的成员变量或方法的访问权限 ...

  10. Python学习笔记18-发送邮件

    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块,email负责构造邮件, ...