hdu4771 Stealing Harry Potter's Precious(DFS,BFS)
练习dfs和bfs的好题。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const int eps=1e-;
const int INF=;
const int maxnm=+;
int ans,n,m,k,d[][];
int dx[]= {,-,,};
int dy[]= {,,,-};
char maps[maxnm][maxnm];
int had[maxnm][maxnm];
struct Precious
{
int x,y;
bool used;
} p[];
int Bfs(int from,int to);
bool Dfs(int id,int now,int step);
void ini();
int main()
{
//freopen("in8.txt","r",stdin);
while(scanf("%d%d",&n,&m)==)
{
if(n==&&m==) break;
ini();
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
cin>>maps[i][j];
if(maps[i][j]=='@')
{
p[].x=i;
p[].y=j;
}
}
}
scanf("%d",&k);
for(int i=; i<=k; i++)
{
int xx,yy;
scanf("%d%d",&xx,&yy);
p[i].x=xx;
p[i].y=yy;
}
if(Dfs(,,))
{
printf("%d\n",ans);
}
else
{
printf("-1\n");
}
}
return ;
}
void ini()
{
memset(d,-,sizeof(d));
ans=INF;
for(int i=; i<; i++)
{
p[i].used=;
}
}
int Bfs(int from,int to)
{
if(d[from][to]!=-) return d[from][to];
if((p[from].x==p[to].x)&&(p[from].y==p[to].y))
{
return d[from][to]=;
}
else
{
queue<pii>q;
memset(had,,sizeof(had));
q.push(make_pair(p[from].x,p[from].y));
had[p[from].x][p[from].y]=;
while(!q.empty())
{
int tx=q.front().first;
int ty=q.front().second;
q.pop();
for(int i=; i<; i++)
{
int X=tx+dx[i];
int Y=ty+dy[i];
if(X>=&&X<=n&&Y>=&&Y<=m)
{
if(had[X][Y]||maps[X][Y]=='#')
{
continue;
}
else
{
q.push(make_pair(X,Y));
had[X][Y]=had[tx][ty]+;
if(X==p[to].x&&Y==p[to].y)
{
return d[from][to]=had[X][Y]-;
}
}
}
}
}
return d[from][to]=-;
}
} bool Dfs(int id,int now,int step)
//id表示现在在哪点,now表示已经拿了几个,step表示目前一共走了几步
{
bool w=;
p[id].used=;
if(now==k)
{
//cout<<"***"<<step<<endl;
ans=min(ans,step);
p[id].used=;
return true;
}
else
{
for(int i=; i<=k; i++)
{
if(p[i].used==)
{
int stt=Bfs(id,i);
if(stt==-)
{
p[id].used=;
return false;
}
else
{
if(Dfs(i,now+,step+stt))
{
w=;
}
}
}
}
if(w==)
{
p[id].used=;
return true;
}
else
{
p[id].used=;
return false;
}
}
}
hdu4771 Stealing Harry Potter's Precious(DFS,BFS)的更多相关文章
- HDU 4771 Stealing Harry Potter's Precious dfs+bfs
Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...
- 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 ...
- 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压
2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...
- hdu4771 Stealing Harry Potter's Precious
注意--你可能会爆内存-- 假设一个直接爆搜索词-- 队列存储器元件被减少到-- #include<iostream> #include<map> #include<st ...
- 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
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- Stealing Harry Potter's Precious BFS+DFS
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
- hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@' 表示的是起点,'#' 表示的是障碍物不能通过,'.' 表示的是路能通过的: ...
- HDU Stealing Harry Potter's Precious(状压BFS)
状压BFS 注意在用二维字符数组时,要把空格.换行处理好. #include<stdio.h> #include<algorithm> #include<string.h ...
随机推荐
- 使用ajax进行汽车详情表的查询
主界面代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- 02 Spring框架 简单配置和三种bean的创建方式
整理了一下之前学习Spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节学习了如何搭建SpringIOC的环境,下一步我们就来讨论一下如何利用ioc来管理对象和维护对象关系. <? ...
- PAT 天梯赛 L2-021. 点赞狂魔 【水】
题目链接 https://www.patest.cn/contests/gplt/L2-021 题意 给出一个若干个人名,后面给出点赞的总数,以及每个赞的标签类型,输出前三个点赞狂魔,按标签类型不同数 ...
- Cocos2d-x项目移植到WP8系列之六:C#工程使用C++的DLL
原文链接: http://www.cnblogs.com/zouzf/p/3984510.html 此时,一些大问题都被解决后,整个工程基本能跑起来了,最后一个大问题是:业务层是用Lua开发的,底层的 ...
- 大话设计模式之PHP篇 - 策略模式
什么是策略模式? 定义:策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化. 组成:抽象策略角色: 策略类,通常由一个接口或者抽象 ...
- Qt开发UDP
一.单播 1.声明udp对象 QUdpSocket* udpClient: 2.new出对象 udpClient = new QUdpSocket(this); 3.分配本地地址(如果不分配,使用系统 ...
- [Python] 弗洛伊德(Floyd)算法求图的直径并记录路径
相关概念 对于一个图G=(V, E),求图中两点u, v间最短路径长度,称为图的最短路径问题.最短路径中最长的称为图的直径. 其中,求图中确定的某两点的最短路径算法,称为单源最短路径算法.求图中任意两 ...
- 基于matlab的边缘提取方法的比较
1.Matlab简述 Matlab是国际上最流行的科学与工程计算的软件工具,它起源于矩阵运算,已经发展成一种高度集成的计算机语言.有人称它为“第四代”计算机语言,它提供了强大的科学运算.灵活的程序设计 ...
- No input clusters found in output/ZS_TEST_OUTPUT3404268420/clusters-0/part-randomSeed. Check your -c argument.
错误原因可能为: -i 后面参数路径对应的目录或文件中的数据为空,即输入集为空,所以找不到cluster
- C++中随机数的生成
1.随机数由生成器和分布器结合产生 生成器generator:能够产生离散的等可能分布数值 分布器distributions: 能够把generator产生的均匀分布值映射到其他常见分布,如均匀分布u ...