hdu 4771 Stealing Harry Potter's Precious
题目:给出一个二维图,以及一个起点,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's Precious的更多相关文章
- 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
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 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 ...
- hdu4771 Stealing Harry Potter's Precious
注意--你可能会爆内存-- 假设一个直接爆搜索词-- 队列存储器元件被减少到-- #include<iostream> #include<map> #include<st ...
- hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@' 表示的是起点,'#' 表示的是障碍物不能通过,'.' 表示的是路能通过的: ...
- 【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 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 ...
随机推荐
- NYOJ 1066 CO-PRIME(数论)
CO-PRIME 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 This problem is so easy! Can you solve it? You are ...
- python与其它语言进行比較
近期python语言貌似比較火, 今天闲来无事,简单的看了下,算是个入门吧.一门语言之所以值得这么多人去学,必然有它的独到之处,以下我们就用python和其它语言做个比較. Pythond VS C# ...
- java中遍历MAP,嵌套map的几种方法
java中遍历MAP的几种方法 Map<String,String> map=new HashMap<String,String>(); map.put("us ...
- [置顶] head first 设计模式之----Observer pattern
浅谈设计模式之----观察者模式 观察者模式也是我们日常程序编写中碰到比较多的一种设计模式.首先,所谓观察者模式定义就是指:在对象之间定义了一对多的依赖,这样一来,当一个对象的状态发生变化的 ...
- Delphi读写UTF-8、Unicode格式文本文件
// UTF-8文件写入函数procedure SaveUTFFile(const FileName: string; S: string; WriteHeader: Boolean = True); ...
- 14.4.7 Configuring the Number of Background InnoDB IO Threads 配置 后台InnoDB IO Threads的数量
14.4.7 Configuring the Number of Background InnoDB IO Threads 配置 后台InnoDB IO Threads的数量 InnoDB 使用bac ...
- 总结showModalDialog在开发中的一些问题
一.在页面调用window.open()函数后,可以直接在打开的页面中用window.opener来调用父页面的方法,然而如果用showModalDialog打开一个模态窗口,就不能通过window. ...
- asp.net(C#)之NPOI"操作Excel
1.首先到网上下载"NPOI.DLL".引用. 2.新建一个操作类"ExcelHelper.cs": using System.Collections.Gene ...
- 从Hadoop骨架MapReduce在海量数据处理模式(包括淘宝技术架构)
从hadoop框架与MapReduce模式中谈海量数据处理 前言 几周前,当我最初听到,以致后来初次接触Hadoop与MapReduce这两个东西,我便稍显兴奋,认为它们非常是神奇.而神奇的东西常能勾 ...
- 在Windows系统上实现轻量级的线程间及进程间消息队列
Windows没有message queue累世的IPC内核对象,使得在在处理IPC时少了一种传递消息的手段. 利用Windows的Naming Object可以实现一套简单的Inter-Thread ...