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

Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

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.

 
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'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.

 
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 
Sample Input

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)的更多相关文章

  1. HDU 1010 temp of the bone 解题报告 (DFS)

    转载大佬的blog,很详细,学到了很多东西 奇偶剪枝:根据题目,dog必须在第t秒到达门口.也就是需要走t-1步.设dog开始的位置为(sx,sy),目标位置为(ex,ey).如果abs(ex-x)+ ...

  2. HDU 1010 Tempter of the Bone 骨头诱惑(DFS+剪枝)

    题意: 必须在第t秒走到格子D上,S为起点,D为终点,点就是可以走,X就是墙. 思路: 将迷宫外围四面都筑墙‘X’.深度搜索+奇偶剪枝,再加一个剪枝“无法在指定时间内到达”. #include < ...

  3. Tempter of the Bone HDU 1010(DFS+剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  4. 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 ...

  5. F - JDG HDU - 2112 (最短路)&& E - IGNB HDU - 1242 (dfs)

    经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬 ...

  6. Number Cutting Game HDU - 2848(DFS)

    两个对于一个数切割 k 次,然后切割以后把这些值加起来,然后继续切割 k 次,问谁先没有办法切割. 对于第一个人,先枚举每种切割的情况,然后拿去给第二个人切割,如果第二个人怎么样都没办法切割出来,那么 ...

  7. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  8. 2021.08.16 P1300 城市街道交通费系统(dfs)

    2021.08.16 P1300 城市街道交通费系统(dfs) P1300 城市街道交通费系统 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 城市街道交费系统最近创立了.一 ...

  9. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

随机推荐

  1. Java操作Excel之POI简单例子

    /** * 利用POI操作Excel表单 * * 需要jar包: * HSSF针对03及以前版本,即.xls后缀 * |---poi-3.16.jar * XSSF针对07及以后版本,即xlsx后缀 ...

  2. varnish pipe 和pass的区别分析

    这两天在学习varnish,在学到vcl时,不理解pipe和pass的区别以及如何区分加以应用.通过两天的搜索,总算是理清了概念.现在记录在博客上跟大家分享. 当 vcl_recv 函数接收到请求时, ...

  3. 利用rand7()构造rand10()

    题意 已知有个rand7()的函数,返回1到7随机自然数,让利用这个rand7()构造rand10() 随机1~10 参考代码 int rand7() { srand((int)time(NULL)) ...

  4. SpringMVC配置文件 中 mvcview-controller 标签的使用

    一.<mvc:view-controller path=""/>标签的作用 工程WEB-INF目录下面的JSP页面,我们知道是不能直接使用URL访问到.需要通过控制器转 ...

  5. SpringMVC 返回实体对象时屏蔽某些属性

    SpringMVC 可以直接已JSON的结果返回实体对象,可是返回时是所有属性与属性值都会一并返回, 怎样才能屏蔽某些属性?方法很简单,只要在实体对象类中要屏蔽的属性值上加 @JsonIgnore 注 ...

  6. 配置百度云盘python客户端bypy上传备份文件

    要求:安装python2.7,安装git 1.git clone https://github.com/houtianze/bypy.git 2.cd bypy 3.sudo python setup ...

  7. 谈谈我对MVC的View层实现的理解

    MVC框架可以把应用清晰明了地分为三个部分:Model层–数据层,View层–视图层,Controller–逻辑层,Model层负责整合数据,View层负责页面渲染,Controller层负责实现业务 ...

  8. Django组件:forms组件(简易版)

    一.校验字段功能 1.模型:models.py class UserInfo(models.Model): name=models.CharField(max_length=32) pwd=model ...

  9. Callback, Promise和Async/Await的对比

    Callback, Promise和Async/Await的对比 Callback Hell getData1(function (data1) { console.log('我得到data1了') ...

  10. thinkphp简易搜索

    需求: 用户输入关键词,选项卡的每个选项输出一个分类的列表内容,分类有文章.ppt.学习. 实现过程: 视图中用户输入的关键词post给控制器,控制器对这个关键词做三次模糊查询处理,因为是三个分类对应 ...