题目:给出一个二维图,以及一个起点,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. MSSQL - 存储过程OutPut返回值

    1.存储过程中不使用外部参数. 存储过程: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ========================== ...

  2. CButtonEx的实现

    要想修改CButton类按钮背景颜色和文字颜色,必须利用自绘方法对按钮进行重新绘制.这可以通过定义一个以CButton为基类的新按钮类来实现.以下为具体的实现方法: 方法一: 加入一个新类,类名:CB ...

  3. App 运营 推广相关

    基本要素 1.定位和产品 2.取个好名字,一目了然+下载冲动 3.设计一个好图标,有感性和直观的认识 4.做好产品的说明.关键字,截图(前1-2行是重点) 5.做市场的排名(相关因素如下)   (1) ...

  4. Swift - 使用UIScrollView实现页面滚动切换

    UIScrollView提供了以页面为单位滚动显示各个子页面内容的功能,每次手指滑动后会滚动一屏的内容.   要实现该功能,需要如下操作: 1,将UIScrollView的pagingEnabled属 ...

  5. niu人

    金步国简历 金步国简历 基本资料 姓名 金步国 性别 男 年龄 30 籍贯 江苏 淮安 院校 同济大学 专业 土木工程 学历 本科肄业 工作经验 5年 期望地点 长江以南 期望薪水 18000/月 个 ...

  6. 在 Ubuntu 12.04 上通过安装源安装 Open vSwitch (OVS)

    先把Ubuntu 12.04更新一下 sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade 删除 Ebtables包 s ...

  7. 【C++版】Face Alignment at 3000 FPS by Regressing Local Binary Features源码下载

    下载地址: 本帖隐藏的内容 <ignore_js_op> face-alignment-in-3000fps-master.zip (794.42 KB, 下载次数: 1076) 该源码采 ...

  8. 《转》div 中间固定 左右自适应实现

    <转自>:http://www.w3cplus.com/css/layout-column-three 对于我来说,这是一种很少碰到的布局方法,不知道大家有何体会,那么下面我们一起来看这种 ...

  9. Webbrowser控件execcommand参数详解

    2D-Position 允许通过拖曳移动绝对定位的对象.AbsolutePosition 设定元素的 position 属性为“absolute”(绝对).BackColor 设置或获取当前选中区的背 ...

  10. [Xcode]SVN的使用

    当发生冲突时: (p)postpone: -mark the conflict be resolved later 保持冲突,手动修改源文件解决冲突 (df)diff-full: -show all ...