Tempter of the Bone HDU - 1010(dfs)
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 148156 Accepted Submission(s): 39500
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
YES 题意:一个迷宫,要刚好在t秒的时候走到门口才可以,每一次走过一个格子就不能再走这个格子了,问可不可以走出来
思路:dfs搜索,如果时间t比所有格子的数量都多那肯定走不出来了,画一画就知道如果绕一绕的话肯定是增加偶数步的,那就可以进行剪枝了,如果不可以的话为了继续找寻路,记得把走过的那个点标记成未走过哦
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
typedef long long ll;
const int maxn = 1e2+;
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
}
int N,M,T,startx,starty,endx,endy;
char maze[maxn][maxn];
int dx[]={,,-,};
int dy[]={,,,-};
int step;
int ok;
void dfs(int x,int y,int step)
{
int temp;
if(x==endx && y==endy && step==T)
{
ok=;
return ;
}
if(x<= || x>N || y<= || y>M)
return ;
temp=abs(T-step)-(abs(x-endx)+abs(y-endy));
if(temp< || temp&)
return ;
for(int i=;i<;i++)
{
int nx=x+dx[i],ny=y+dy[i];
if(maze[nx][ny]!='X')
{
maze[nx][ny]='X';
dfs(nx,ny,step+);
if(ok)
return;
maze[nx][ny]='.';
}
}
return ;
}
int main()
{
while(scanf("%d%d%d",&N,&M,&T) && (N&&M&&T))
{
int num=;
for(int i=;i<=N;i++)
for(int j=;j<=M;j++)
{
cin>>maze[i][j];
if(maze[i][j]=='S')
{
startx=i;
starty=j;
}
else if(maze[i][j]=='D')
{
endx=i;
endy=j;
}
else if(maze[i][j]=='X')
num++;
}
if(N*M-num<=T)
{
cout<<"NO"<<endl;
continue;
}
ok=;
maze[startx][starty]='X';
dfs(startx,starty,);
if(ok)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl; }
}
Tempter of the Bone HDU - 1010(dfs)的更多相关文章
- HDU 1010 temp of the bone 解题报告 (DFS)
转载大佬的blog,很详细,学到了很多东西 奇偶剪枝:根据题目,dog必须在第t秒到达门口.也就是需要走t-1步.设dog开始的位置为(sx,sy),目标位置为(ex,ey).如果abs(ex-x)+ ...
- HDU 1010 Tempter of the Bone 骨头诱惑(DFS+剪枝)
题意: 必须在第t秒走到格子D上,S为起点,D为终点,点就是可以走,X就是墙. 思路: 将迷宫外围四面都筑墙‘X’.深度搜索+奇偶剪枝,再加一个剪枝“无法在指定时间内到达”. #include < ...
- Tempter of the Bone HDU 1010(DFS+剪枝)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- Tempter of the Bone HDU - 1010
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...
- F - JDG HDU - 2112 (最短路)&& E - IGNB HDU - 1242 (dfs)
经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬 ...
- Number Cutting Game HDU - 2848(DFS)
两个对于一个数切割 k 次,然后切割以后把这些值加起来,然后继续切割 k 次,问谁先没有办法切割. 对于第一个人,先枚举每种切割的情况,然后拿去给第二个人切割,如果第二个人怎么样都没办法切割出来,那么 ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 2021.08.16 P1300 城市街道交通费系统(dfs)
2021.08.16 P1300 城市街道交通费系统(dfs) P1300 城市街道交通费系统 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 城市街道交费系统最近创立了.一 ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
随机推荐
- 2009年3月新浪PHP面试题及答案(一)
1. echo count(“abc”); 输出什么? 答案:1 2. 用PHP代码写出显示客户端IP与服务器IP的代码. 答案:客户端 getenv(‘REMOTE_ADDR’); 服务器端 get ...
- using System.Web.Script.Serialization
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...
- X64下IIS调用32位的dll
WebAPI项目中遇到了需要调用32位C++的dll的情况,调试的时候能正常调用,但是发布了之后部署在IIS中出现了BadFormatImage异常, 解决方法是在IIS中相应应用程序池=>高级 ...
- 构建第一个Spring Boot2.0应用之集成mybatis(六)
一.环境: IDE:IntelliJ IDEA 2017.1.1 JDK:1.8.0_161 Maven:3.3.9 springboot:2.0.2.RELEASE 二.步骤 方式一:利用配置文件配 ...
- Android stutdio2.2 启动模拟器出现“/dev/kvm is not found.”解决方法
第一次启动avd,Android stutdio会自动安装Intel HAXM,而且表面看是成功的,再次启动会出现“/dev/kvm is not found.”,这说明Intel HAXM没有安装成 ...
- extends 继承
继承的作用:子类可以直接拥有父类成员:其中,私有成员和构造函数不参与继承: java中类继承的特点:只支持单一继承和多重继承,不支持多继承(一个类不能同时继承多个类) 继承中成员变量的特点:子类中可以 ...
- IIS重叠回收
在IIS应用程序池的高级设置中,有一个“禁用重叠回收”属性,默认值是False. 重叠回收(Overlapped Recycling),指的是当回收的时候,原来的进程继续处理正在处理的请求,同时一个新 ...
- 获得session中的用户信息
由于每个系统都有往session中放入用户信息以及把用户信息取出来的模块,而且在session中取出用户信息的地方非常之多,所以有必要把session中对用户的操作封装成为一个工具类,以便在以后的使用 ...
- 新版graylog2安装过程
Graylog是一个开源的 log 收容器,背后的储存是搭配 mongodb,而搜寻引擎则由 elasticsearch 提供.以前版本主要有两个部分集合而成 server 与 web interfa ...
- ADO.NET #3-1 (GridView + DataReader + SqlCommand)完全手写Code Behind
[C#] ADO.NET #3-1 (GridView + DataReader + SqlCommand)完全手写.后置程序代码 之前有分享过一个范例 [C#] ADO.NET #3 (GridVi ...