M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
'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
Sample Input
Sample Output
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std; char map[][];
bool vis[][];
int s_x,s_y,e_x,e_y;
int a,b,t,ok; bool check(int x,int y)
{
if (x<=||x>a||y<=||y>b)
return ;
if (map[x][y]=='X'||vis[x][y]||ok)
return ;
return ;
} void dfs(int x,int y,int time)
{
vis[x][y]=;
//printf("(%d,%d)\n",x,y);
if (x==e_x&&y==e_y&&time==t)//满足条件到终点
{
ok=;
return;
} int temp=t-time-(abs(x-e_x)+abs(y-e_y)); if (temp<)//剩余步数小于 0
return; int n_x,n_y; n_x=x,n_y=y+;
if (check(n_x,n_y))
{
dfs(n_x,n_y,time+);
vis[n_x][n_y]=;
} n_x=x+,n_y=y;
if (check(n_x,n_y))
{
dfs(n_x,n_y,time+);
vis[n_x][n_y]=;
} n_x=x,n_y=y-;
if (check(n_x,n_y))
{
dfs(n_x,n_y,time+);
vis[n_x][n_y]=;
} n_x=x-,n_y=y;
if (check(n_x,n_y))
{
dfs(n_x,n_y,time+);
vis[n_x][n_y]=;
}
} int main()
{
int i,j;
while (scanf("%d%d%d",&a,&b,&t)&&a+b+t)
{
getchar();
int wall=;
for (i=;i<=a;i++)
{
for (j=;j<=b;j++)
{
scanf("%c",&map[i][j]);
if (map[i][j]=='S')
{
s_x=i;
s_y=j;
}
if (map[i][j]=='D')
{
e_x=i;
e_y=j;
}
if (map[i][j]=='X')
wall++;
}
getchar();
} if (a*b-wall<=t)//所有路都走了还到不了终点,注意是 <=t ,可以少300ms
{
printf("NO\n");
continue;
} ok=;
memset(vis,,sizeof(vis));
int temp=t-(abs(s_x-e_x)+abs(s_y-e_y)); if (temp%==)//奇偶性相同才bfs
dfs(s_x,s_y,); if (ok)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
M - 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 ...
- 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+奇偶剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...
- hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...
- hdu Tempter of the Bone (奇偶剪枝)
学习链接:http://www.ihypo.net/1554.html https://www.slyar.com/blog/depth-first-search-even-odd-pruning.h ...
- hdu1010Tempter of the Bone(dfs+奇偶剪枝)
题目链接:pid=1010">点击打开链接 题目描写叙述:给定一个迷宫,给一个起点和一个终点.问是否能恰好经过T步到达终点?每一个格子不能反复走 解题思路:dfs+剪枝 剪枝1:奇偶剪 ...
- hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- hdoj--1010<dfs+奇偶剪枝>
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...
随机推荐
- Qt学习 之 多线程程序设计
QT通过三种形式提供了对线程的支持.它们各自是, 一.平台无关的线程类 二.线程安全的事件投递 三.跨线程的信号-槽连接. 这使得开发轻巧的多线程Qt程序更为easy,并能充分利用多处理器机器的优势. ...
- linux命令之head、tail命令具体解释
head 语法 样例 tail 语法 样例 head和tail组合使用方法举例 head 语法 head [-n -k ]... [FILE]... 样例 默认是显示开头前10行. head /etc ...
- iOS 程序进入后台,包含用户上拉快捷菜单导致程序失去活跃的研究
今日在使用某App时候,突然发现上拉菜单.程序视频扔在播放,咦!引起了我的兴趣. 首先,列出两个方法, 第一个方法是AppDelegate的代理.当程序进入后台时候调用 - (void)applica ...
- 简单模拟javaScript面向对象
<html> <head> <script type="text/javascript"> if (!Object.create) { Obje ...
- MATLAB 的字符串分析
MATLAB的字符串分析. 字符串实际上是指1Xn 的字符数组. MATLAB软件具有强大的字符串处理功能,提供了很多的字符或字符串处理函数,包括字符串的创建.字符串的属性.比较.查找以及字符串的转换 ...
- python中的多进程处理
转载于:http://blog.csdn.net/jj_liuxin/article/details/3564365 帮助文档见https://docs.python.org/2.7/library/ ...
- mvc5整合Autofac
本文中将使用 mvc5与webapi2进行对Autofac整合 准备工作: 1.vs2013 or vs2013+ 2.网络良好,nuget正常访问 好了需要的准备工作就这么多. ---------- ...
- mysql被动模式下的主主配置
mysql 架构最简单用得也最多的的是主从,主主等,主从有个切换的问题,从库不可写,在主库一定的情况下,切换挺麻烦,这里可以用主主模式. 但是主主也有个问题,就是两边同时写有可能冲突,主键冲突,虽然可 ...
- vs2015创建webService
- NERDTree 快捷件
key description ctrl+e 打开/关闭文件浏览器 j 向下移动 k 向上移动 o 小写字母o,打开文件或者展开目录 shift+c 即大写字母C,当前选中目录作为根目录 u 上一层目 ...