练习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)的更多相关文章

  1. HDU 4771 Stealing Harry Potter's Precious dfs+bfs

    Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...

  2. 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 ...

  3. 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压

    2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...

  4. hdu4771 Stealing Harry Potter&#39;s Precious

    注意--你可能会爆内存-- 假设一个直接爆搜索词-- 队列存储器元件被减少到-- #include<iostream> #include<map> #include<st ...

  5. 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 ...

  6. HDU 4771 Stealing Harry Potter's Precious

    Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  7. Stealing Harry Potter's Precious BFS+DFS

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  8. hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的: ...

  9. HDU Stealing Harry Potter's Precious(状压BFS)

    状压BFS 注意在用二维字符数组时,要把空格.换行处理好. #include<stdio.h> #include<algorithm> #include<string.h ...

随机推荐

  1. 1.6 使用电脑测试MC20的读取带中文短信功能

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...

  2. Http1.0和Http1.1的主要区别

    1.HTTP 1.1支持长连接(PersistentConnection)和请求的流水线(Pipelining)处理 HTTP 1.0规定浏览器与服务器只保持短暂的连接,浏览器的每次请求都需要与服务器 ...

  3. python基础28 -----python中sockserver模块

    一.Python中的sockserver模块 1.该模块与sock模块不同之处是该模块自动帮我们分装好了一些功能,让我们在编程的时候直接调用这些功能就可以了,节省了编程步骤. 2.如图所示 注释:上图 ...

  4. requirejs源码分析: define 方法

    define = function (name, deps, callback) {     var node, context; //Allow for anonymous modules     ...

  5. ZOJ 3958 Cooking Competition 【水】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3958 AC代码 #include <cstdio> ...

  6. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  7. 每天一个Linux命令(56)yum命令

          用于添加/删除/更新RPM包,自动解决包的依赖问题以及系统更新升级.     (1)用法:     用法:  yum  [参数] [软件名]     (2)功能:     功能:  yum ...

  8. Socket 例子

    package com.pab.util; import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import ...

  9. Wi-Fi基带芯片和Wi-Fi无线网卡设计方案

    转:http://wenku.baidu.com/link?url=Q0ImC 0IIG7qrbB8DpGrrU3aOYvxNYCyHsxukspH8XMCDYMjYMPSJq_TCISC5amsNY ...

  10. jQuery仿Android锁屏图案应用

    在线演示 本地下载