题目:给出一个二维图,以及一个起点,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. DRP分销系统总结

    上个月看完的分销系统的视频,用了漫长的时间看这个项目视频,能安慰自己的是不光是看视频了,还做了很多自己想做的事情,比如驾照拿下来了,比如参加了一些考试,比如讲了一些课程等等.把这个系统的总结总算是补上 ...

  2. Net基础恶补

    一 自定义事件 1 之前一直都是使用事件调用来触发事件,看代码 // 定义一个事件 public event EventHandler; //触发事件 public void OnEvent(){ i ...

  3. 如何在使用摩托罗拉上的RSS阅读器应用进行一次订阅

    订阅一个CSDN的RSS为例. 1.打开RSS阅读器. 2.设置->新增订阅->手动新增 订阅URL:输入http://articles.csdn.net/api/rss.php?tid= ...

  4. 与众不同 windows phone (1) - Hello Windows Phone

    原文:与众不同 windows phone (1) - Hello Windows Phone [索引页] [源码下载] 与众不同 windows phone (1) - Hello Windows ...

  5. Java常用代码段 - 未完待续

    记录一些自己写项目常用的代码段. 格式化常用日期格式 Date date = new Date(System.currentTimeMillis()); DateFormat d3 = DateFor ...

  6. 自己定义UITabBarController

    网上大多的自己定义TabBar都是继承View的,项目中要用到path+Tabbat这种话(path用的MMDrawerController这个框架),继承View的Tabbar是无法满足条件的(不是 ...

  7. 推动Common Lisp的实际应用

    推动Common Lisp的实际应用 推动Common Lisp的实际应用

  8. ThinkPHP分页使用例子(二十一)

    原文:ThinkPHP分页使用例子(二十一) ThinkPHP分页使用 PHP代码: public function fenye(){ $User = M('Leyangjun'); // 实例化Us ...

  9. Lambda高手之路第一部分

    转http://www.cnblogs.com/lazycoding/archive/2013/01/06/2847574.html 介绍 Lambda表达式是使代码更加动态,易于扩展并且更加快速(看 ...

  10. 小议common lisp程序开发流程 - Ever 17 - 博客频道 - CSDN.NET

    小议common lisp程序开发流程 - Ever 17 - 博客频道 - CSDN.NET 小议common lisp程序开发流程 分类: lisp 2011-04-17 20:59 1316人阅 ...