Stealing Harry Potter's Precious

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
 
题意:给你一个@起点,n*m的图,再给你q个宝石位置,求集齐所有宝石的最短路
题解:可以求出所有宝石,起点之间的最短路,再暴力求最佳方案;
///
#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的更多相关文章

  1. 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压

    2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...

  2. HDU 4771 Stealing Harry Potter's Precious

    Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  3. 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 ...

  4. hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的: ...

  5. hdu 4771 Stealing Harry Potter's Precious (BFS+状压)

    题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...

  6. hdu4771 Stealing Harry Potter's Precious(DFS,BFS)

    练习dfs和bfs的好题. #include<iostream> #include<cstdio> #include<cstdlib> #include<cs ...

  7. hdu 4771 Stealing Harry Potter&#39;s Precious(bfs)

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

  8. 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 ...

  9. hdu 4771 Stealing Harry Potter&#39;s Precious

    题目:给出一个二维图,以及一个起点,m个中间点,求出从起点出发,到达每一个中间的最小步数. 思路:由于图的大小最大是100*100,所以要使用bfs求出当中每两个点之间的最小距离.然后依据这些步数,建 ...

随机推荐

  1. webpack常见问题

    概念问题一:什么是webpack和grunt和gulp有什么不同 答案:Webpack是一个模块打包器,他可以递归的打包项目中的所有模块,最终生成几个打包后的文件.他和其他的工具最大的不同在于他支持c ...

  2. CAD实现文档坐标到视区坐标的转换(com接口Delphi语言)

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  3. openssl 下的对称加密和非对称加密

    对称加密: 在加密和解密过程中使用相同的密钥, 或是两个可以简单地相互推算的密钥的加密算法. 非对称加密: 也称为公开加密, 它需要一个密钥对, 一个是公钥, 一个是私钥, 一个负责加密, 一个负责解 ...

  4. LeetCode_16 3SumCloest

    3Sum Closest Given an array nums of n integers and an integer target, find three integers in nums su ...

  5. 洛谷——P1516 青蛙的约会

    P1516 青蛙的约会 题目描述 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件 ...

  6. Python,subprocess模块(补充)

    1.subprocess模块,前戏 res = os.system('dir') 打印到屏幕,res为0或非0 os.popen('dir') 返回一个内存对象,相当于文件流 a = os.popen ...

  7. JSP页面中的动作标识

    JSP页面中的动作标识 制作人:全心全意 包含文件标识<jsp:include> 此标识和include指令类似,用于向当前页面中包含其它的文件,且包含的文件可以是动态文件,也可以是静态文 ...

  8. str类型内置方法

    目录 str类型内置方法 用途 定义方式 常用操作和内置方法 优先掌握 需要掌握 了解 存一个值or多个值 有序or无序 可变or不可变 强化训练 str类型内置方法 用途 字符串数字.字母.下划线组 ...

  9. Hello Shiro

    [HelloWorld Shiro] 1.搭建开发环境-加入jar包 2.步骤(前提:已下载好Shiro资源包): ①找到shiro-root-1.2.3-source-release包, ②按Apa ...

  10. 九度oj 题目1473:二进制数

    题目描述: 大家都知道,数据在计算机里中存储是以二进制的形式存储的. 有一天,小明学了C语言之后,他想知道一个类型为unsigned int 类型的数字,存储在计算机中的二进制串是什么样子的. 你能帮 ...