hdu 1983(BFS+DFS) 怪盗Kid
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的更多相关文章
- HDU 1983 BFS&&DFS
大多数刚需封锁4区域可以,DFS地区封锁.BFS无论是通过 #include "stdio.h" #include "string.h" #include &q ...
- hdu 1175(BFS&DFS) 连连看
题目在这里:http://acm.hdu.edu.cn/showproblem.php?pid=1175 大家都很熟悉的连连看,原理基本就是这个,典型的搜索.这里用的是广搜.深搜的在下面 与普通的搜索 ...
- hdu 1044(bfs+dfs+剪枝)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...
- 邻结矩阵的建立和 BFS,DFS;;
邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- Collect More Jewels(hdu1044)(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
随机推荐
- FMS Dev Guide学习笔记(权限控制)
一.开发交互式的媒体应用程序 1.关于访问(权限)控制 当一个用户访问服务器的时候,默认情况下,他可以访问所有的流媒体文件和共享对象.但是你可以使用服务端ActionScript为流媒体文件和 ...
- Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans.xsd'
明明项目没错误,但application.xml就报了错误,这是什么问题呢? 问题在于我们找不到org/springframework/beans/spring-beans这个包,也就是我们的spri ...
- jquery 获取子元素的限制jquery
今天练习jqueryAPI发现一个问题就是子元素如果采用nth-child,元素不同就获取不到,因此一个父元素下的子元素标签必须相同,如果不同第一个元素可以用这个方法实现,但是如果第二元素及以后如果出 ...
- jstl-随机数-借用jsp嵌入的代码
) %></c:set> ${rand }
- 外购半成品报SHORT问题(非验货客户)
外购半成品报SHORT问题(验货客户)https://www.cnblogs.com/Snowfun/p/8660646.html 下面看非验货客户: 1.检查采购类型是否为F(SAP_MARC),为 ...
- jms版本
Java消息服务是一个在 Java标准化组织(JCP)内开发的标准(代号JSR 914). 2001年6月25日,Java消息服务发布JMS 1.0.2b,2002年3月18日Java消息服务发布 1 ...
- linux重新安装python
第一步:下载python2.7 wget https://www.Python.org/ftp/python/2.7.12/Python-2.7.12.tar.xz 第二步: 解压刚刚下载的压缩包 ...
- 一、Blender/Python 快速入门
原文:https://docs.blender.org/api/blender_python_api_current/info_quickstart.html#native-types 1 前言 可以 ...
- PHP 获取 IE浏览器版本号
function getIEBrowserVersion(){ $agent = strtolower($_SERVER['HTTP_USER_AGENT']); if(strpos($agent, ...
- GitHub优秀项目
https://blog.csdn.net/javaxuexi123/article/details/79248124