hdu 1010 dfs搜索
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 107138 Accepted Submission(s): 29131
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.
题目大意是要在那个时间点找到D点,典型的DFS问题,重点是要求奇偶减枝
#include <stdio.h>
#include <math.h>
#include <cmath>
#include <iostream>
using namespace std;
#define Maxn 100
int hang,lie,Time;
char MAP[Maxn][Maxn];
int begin_x,begin_y;
int end_x,end_y;
bool flag;
int dir[4][2] = {
{0,1},
{0,-1},
{1,0},
{-1,0}
};
void print()
{
for(int i = 0; i < hang; i++)
{
for(int j = 0; j < lie; j++)
{
printf("%c",MAP[i][j]);
}
printf("\n");
}
}
void dfs(int x, int y, int t)
{
// print();
// printf("\n");
if (flag)
{
return ;
}
// printf("Q\n", );
if (x == end_x && y == end_y && t == Time)
{
// printf("YES~~~~~~~~~~~\n");
flag = true;
return ;
}
int temp = (Time - t) - ( abs(x- end_x) + abs(y - end_y) );
if (temp < 0 || temp & 1)
{
return ; // 奇偶剪枝
/*要理解奇偶剪枝,先了解一下曼哈顿距离,
从一个点到达另外一个点的最
短路径长度(时间)可以根据两点坐标求出,
路径长度(非最短)与最短路径的长度同奇偶,
它们的差一定是偶数!举个例子,就像两个偶数的差
差是偶数,两个个数的差也是偶数.*/
}
for(int i = 0; i < 4; i++)
{
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if (xx >= 0 && xx < hang && yy >= 0 && yy < lie)
{
if (MAP[xx][yy] != 'X')
{
MAP[xx][yy] = 'X';
dfs(xx,yy,t+1);
MAP[xx][yy] = '.';
}
}
}
return ;
}
int main()
{
while(cin >> hang >> lie >> Time,hang + lie + Time)
{
flag = false;
for(int i = 0; i < hang; i++)
{
scanf("%s",MAP[i]);
}
for(int i = 0; i < hang; i++)
{
for(int j = 0; j < lie; j++)
{
if (MAP[i][j] == 'S')
{
begin_x = i;
begin_y = j;
}
else if (MAP[i][j] == 'D')
{
end_x = i;
end_y = j;
}
}
}
// printf("%d %d\n",begin_x,begin_y);
MAP[begin_x][begin_y] = 'X';
dfs(begin_x,begin_y,0);
if (flag)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
}
hdu 1010 dfs搜索的更多相关文章
- HDU 1010 (DFS搜索+奇偶剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...
- hdu 1010(DFS) 骨头的诱惑
http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意从S出发,问能否在时间t的时候到达终点D,X为障碍 需要注意的是要恰好在t时刻到达,而不是在t时间 ...
- HDU 1045 (DFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...
- HDU 1241 (DFS搜索+染色)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...
- Tempter of the Bone HDU 1010(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
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- HDU 1312:Red and Black(DFS搜索)
HDU 1312:Red and Black Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- hdu 1312:Red and Black(DFS搜索,入门题)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Tempter of the Bone HDU - 1010(dfs)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- 拥抱模块化的JavaScript
前言 我们再一次被计算机的名词.概念笼罩. Backbone.Emberjs.Spinejs.Batmanjs 等MVC框架侵袭而来.CommonJS.AMD.NodeJS.RequireJS.Sea ...
- AdaBoost原理,算法实现
前言: 当做重要决定时,大家可能综合考虑多个专家而不是一个人的意见.机器学习处理问题也是如此,这就是元算法背后的思路.元算法是对其他算法进行组合的一种方式,前几天看了一个称作adaboost方法的介绍 ...
- Java面向对象程序设计--与C++对比说明:系列1(面向对象基础)
面向对象程序设计作为一种重要的程序设计思想,在近些年来得到广泛的推崇.而Java和C++更是面向对象程序设计语言中的翘楚,要彻底摸清面向对象程序设计的精髓和在实际应用中更加高效的进行代码设计 ...
- ecshop 函数列表大全
lib_time.phpgmtime() P: 获得当前格林威治时间的时间戳 /$0server_timezone() P: 获得服务器的时区 /$0local_mktime($hour = NULL ...
- sql语句select group by order by where一般先后顺序 转载
写的顺序:select ... from... where.... group by... having... order by..执行顺序:from... where...group by... h ...
- pip 的 Assert Error
在Ubuntu 14.04 中安装了Python之后,使用pip freeze,出现AssertError. 发现是pip版本太低,只有1.5.6 更新pip之后就OK了.
- 实现 winform 异步跨线程访问UI控件
在开发winform时经常会用到多线程防止界面出现假死现象,比如当你单击某个按钮时,需要执行很多代码,但是在执行过程中想实时的将当前执行的情况报告给用户,类型进度条或文本什么的. 这个时候很显然,如果 ...
- Ajax跨域访问问题-方法大全
Case I. Web代理的方式 (on Server A) 即用户访问A网站时所产生的对B网站的跨域访问请求均提交到A网站的指定页面,由该页面代替用户页面完成交互,从而返回合适的结果.此方案可以解决 ...
- WPF Canvas小例子
源码下载:DraggingElementsInCanvas_demo.rar
- 解决Tomcat无法加载css和js等静态资源文件
解决思路有两个 一是,你使用了Apache服务器,html不交给Tomcat处理,所以你找不到Html等静态资源,所以你先停掉阿帕奇,然后只用Tomcat猫试试. 二是,像我一样,使用了Jetty开发 ...