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. nginx图片缓存服务器配置实战

     1.图片目录设置: 假定服务器主目录为nginx的默认目录:/usr/local/nginx-0.8.32/html/ 图片存放目录为:/usr/local/nginx-0.8.32/html/SD ...

  2. NLog学习笔记一

    一.NLog是什么? NLog是一个基于.NET的免费的开源的日志记录类库.(官网:http://nlog-project.org/) NLog特点如下: 配置简单方便.可以将配置信息写的应用程序的配 ...

  3. ElasticSearch多个字段分词查询高亮显示

    ElasticSearch关键字查询,将关键字分词后查询,多个字段,查询出来字段高亮显示. 查询方法如下: public List<NewsInfo> searcher2(String k ...

  4. 数据库(JDBC、DBUtils)

     JDBC(Java DataBase Connection) 今日内容介绍 u SQL语句查询 u JDBC 第1章 JDBC 1.1 JDBC概述 JDBC(Java Data Base Conn ...

  5. redis---安全设置

    redis的安全性是通过设置口令来实现的. 首先打开redis的配置文件,我的是在/etc/redis/redis.conf,个人的路径可能会有不同,可以自行查找. 打开redis.conf文件以后, ...

  6. RxJava2 中多种取消订阅 dispose 的方法梳理( 源码分析 )

    Github 相关代码: Github地址 一直感觉 RxJava2 的取消订阅有点混乱, 这样也能取消, 那样也能取消, 没能系统起来的感觉就像掉进了盘丝洞, 迷乱… 下面说说这几种情况 几种取消的 ...

  7. linux下获取外网IP

    使用阿里云或者有多个网卡IP的机器需要取外网IP时,可以用下面这种 wget -qO - ifconfig.co 更多方法参考:https://yq.aliyun.com/ziliao/105999

  8. Internationalization(i18n) support in SAP CRM,UI5 and Hybris

    i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是"国际化"的简称.对程序来说,在不修改内部代码的情况下,能根据不同语言及 ...

  9. Android(java)学习笔记96:layout_weight使用注意事项

    1. android:layout_weight使用说明: layout_weight是权重的意思,也就是各个控件所占的比重,用在LinearLayout布局中.当我们使用layout_weight的 ...

  10. springboot框架快速搭建

    1.    新建Maven项目  spring-boot 2.    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0 ...