http://acm.hdu.edu.cn/showproblem.php?pid=1983

首先,题目要求出口和入口不能封闭,那么,只要把出口或入口的周围全给封闭了那盗贼肯定无法成功偷盗,出口或入口周围最多

会有四个点,所以初始答案ans=4(没必要计算出出口或入口可以封闭的最小值,就初始为4就好),然后从一到四枚举看有没有

最小的符合条件的值,

所以先用BFS查找出至少偷到一个宝石的最小时间的路径,记录下来,然后枚举封闭路径上的点递归回去,再BFS判断是否能偷盗

成功---DFS中插入BFS

code

 #include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct point{
int x,y;
int time,flag;//记录时间和是否至少偷到一个宝石
int rx[],ry[];//记录路径
};
char map[][];
int visit[][][];//开三维的数组时因为有偷到了和还没有偷到两种情况,作用相当于是当偷到第一个宝石后,再议这个宝石的地方为起点寻找到终点的时间最短路径,确保了是最短的
int n,m,sx,sy,tt,ans;
int dx[]={,-,,};
int dy[]={,,,-};
void DFS(int deep)
{
int i,k;
if (deep>ans) return ;
memset(visit,,sizeof(visit));
queue<point>Q;
point now,next;
now.x=sx,now.y=sy;
now.time=now.flag=;
visit[now.x][now.y][now.flag]=;
Q.push(now);
int minn=-;
while (!Q.empty())
{
now=Q.front();
Q.pop();
if (map[now.x][now.y]=='E'&&now.flag==)
{
minn=now.time;
break;
}
for (i=;i<;i++)
{
next.x=now.x+dx[i];
next.y=now.y+dy[i];
if (next.x<||next.x>n||next.y<||next.y>m)continue;
if (map[next.x][next.y]=='#')continue;
if (now.time>=tt)continue;
if (map[next.x][next.y]=='J') next.flag=;
else next.flag=now.flag;
if (visit[next.x][next.y][next.flag]==)continue;
for (k=;k<=now.time;k++) //记录路径
{
next.rx[k]=now.rx[k];
next.ry[k]=now.ry[k];
}
next.time=now.time+;
visit[next.x][next.y][next.flag]=;
next.rx[next.time]=next.x;
next.ry[next.time]=next.y;
Q.push(next);
}
}
if (minn==-)
{
if (deep<ans)
ans=deep;
return ;
}
for (i=;i<=now.time;i++)
{
char op=map[now.rx[i]][now.ry[i]];
if (op=='E'||op=='S') continue;
map[now.rx[i]][now.ry[i]]='#';
DFS(deep+);
map[now.rx[i]][now.ry[i]]=op;//回溯
}
}
int main()
{
int t,i,j;
while (~scanf("%d",&t))
{
while (t--)
{
getchar();
scanf("%d %d %d",&n,&m,&tt);
for (i=;i<=n;i++)
{
for (j=;j<=m;j++)
{
scanf(" %c",&map[i][j]);
if (map[i][j]=='S')
sx=i,sy=j;
}
}
ans=;
DFS();
printf("%d\n",ans);
}
}
return ;
}

hdu 1983(BFS+DFS) 怪盗Kid的更多相关文章

  1. HDU 1983 BFS&amp;&amp;DFS

    大多数刚需封锁4区域可以,DFS地区封锁.BFS无论是通过 #include "stdio.h" #include "string.h" #include &q ...

  2. hdu 1175(BFS&DFS) 连连看

    题目在这里:http://acm.hdu.edu.cn/showproblem.php?pid=1175 大家都很熟悉的连连看,原理基本就是这个,典型的搜索.这里用的是广搜.深搜的在下面 与普通的搜索 ...

  3. hdu 1044(bfs+dfs+剪枝)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)

    思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...

  5. 邻结矩阵的建立和 BFS,DFS;;

    邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...

  6. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  7. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  8. Collect More Jewels(hdu1044)(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  9. Cleaning Robot (bfs+dfs)

    Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...

随机推荐

  1. 通过日志恢复SQL Server的历史数据

    通过日志恢复SQL Server的历史数据 Posted on 2008-11-14 21:47 代码乱了 阅读(4658) 评论(10)  编辑 收藏 园子里前段时间发过一篇通过日志恢复MSSQL数 ...

  2. C# 根据Excel生成树

    需求: 根据Excel生成树,Excel的某些节点为属性节点, 如: 列(桩号.构件编码.测试属性1) 是列(分项工程名称) 的属性,非节点. 列(桩号.构件编码.测试属性1) 以属性的方式存在 导入 ...

  3. Python之路 - Socketserver实现多并发

    Python之路 - Socketserver实现多并发 阅读指引

  4. java 代码块,静态代码块,构造器等的执行顺序

    写了一段测试代码,如下: public class ExecutionSequence extends fatherClass{    static{        System.out.printl ...

  5. spark pyspark 常用算法实现

    利用Spark-mllab进行聚类,分类,回归分析的代码实现(python) http://www.cnblogs.com/adienhsuan/p/5654481.html 稀疏向量: 关于Spar ...

  6. linux服务器中Jenkins集成git、Gradle持续构建Springboot项目

    Jenkins是用java编写的开源持续集成工具,目前被国内外各公司广泛使用.本章教大家如何在linux服务器中使用Jenkins自动发布一个可作为linux服务发布的Springboot项目. 自动 ...

  7. vue样式绑定

    vue 绑定class 和style 有相同的地方,可以是数组和对象,对于class class是真实的在css样式中添加的,只不过在元素中添加需要:class这样代表绑定,然后这个class作为对象 ...

  8. Redis安装完后redis-cli无法使用(redis-cli: command not found)已使用

    wget http://download.redis.io/redis-stable.tar.gz(下载redis-cli的压缩包) tar xvzf redis-stable.tar.gz(解压) ...

  9. 11.15java课后作业

    1,编写一个程序,指定一个文件夹,能自动计算出其总容量 package Account; import java.io.File; import java.util.ArrayList; public ...

  10. HLSL

    [HLSL] 1.Direct3D 9 shaders can be designed using shader model 1, shader model 2 and shader model 3; ...