HDU 4771 Stealing Harry Potter's Precious dfs+bfs
Stealing Harry Potter's Precious
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.
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
##@
#.#
1
2 2
4 4
#@##
....
####
....
2
2 1
2 4
0 0
5
///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define inf 100000007
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//****************************************
#define maxn 105
int dis[maxn][maxn],vis[maxn][maxn],v[maxn],n,m,q,st,ed;
int mp[maxn][maxn],mps[maxn][maxn],a[maxn],b[maxn],ans;
int ss[][]={-,,,-,,,,};
bool check(int x,int y){
if(x<=||y<=||x>n||y>m)return ;return ;
}
void bfs(int x,int y){
mem(vis);memset(dis,/,sizeof(dis));
queue<pair<int ,int > >q;
q.push(make_pair(x,y));
vis[x][y]=;dis[x][y]=;
while(!q.empty()){
pair<int ,int >k;
k=q.front();q.pop();
for(int i=;i<;i++){
int xx=k.first+ss[i][];
int yy=k.second+ss[i][];
if(check(xx,yy)||mp[xx][yy]=='#'||vis[xx][yy])continue;
dis[xx][yy]=dis[k.first][k.second]+;
vis[xx][yy]=;q.push(make_pair(xx,yy));
}
}
}
void floyd(int x,int sum)
{
v[x]=;
bool flag=;
for(int i=;i<=q;i++){
if(!v[i])flag=;
}
if(flag)ans=min(ans,sum);
int tmp=inf*;
for(int i=;i<=q;i++)
{
if(!v[i]&&mp[x][i]!=){
floyd(i,sum+mps[x][i]);
}
}
v[x]=;
}
void test(){
for(int i=;i<=q;i++){
for(int j=;j<=q;j++){
cout<<mps[i][j]<<" ";
}
cout<<endl;
}
}
int main()
{ while(scanf("%d%d",&n,&m)&&n&&m){
for(int i=;i<=n;i++){
getchar();
for(int j=;j<=m;j++){
scanf("%c",&mp[i][j]);
if(mp[i][j]=='@'){st=i;ed=j;}
}
}
q=read();
for(int i=;i<=q;i++){
scanf("%d%d",&a[i],&b[i]);
}q++;a[q]=st;b[q]=ed;mem(mps);
for(int i=;i<=q;i++){
bfs(a[i],b[i]);//test();//return 0;
for(int j=;j<=q;j++){
if(j!=i){
mps[i][j]=dis[a[j]][b[j]];
}
}
}mem(v);//test();//cout<<q<<endl;
ans=inf;floyd(q,);if(ans>=inf)ans=-;
printf("%d\n",ans);
}
return ;
}
代码
HDU 4771 Stealing Harry Potter's Precious dfs+bfs的更多相关文章
- 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压
2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...
- HDU 4771 Stealing Harry Potter's Precious
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDU 4771 Stealing Harry Potter's Precious (2013杭州赛区1002题,bfs,状态压缩)
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@' 表示的是起点,'#' 表示的是障碍物不能通过,'.' 表示的是路能通过的: ...
- hdu 4771 Stealing Harry Potter's Precious (BFS+状压)
题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...
- hdu4771 Stealing Harry Potter's Precious(DFS,BFS)
练习dfs和bfs的好题. #include<iostream> #include<cstdio> #include<cstdlib> #include<cs ...
- hdu 4771 Stealing Harry Potter's Precious(bfs)
题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...
- hdu 4771 13 杭州 现场 B - Stealing Harry Potter's Precious 暴力bfs 难度:0
Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. W ...
- hdu 4771 Stealing Harry Potter's Precious
题目:给出一个二维图,以及一个起点,m个中间点,求出从起点出发,到达每一个中间的最小步数. 思路:由于图的大小最大是100*100,所以要使用bfs求出当中每两个点之间的最小距离.然后依据这些步数,建 ...
随机推荐
- The user specified as a definer ('root'@'%') does not exist 解决方法
mysql> grant all privileges on *.* to root@"%" identified by "."; Query OK, r ...
- C/c++中 数组全局变量的定义声明 与 引用声明
数组声明一次,所有的 定义声明 有切仅有一次! 别人遇到的问题如下: 在声明定义时,定义数组如下: int G_glob[100]; 在其他文件引用时声明如下: int *G_glob; 这样的操 ...
- [leetcode]Add Two Numbers——JS实现
Javascript的结构体应用,如下: function station(name, latitude, longitude){ this.name = name; ...
- POJ_2536_Gopher II
题意:n只地鼠,m个地鼠洞,地鼠必须以v的速度在s秒内钻进洞且每个洞仅能容纳一只地鼠,问最少有几只地鼠会被老鹰吃掉. 分析:最大匹配问题,将s秒内地鼠能够跑到的洞与该地鼠连成一条边,在最后得到的图中使 ...
- NSAllowsArbitraryLoadsInWebContent NSAllowsArbitraryLoads
By specifying NSAllowsArbitraryLoadsInWebContent, you are overriding NSAllowsArbitraryLoads on iOS 1 ...
- Xamarin绑定ios静态库
以下是官方的步骤介绍,我就不再一步步解释了 https://docs.microsoft.com/zh-cn/xamarin/ios/platform/binding-objective-c/walk ...
- 梦想3D控件 2018.7.26更新
下载地址: http://www.mxdraw.com/ndetail_108.html 1. 编写所有接口函数使用的CHM文档 2. 增加交互绘制功能 3. 增加案例弧形窗建模案例 4. 增 ...
- ThinkPHP---TP功能类之验证码
[一]验证码 验证码全称:captcha(全自动识别机器与人类的图灵测试),简单理解就是区分当前操作是人执行的还是机器执行的 常见验证码分3种:页面上图片形式.短信验证码(邮箱验证可以归类到短信验证码 ...
- 十二届 - CSU 1803 :2016(同余定理)
题目地址:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1803 Knowledge Point: 同余定理:两个整数a.b,若它们除以整数m所 ...
- js之循环语句
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...