hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771
题目意思:'@' 表示的是起点,'#' 表示的是障碍物不能通过,'.' 表示的是路能通过的;
目的:让你从 '@' 点出发,然后每个点只能走一次,求出最小的距离;
解题思路:先用 bfs 求解出任意两点之间的距离,用 ans[i][j],表示点 i 到点 j 的距离;
然后用 dfs 递归求出从起点经过所有点的距离中,比较出最小的;
AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<cmath>
using namespace std;
int dir[][]={,,,-,,,-,};
int m,n;
int sx,sy,T;
int ans[][];//点之间的距离
char Map[][];
int vis[][];//标记路径
struct node
{
int x,y,step;
}a[],now,eed;
int bfs(int T1,int ee,int vv)
{
int TT=;
vis[a[T1].x][a[T1].y]=vv;//每次标记的都发生了变化,这样vis数组不用每次都清零
queue< node >p;
now.x = a[T1].x;
now.y = a[T1].y;
now.step = ;
p.push(now);
while(!p.empty())
{
now=p.front();
p.pop();
for(int i=T1+; i<=T; i++)
{
if(now.x == a[i].x && now.y == a[i].y)
{
ans[T1][i]=ans[i][T1] = now.step;
TT++;
}
}
for(int i=; i<; i++)
{
eed.x = now.x + dir[i][]; eed.y = now.y + dir[i][];
if(eed.x>= && eed.x<=m && eed.y>= && eed.y<=n && vis[eed.x][eed.y]!=vv && Map[eed.x][eed.y]!='#')
{
eed.step = now.step+;
vis[eed.x][eed.y] = vv;
p.push(eed);
}
}
if(TT == ee) //如果该访问的点都访问了,直接返回;
return ;
}
return - ;//如果其中有点不能访问到,直接返回-1,输出 -1 ;
} int net[],ans1;
void dfs(int x,int step,int sum)
{
if(step==T)
{
if(ans1>sum) ans1=sum;
return;
}
for(int i=;i<=T;i++)
if(!net[i])
{
net[i]=;
dfs(i,step+,sum+ans[x][i]);
net[i]=;
}
}
int main()
{
int x,y;
// freopen("in1.txt","r",stdin);
// freopen("out1.txt","w",stdout);
while(cin>>m>>n && m+n)
{
ans1=;
memset(ans,,sizeof(ans));
memset(net,,sizeof(net));
memset(vis,,sizeof(vis));
for(int i=; i<=m; i++)
for(int j=; j<=n; j++)
{
scanf(" %c",&Map[i][j]);
if(Map[i][j] == '@')
{
sx= i; sy = j;
}
}
scanf("%d",&T);
a[].x=sx; a[].y=sy;
for(int i=; i<=T; i++)
{
scanf("%d %d",&x,&y);
a[i].x = x;
a[i].y = y;
}
int flag = ;
for(int i = ; i<T; i++)
{ flag = bfs(i,T-i,i+);
if(flag == -)
break;
}
if(flag == -)
printf("-1\n");
else
{
net[]=;
dfs(,,);
printf("%d\n",ans1);
}
}
return ;
}
hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径的更多相关文章
- 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 dfs+bfs
Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...
- 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】BFS+状压
2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...
- hdu 4771 Stealing Harry Potter's Precious (BFS+状压)
题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...
- hdu 4771 Stealing Harry Potter's Precious(bfs)
题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...
- hdu 4771 Stealing Harry Potter's Precious
题目:给出一个二维图,以及一个起点,m个中间点,求出从起点出发,到达每一个中间的最小步数. 思路:由于图的大小最大是100*100,所以要使用bfs求出当中每两个点之间的最小距离.然后依据这些步数,建 ...
- 2013 ACMICPC 杭州现场赛 I题
#include<iostream> #include<cstring> #include<algorithm> #include<cmath> #in ...
- 2013 Asia acm Hangzhou Regional Contest 杭州现场赛
B Stealing Harry Potter's Precious 题目大意:给定一个n*m的地图,某些点可以走,某些点可以走某些点不可以走,给定一个起点,又给出了k个点k<=4,要求从起点 ...
随机推荐
- [Linux] ubuntu 软件安装必须看的网址
http://wiki.ubuntu.org.cn/index.php?title=Qref/Apps&variant=zh-hans 这里介绍了unbuntu常用软件及其安装,免得你百度来百 ...
- 重写alert方法完成类似gmail的友好提示
当在网页中调用aelrt()方法的时候,系统会自动显示友好的提示方式 . 下面是css样式控制代码: /*----------------------------------------------- ...
- 【Todo】机器学习系列
看了这篇文章很好,有很多指导性思想: http://www.cnblogs.com/tornadomeet/p/3395593.html 另外这个人的系列文章里面也有很多干货. 就看这个系列的吧: h ...
- DXR
https://github.com/ConfettiFX/The-Forge/blob/master/CommonRaytracing_3/ThirdParty/DXR/doc/D3D12%20Ra ...
- hdu 1408 acdearm "Money, Money, Money"
#include<stdio.h> int main() { long long int x; while(~scanf("%lld",&x)) { if(x% ...
- About stats collected
pg_class.relpages pg_class.reltuples仅仅是近似值,和实际数据会有点误差: 新建空表.首次insert对自己主动收集和更新统计信息,影响的表pg_class\pg_s ...
- PHP数组问题
转换为数组 对于任意 integer , float , string , boolean 和 resource 类型,如果将一个值转换为数组,将得到一个仅有一个元素的数组,其下标为 0,该元素即为此 ...
- 代码可读性艺术在Andorid中的体现
前言 最近接手的一些项目,不同的人编码风格迥异,类里的变量.方法的定义穿插,注释极为稀少,更有一些变量和方法的命名非常近似,例如表示播放队列的"playQueue"和表示歌单的&q ...
- [Mac A]为什么国外程序员爱用 Mac?
from http://www.vpsee.com/2009/06/why-programmers-love-mac/ Mac 在国外很受欢迎,尤其是在 设计/web开发/IT 人员圈子里.普通用户喜 ...
- 为何 IntelliJ IDEA 比 Eclipse 更好
http://www.oschina.net/news/26929/why-intellij-is-better-than-eclipse圣战 有一些没有唯一正确答案的“永恒”的问题,例如哪个更好:是 ...