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 ...
随机推荐
- LeetCode:至少是其他数字两倍的最大数【747】
LeetCode:至少是其他数字两倍的最大数[747] 题目描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素 ...
- javaweb学习纲要
Java Web 概述 大纲: 1.C/S体系结构 2.B/S体系机构 3.两种体系结构比较 4.主流的Web程序应用平台 5.java web学习路线图 1.C/S体系结构 C/S是Client/ ...
- Python框架之Tornado(二)预备知识epoll最好的讲解
问:epoll 或者 kqueue 的原理是什么?为什么 epoll 和 kqueue 可以用基于事件的方式,单线程的实现并发?我没看过 linux 内核,对这方面一直有疑问…… 必须从很多基础的概念 ...
- FarBox的使用经历
新年伊始,一个崭新的开始,我的博客也有个新的起点.怎么会有这个想法呢?个人觉得这是程序员那颗不安分的心开始躁动了(其实就是开始作了~~哈哈,开个玩笑). 更佳界面.更流畅的操作.更方便的查看.更炫酷动 ...
- PHPExcel读写封装
<? require_once ('inc/PHPExcel/PHPExcel/IOFactory.php'); /** * @author lgl * 使用实例 * $fieldMap=['昵 ...
- H3C 交换机设置telnet WEB用户
huwei : local-user admin password cipher @#$@#$ service-type telnet ssh service-type telnet ssh leve ...
- 安装配置Apollo-Prota web中心平台
首先要求2g以上内存哈,JDK1.8 搭建数据库 创建表 apollo-build-scripts-master 整个阿波罗环境包 使用一个命令启动整个阿波罗服务环境 创建两个数据库 分别为:apol ...
- repo 小结
repo只是google用Python脚本写的调用git的一个脚本,主要是用来下载.管理Android项目的软件仓库. 1. 下载 repo 的地址: http://android.git.kerne ...
- vue中编辑代码是不注意格式时会报错
1.是因为我们使用了eslint的代码规范,我们不要使用这种规范就好 2.在build目录下找到webpack.base.conf.js 在里面找到关于eslint的相关配置注释或移除掉就好
- 判断一个浏览器是否支持opacity
支持opacity的浏览器,总会将opacity值规范成小于1.0且以0开头的值.例如,如果将opacity指定为:.5,原始支持opacity的浏览器就会将该值规范为0.5,而不支持opacity的 ...