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求出当中每两个点之间的最小距离.然后依据这些步数,建 ...
随机推荐
- 基于spring 3.0mvc 框架的文件上传实现
Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这样的 Web 框 ...
- Sass 主要知识点小记
Sass 主要知识点小记 以前写样式的时候,每个元素的颜色,背景色都需要重新写一遍,然后就想CSS难道没有变量么?最后就查到Sass.但当时没有静下心来好好的看一下,今天正好有时间,就在这里边看边整理 ...
- cgroup代码浅析(1)
前置:这里使用的linux版本是4.8,x86体系. cgroup_init_early(); 聊这个函数就需要先了解cgroup. cgroup概念 这个函数就是初始化cgroup所需要的参数的.c ...
- iOS的影片播放 MediaPlayer 和 AVPlayer
在iOS開發上,如果遇到需要播放影片,如開機動畫…,我們很習慣地會使用MediaPlayer來播放影片,因為很方便使用,所以就一直使用下去.但是隨著客戶的要求越來越嚴苛,尤其是過場動畫或互動效果上的表 ...
- C++11 Thread多线程的学习心得与问题
C++11 ,封装了thread的多线程的类,这样对多线程的使用更加方便. 多线程的原理我不加赘述,可以参看操作系统等参考书. 多线程代码可以最大化利用计算机性能资源,提高代码的运行效率,是常用优化方 ...
- 木块问题(The Blocks Problem,Uva 101)
不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...
- Oracle 密码文件
一.密码文件 作用:主要进行DBA权限的身份认证 DBA用户:具有sysdba,sysoper权限的用户被称为dba用户.默认情况下sysdba角色中存在sys用户,sysoper角色中存在syste ...
- Python判断字符串是否全是字母或数字
str.isnumeric(): True if 只包含数字:otherwise False.注意:此函数只能用于unicode string str.isdigit(): True if 只包含数字 ...
- 关于python字典中文显示的处理办法
最近工作中遇到字典包含中文,显示\uxxxx的问题,怎么转换都无法输入正常的中文:{"gc": "\u4eba\u751f\u7f8e\u597d", &quo ...
- 恶补数论(二) Baby-Step-Giant-Step 大步小步求离散模对数
知识概述 好吧,我承认这是我初三寒假就听过的知识,然而我现在早就高一了(又是寒假,只不过我已经在省选了...) 额,这是求离散模对数的一种算法 也就是求满足方程a^x≡b(mod p)的最小的x(其中 ...