题目链接: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) 带权值的路径的更多相关文章

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

  2. HDU 4771 Stealing Harry Potter's Precious dfs+bfs

    Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...

  3. HDU 4771 Stealing Harry Potter's Precious

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

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

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

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

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

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

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

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

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

  8. 2013 ACMICPC 杭州现场赛 I题

    #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #in ...

  9. 2013 Asia acm Hangzhou Regional Contest 杭州现场赛

     B Stealing Harry Potter's Precious 题目大意:给定一个n*m的地图,某些点可以走,某些点可以走某些点不可以走,给定一个起点,又给出了k个点k<=4,要求从起点 ...

随机推荐

  1. postgres--wal

    WAL机制 持久性指事务提交后对系统的影响必须是永久的,即使系统意外宕机,也必须确保事务修改的数据已真正永久写入到永久存储中. 最简单的实现方法,是在事务提交后立即将修改的数据写到磁盘.但磁盘和内存之 ...

  2. 通过HTTP发包工具了解HTTP协议

    一.HTTP.pl功能简介 HTTP.pl perl编写的发包工具,简化版本curl,像curl致敬(唉,“致敬”都被于妈玩坏了).   该发包工具支持HEAD,GET,METHOD三种基本请求方法, ...

  3. 一步一步学RenderMonkey(5)--渲染到纹理(RTT) 【转】

    转载请注明出处:  http://blog.csdn.net/tianhai110 渲染到纹理: 新建一个空effect; 添加渲染目标纹理, Add Texture-> Add Render ...

  4. IntelliJ IDEA 创建 java Maven项目

    1.下载安装Maven 下载官网:http://maven.apache.org/download.cgi 下载解压到当前目录并建立LocalWarehouse文件夹,该文件夹为自己的文件仓库做存储. ...

  5. 在android中画圆形图片的几种办法

    在开发中常常会有一些需求,比方显示头像,显示一些特殊的需求,将图片显示成圆角或者圆形或者其它的一些形状. 可是往往我们手上的图片或者从server获取到的图片都是方形的.这时候就须要我们自己进行处理, ...

  6. DirectX游戏开发——从一个小游戏開始

    本系列文章由birdlove1987编写,转载请注明出处. 文章链接: http://blog.csdn.net/zhurui_idea/article/details/26364129 写在前面:自 ...

  7. [HTML5] Add an SVG Image to a Webpage and Get a Reference to the Internal Elements in JavaScript

    We want to show an SVG avatar of the patio11bot, so we'll do that in three ways: Using an img tag - ...

  8. [Angular] Extract Implementation Details of ngrx from an Angular Application with the Facade Pattern

    Extracting away the implementation details of ngrx from your components using the facade pattern cre ...

  9. hdu1800Flying to the Mars (字典树)

    Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the population growi ...

  10. NSData 转 bytes 字节数据

    NSData 转 bytes 字节数据 NSData *data = [NSData dataWithContentsOfFile:filePath]; NSUInteger len = [data ...