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. Asp.Net WebApi 学习记录(一)

    刚创建的 Asp.Net Web Api 项目,在进行简单的测试时发现返回的 JSON 数据很丑陋.与平时我们使用的 JSON.NET 序列化出来的字符串不一样.通过下面的设置就可以了: // 清除所 ...

  2. curl get请求

    curl --proxy 192.168.1.159:8889  -d "grant_type=client_credential&appid=wxxx&secret=xxx ...

  3. response.sendfile() fails with Error: Forbidden

    [response.sendfile() fails with Error: Forbidden] 参考:https://github.com/expressjs/express/issues/146 ...

  4. EasyUI值的清空与获取

    清空: 一般值 $("#searchx").val(""); 时间选择框 $('#starttime').datetimebox('setValue', '') ...

  5. 解题2(IpIsSameSubNet)

    题目描述 子网掩码是用来判断任意两台计算机的IP地址是否属于同一子网络的根据.子网掩码与IP地址结构相同,是32位二进制数,其中网络号部分全为“1”和主机号部分全为“0”.利用子网掩码可以判断两台主机 ...

  6. ELK 日志学习

    一.Elasticsearch 安装(所有的版本均使用5.5.0 ,且版需要相同 logstash \ kibana \ filebeat ) 官网下载地址:https://www.elastic.c ...

  7. java面试题:多线程与并发

    多线程 关键词:线程池 Q:如何新建一个线程? 继承Thread,或者实现Runnable接口,或者通过Callable接口实现. Q:Callable怎么用? Callable可以作为FutureT ...

  8. 二 random模块

    1 import random 2 3 print(random.random())#(0,1)----float 大于0且小于1之间的小数 4 5 print(random.randint(1,3) ...

  9. NumPy 排序、条件刷选函数

    NumPy 排序.条件刷选函数 NumPy 提供了多种排序的方法. 这些排序函数实现不同的排序算法,每个排序算法的特征在于执行速度,最坏情况性能,所需的工作空间和算法的稳定性. 下表显示了三种排序算法 ...

  10. 203. Remove Linked List Elements (List)

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...