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 ...
随机推荐
- redis3.2.11单机多实例集群部署并测试连接情况
配置准备: redis3.2.11安装配置规划 机器 192.168.169.135(本机虚拟机) 系统 Red Hat Enterprise Linux Server release 6.4 (Sa ...
- hadoop学习第二天-了解HDFS的基本概念&&分布式集群的搭建&&HDFS基本命令的使用
一.HDFS的相关基本概念 1.数据块 1.在HDFS中,文件诶切分成固定大小的数据块,默认大小为64MB(hadoop2.x以后是128M),也可以自己配置. 2.为何数据块如此大,因为数据传输时间 ...
- Python进阶(5)_进程与线程之协程、I/O模型
三.协程 3.1协程概念 协程:又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存 ...
- Linux基础系列:常用命令(1)
1.开启Linux操作系统,要求以root用户登录GNOME图形界面,语言支持选择为汉语 2.使用快捷键切换到虚拟终端2,使用普通用户身份登录,查看系统提示符 命令:ctrl+alt+F2 3.使用命 ...
- JavaWeb:实现文件上传与下载
JavaWeb:实现文件上传与下载 文件上传前端处理 本模块使用到的前端Ajax库为Axio,其地址为GitHub官网. 关于文件上传 上传文件就是把客户端的文件发送给服务器端. 在常见情况(不包含文 ...
- python的PIL模块安装
一.Centos安装PIL #尤其重要,否则会报错 yum install python-devel yum install libjpeg libjpeg-devel zlib zlib-devel ...
- vscode使用vue中的v-for提示错误
"vetur.validation.template": false 在设置里面把vetur.validation.template改为false 文件→首选项→设置 搜索vetu ...
- javascript重置(base层)(。。。。不完整)
1.nextSibling浏览器兼容问题 <ul> <li id="item1"></li> <li id="item2&quo ...
- 【转载】OpenWrt sysupgrade 命令行更新固件到最新版
OpenWrt sysupgrade 命令行更新固件到最新版 下面我们要使用 sysupgrade 更新固件到trunk最新版. 要注意的是,trunk包含试验的功能,可能不稳定,刷机风险自己承担. ...
- 主攻ASP.NET MVC4.0之重生:Jquery Mobile 列表
代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title ...