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 ...
随机推荐
- centos中搭建nginx环境
原文地址 安装PCRE 源码:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ cd /usr/local/src wget ftp://f ...
- AWK Demo
AWK is so poweful. AWK功能太强大了: @1:处理格式化文本(感觉主要还是以这个为主) @2:对文本逐行进行处理(过滤某些行,保留符合条件的) @3:将文件根据不同的条件分成多个文 ...
- 【整理学习Hadoop】H D F S 一个分布式文件系统
Hadoop分布式文件系统(HDFS)被设计成适合运行在通用硬件(commodity hardware)上的分布式文件系统.它和现有的分布式文件系统有很多共同点.但同时,它和其他的分布式文件系统的区别 ...
- Android:日常学习笔记(7)———探究UI开发(1)
Android:日常学习笔记(7)———探究UI开发(1) 常用控件的使用方法 TextView 说明:TextView是安卓中最为简单的一个控件,常用来在界面上显示一段文本信息. 代码: <T ...
- 十位用户唯一ID生成策略
新浪微博和twitter 等系统都有一窜数字ID来标示一个唯一的用户,这篇文章就是记录如何实现这种唯一数字ID 原理:使用MYSQL 自增ID 拼接任意字符..然后使用进制转换打乱规则 一般来说实现唯 ...
- json教程系列(1)-使用json所要用到的jar包下载
json是个非常重要的数据结构,在web开发中应用十分广泛.我觉得每个人都应该好好的去研究一下json的底层实现,基于这样的认识,金丝燕网推出了一个关于json的系列教程,分析一下json的相关内容, ...
- $ 用python处理Excel文档(1)——用xlrd模块读取xls/xlsx文档
本文主要介绍xlrd模块读取Excel文档的基本用法,并以一个GDP数据的文档为例来进行操作. 1. 准备工作: 1. 安装xlrd:pip install xlrd 2. 准备数据集:从网上找到的1 ...
- USB引脚及定义
USB 2.0 USB接口定义: USB引脚定义: 针脚 名称 说明 接线颜色 1 VCC +5V电压 红色 2 D- 数据线负极 白色 3 D+ 数据线正极 绿色 4 GND 接地 黑色 Min ...
- Go Concurrency or Parallel
关于并发和并行,先看两个示例 示例1: package main import "fmt" var quit = make(chan int) func foo6(){ for i ...
- java写出图形界面
1. 做出简单的窗体 package javaGUI; import java.awt.BorderLayout; import java.awt.Color; import javax.swing. ...