题目:给出一个二维图,以及一个起点,m个中间点,求出从起点出发,到达每一个中间的最小步数。

思路:由于图的大小最大是100*100,所以要使用bfs求出当中每两个点之间的最小距离。然后依据这些步数,建立一个新的图,使用dfs求出最佳步数。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 100000000
using namespace std;
char ma[103][103];
int vis[103][103];
int map[6][6],net[6];
int m,n,k;
struct node
{
int x,y;
int k;
}t;
queue<node>q;
int bfs(int x,int y,int l,int r)
{
memset(vis,0,sizeof(vis));
while(!q.empty())
{
q.pop();
}
vis[x][y]=1;
t.x=x,t.y=y,t.k=0;
q.push(t);
while(!q.empty())
{
t=q.front();
q.pop();
x=t.x;y=t.y;
if(x==l&&y==r)
{
return t.k;
}
t.k++;
if(x>=2&&ma[x-1][y]!='#'&&!vis[x-1][y])
{
t.x=x-1;t.y=y;
vis[t.x][t.y]=1;
q.push(t);
}
if(y>=2&&ma[x][y-1]!='#'&&!vis[x][y-1])
{
t.x=x;t.y=y-1;
vis[t.x][t.y]=1;
q.push(t);
}
if(x+1<=m&&ma[x+1][y]!='#'&&!vis[x+1][y])
{
t.x=x+1;t.y=y;
vis[t.x][t.y]=1;
q.push(t);
}
if(y+1<=n&&ma[x][y+1]!='#'&&!vis[x][y+1])
{
t.x=x;t.y=y+1;
vis[t.x][t.y]=1;
q.push(t);
}
}
return INF;
}
int ans=INF;
void dfs(int x,int step,int sum)
{
if(step==k)
{
if(ans>sum) ans=sum;
return;
}
for(int i=0;i<=k;i++)
if(!net[i])
{
net[i]=1;
dfs(i,step+1,sum+map[x][i]);
net[i]=0;
}
}
int main()
{
int d[6][2];
while(cin>>m>>n,m,n)
{
int x=0,y=0,l,r,sum;
ans=INF;
for(int i=1;i<=m;i++)
{
scanf("%s",&ma[i][1]);
if(!x)
for(int j=1;j<=n;j++)
if(ma[i][j]=='@')
{
x=i,y=j;
break;
}
}
d[0][0]=x;d[0][1]=y;
cin>>k;
for(int i=1;i<=k;i++)
{
cin>>d[i][0]>>d[i][1];
}
for(int i=0;i<k;i++)
{
x=d[i][0],y=d[i][1];
for(int j=i+1;j<=k;j++)
{
l=d[j][0],r=d[j][1];
sum=bfs(x,y,l,r);
if(sum<INF) map[i][j]=map[j][i]=sum;
else map[i][j]=map[j][i]=INF;
//cout<<sum<<endl;
}
}
net[0]=1;
dfs(0,0,0);
if(ans<INF) cout<<ans<<endl;
else cout<<-1<<endl;
}
return 0;
}

hdu 4771 Stealing Harry Potter&#39;s Precious的更多相关文章

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

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

  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 dfs+bfs

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

  5. hdu4771 Stealing Harry Potter&#39;s Precious

    注意--你可能会爆内存-- 假设一个直接爆搜索词-- 队列存储器元件被减少到-- #include<iostream> #include<map> #include<st ...

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

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

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

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

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

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

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

随机推荐

  1. Delphi的DLL里如何实现定时器功能?

    一,首先引入“mmsystem”单元. 二,启动定时器: var MMTimerID: Integer; // 定时器ID MMTimerID := timeSetEvent(1000, 0, @Ti ...

  2. iot 表索引dump《2》

    iot表测试: 在create table语句后面使用organization index,就指定数据表创建结构是IOT.但是在不指定主键Primary Key的情况下,是不允许建表的. create ...

  3. 虚拟机安装中文Fedora14和C/C++IDE开发环境

    虚拟机安装中文Fedora14和C/C++IDE开发环境 2010-12-05 00:15:58 标签:中文Fedora14 IDE 开发环境 C/C++ 原创作品,允许转载,转载时请务必以超链接形式 ...

  4. css Gradients(渐变)

    渐变分为4类 1:线性渐变(Linear Gradients)- 向下/向上/向左/向右/对角方向 2:径向渐变(Radial Gradients)- 由它们的中心定义 3:对角渐变 4:角度渐变 以 ...

  5. ASIHTTPRequest开源类项目导入问题及解决方法

    在静态库project中加入ASIHTTPRequest导出lib.a.放到project里编译出一下错: Undefined symbols for architecture armv7: &quo ...

  6. 【Node.js 自己封装的库 http_parse, libuv】

    [Node.js 自己封装的库 http_parse, libuv] Node.js 介绍:一个网络框架,更多:http://www.oschina.net/p/nodejs 官网:http://no ...

  7. 代码写解压zip文件

    最近项目中服务器方返回了zip文件类型的文件,在网上搜了好多资料做成一个Demo,这里用来详解一下. ZipArchive类来源于网络.还望多多交流. 1.首先添加libz.dylib框架 2.前往h ...

  8. Java.util.zip adding a new file overwrites entire jar?(转)

    ZIP and TAR fomats (and the old AR format) allow file append without a full rewrite. However: The Ja ...

  9. 14.4.5 Configuring InnoDB Change Buffering 配置InnoDB Change Buffering

    14.4.5 Configuring InnoDB Change Buffering 配置InnoDB Change Buffering 当INSERT,UPDATE,和删除操作在表上操作, 索引列的 ...

  10. 【夯实基础】Spring在ssh中的作用

    尊重版权:http://blog.csdn.net/qjlsharp/archive/2009/03/21/4013255.aspx 写的真不错. 在SSH框假中spring充当了管理容器的角色.我们 ...