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. tf.identity 个人理解

    tf.identity is useful when you want to explicitly transport tensor between devices (like, from GPU t ...

  2. 线程执行synchronized同步代码块时再次重入该锁过程中抛异常,是否会释放锁

    一个线程执行synchronized同步代码时,再次重入该锁过程中,如果抛出异常,会释放锁吗? 如果锁的计数器为1,抛出异常,会直接释放锁: 那如果锁的计数器为2,抛出异常,会直接释放锁吗? 来简单测 ...

  3. rem是如何实现自适应布局的

    原文链接:http://caibaojian.com/web-app-rem.html 摘要:rem是相对于根元素<html>,这样就意味着,我们只需要在根元素确定一个px字号,则可以来算 ...

  4. Linux系统下面crontab选择默认编译器

    crontab修改默认编辑器 crontab默认编辑器为nano. 修改crontab默认编辑器为vi或者其他的编辑器. 法一: export EDITOR="/usr/bin/vim&qu ...

  5. metasploitable使用

    DVWA默认的用户有5个,用户名密码如下(一个足以): admin/password gordonb/abc123 1337/charley pablo/letmein smithy/password

  6. django models返回数据根据某字段倒序排列

    例如有一个models表叫做report,report表中有一个endtime,想将结果按照endtime倒序排列   正序排列的方法:[models对象.objects.order_by(“字段名& ...

  7. GreenDao在列中的单词之间自动加_

    1.第一种情况,原字段(属性.列)是 驼峰式命名法 @Entitypublic class Employee { @Id(autoincrement = true) private Long id; ...

  8. spring jpa nativequery in与修改

    参考 https://blog.csdn.net/a3025056/article/details/79022816 @Modifying@Transactional /* 如果在事务中使用需加上此注 ...

  9. centos7.2 +cloudstack 4.11 +KVM +ceph 安装配置(网卡带聚合)

    系统安装,注意:管理节点版本是有要求的,配置为centos 7.2 最小安装版本(非最小化安装). 系统分区要求 /boot/efi 200MB / 100G /var 100G swap 0 其它给 ...

  10. CTF题-http://120.24.86.145:8002/flagphp/:Bugku----flag.php

    今天做了一道关于序列化的题目,收益颇多,愉快地开始. 首先,提示了“hint”,所以尝试加入hint参数.这儿没啥好说的,最后hint=1显示了重点内容.如下图所示 没错,是金灿灿的网页代码,开心,仔 ...