hdu 1010 Tempter of the Bone(dfs)
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std; char p[][];
int a[][] = {-,,,,,,,-}; // 方向数组
int flag; // 标记是否能够在第t秒时到达出口
int n,m,t;
int sx,sy,dx,dy; // 起始地点 和 出口的地点 void dfs(int x,int y,int k)
{
int i;
if (x< || y< || x>n || y>m) // 判断是否超出边界
return ;
if (k == &&x == dx && y==dy) // 如果刚好在第t秒时到达出口 flag = 1
{
flag = ;
return ;
}
int tmp = k - abs(x-dx)-abs(y-dy);
if (tmp < || tmp&) // 判断 1、是否剩余的最小步数 > 剩余的时间 2、剩余时间和剩余的最小步数的奇偶性是否相同
return ; for (i = ; i < ; i ++)
{
if (p[x+a[i][]][y+a[i][]] != 'X') // 如果时墙则不能通过
{
p[x+a[i][]][y+a[i][]] = 'X'; // 走过之后路就坍塌了(之后不能在通过)
dfs(x+a[i][],y+a[i][],k-);
if (flag) return ; // 如果满足条件直接退出
p[x+a[i][]][y+a[i][]] = '.'; // 当前搜的这条路可能不满足条件 下次再搜别的路时要恢复原样
}
}
return ;
}
int main ()
{
int i,j;
while (scanf("%d%d%d",&n,&m,&t)!=EOF)
{
getchar();
int sum = ;
if (n==&&m==&&t==)
break;
for (i = ; i <= n; i ++)
{
for (j = ; j <= m; j ++)
{
scanf("%c",&p[i][j]);
if (p[i][j] == 'S'){sx = i; sy = j;}
else if (p[i][j] == 'D'){dx = i; dy = j;}
else if (p[i][j] == 'X') sum ++;
}
getchar();
} if (n*m-sum <= t) // 如果可以走的路的步数小于t 直接pass
{
printf("NO\n");
continue;
}
p[sx][sy] = 'X'; // 出发之后不能再回到起点
flag = ;
dfs(sx,sy,t);
if (flag)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
hdu 1010 Tempter of the Bone(dfs)的更多相关文章
- hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1010 Tempter of the Bone (DFS+剪枝)
题意:从S走到D,能不能恰好用T时间. 析:这个题时间是恰好,并不是少于T,所以用DFS来做,然后要剪枝,不然会TEL,我们这样剪枝,假设我们在(x,y),终点是(ex,ey), 那么从(x, y)到 ...
- hdu 1010 Tempter of the Bone(dfs暴力)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1010 Tempter of the Bone(深搜+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- ZOJ 2110 Tempter of the Bone(DFS)
点我看题目 题意 : 一个N×M的迷宫,D是门的位置,门会在第T秒开启,而开启时间小于1秒,问能否在T秒的时候到达门的位置,如果能输出YES,否则NO. 思路 :DFS一下就可以,不过要注意下一终止条 ...
- HDU 1010 Tempter of the Bone(深度+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 题意:就是给出了一个迷宫,小狗必须经过指定的步数到达出口,并且每个格子只能走一次. 首先先来介绍一下奇偶性 ...
- HDU 1010 Tempter of the Bone (广搜+减枝)
题目链接 Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. How ...
- hdu 1010 Tempter of the Bone (奇偶性剪枝)
题意:有一副二维地图'S'为起点,'D'为终点,'.'是可以行走的,'X'是不能行走的.问能否只走T步从S走到D? 题解:最容易想到的就是DFS暴力搜索,,但是会超时...=_=... 所以,,要有其 ...
随机推荐
- 故障排查-linux命令测试端口连通性
方法一:telnet法 预置条件:安装telnet step 1.rpm -qa telnet-server(无输出表示telnet-server未安装,则执行step2:否则执行step3) ste ...
- 基础篇:6.1)形位公差-要素 Feature
本章目的:理解形位公差研究的对象-要素,即点.线.面. 1.定义 2.类型 2.1 按存在的状态分 2.2 按结构特征分 2.3 按所处的地位分 2.4 按结构性能分 2.5 按与尺寸关系分
- linux 系统管理(2) 文件或目录数量统计
统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件 ...
- 主库binlog被purge的情形
1.演示实验环境 --主库版本: root@(none)>show variables like 'version'; +---------------+-------------------- ...
- Python中使用PyMySQL
1.项目中使用PyMySQL一些案例 建立一个config.py 用于存储配置文件 2.测试 ##获取数据 from config import ctf '''connection对象支持的方法 cu ...
- Post、Get请求
post.get请求方法 /// <summary> /// Post.Get请求 /// </summary> /// <param name="url&qu ...
- Oracle PL/SQL编程之函数
注: 以下测试案例所用的表均来自与scott方案,使用前,请确保该用户解锁. 代码的执行环境是在sqlplus中 1.简介 函数用于返回特定的数据,当建立函数时,函数头部必须包含return子句,而在 ...
- Google推荐的图片加载库Glide
英文原文 Introduction to Glide, Image Loader Library for Android, recommended by Google 首发地址 http://jco ...
- OC SEL (@selector) 原理及使用总结(转)
SEL 类成员方法的指针 可以理解 @selector()就是取类方法的编号,他的行为基本可以等同C语言的中函数指针,只不过C语言中,可以把函数名直接赋给一个函数指针,而Object-C的类不能直接应 ...
- RabbitMQ入门-理论
目录 RabbitMQ简介 RabbitMQ原理简介 RabbitMQ安装 .NET Core 使用 RabbitMQ Hello World 工作队列 扇型交换机 直连交换机 主题交换机 远程过程调 ...