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 ...
随机推荐
- ARM开发板系统移植-----u-boot的编译
本文和另外两篇姊妹篇都是为了说明如何裁剪出适合在mini2440 开发板上运行的Linux系统,以记录自己的学习成果.其中本文先介绍了嵌入式系统的软件组成部分,然后介绍编译出适合在mini2440开发 ...
- [C#学习]在多线程中如何调用Winform[转]
问题的产生: 我的WinForm程序中有一个用于更新主窗口的工作线程(worker thread),但文档中却提示我不能在多线程中调用这个form(为什么?),而事实上我在调用时程序常常会崩掉.请问如 ...
- fedora22 无法联网的情况下rpm安装gcc5.1
前天发生件很不幸的事.我在给ubuntu14.04安装NVIDIA显卡驱动的时候,想清空下一个目录,什么目录我也忘了,当时我正好切到root身份(平常我很少切root的),命令格式如下 rm -fr ...
- Python学习 - 编写自己的ORM(2)
上一篇文章简单的实现了ORM(对象关系模型),这一篇文章主要实现简单的MySQL数据库操作. 想要操作数据库,首先要建立一个数据库连接.下面定义一个创建数据库连接的函数,得到一个连接叫做engine. ...
- Uva_11916 Emoogle Grid
题目链接 题意: 有个N X M的棋盘, 有K种颜色, 有B个不可涂色的位置, 共有R种涂色方案. 1)每个可涂色的位置必须涂上一种颜色 2)不可涂色位置不能涂色 3)每个位置必须从K种颜色中选出一种 ...
- Linq 数据合并,比较,连接,交叉 维恩图工具
Except 返回包含两个不同之处的linq结果集 Intersect 返回两个容器中共同的数据项 Union 返回所有成员,相同的成员出现多次,将只返回一个 Concat 返回所有数据项
- Android项目无法运行的解决方法
文件本身是否有问题 有两种方法可以判断:1.比较快捷的办法就是用解压软件打开或者解压的过程中有没有弹出什么错误提示,如果有错误提示那就重新下载一遍文件(javaapk用的压缩工具是winrar4.0, ...
- jQuery 表格插件25
jQuery 表格插件可以让你创建各种各样的表格布局,表格布局是报纸和杂志中最常见的布局,现在的网站中也很常见,在这篇文章中,我向大家推荐25个jQuery 的表格插件,你可以任意控制表格的行和列,用 ...
- xcode5时代如何设置Architectures和Valid Architectures
目前ios的指令集有以下几种: 1,armv6,支持的机器iPhone,iPhone2,iPhone3G及对应的iTouch 2,armv7,支持的机器iPhone4,iPhone4S 3,armv7 ...
- 要将PYTHON应用于工作啦
分析同事在线答疑的数据,考评模型还未最终给出: import time import sys import optparse #操作代码和同事名对应的文件 opfile = 'op_name.txt' ...