题目大意:输入三个整数N,M,T。在接下来的N行、M列会有一系列的字符。其中S表示起点,D表示终点。 .表示路 。 X表示墙。。。问狗能有在T秒时到达D。如果能输出YES,

否则输出NO

解题思路:DFS+剪枝

代码如下:

/*
* 1010_3.cpp
*
* Created on: 2013年8月16日
* Author: Administrator
*/ #include <iostream> using namespace std; bool flag;
int N, M, T;
int si, sj;
int ei, ej; const int maxn = 8;
int dir[4][2] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
char map[maxn][maxn]; bool checkEdge(int x, int y) {
if (!(x < 1 || x > N || y < 1 || y > M)) {
return true;
} return false;
} void dfs(int curX, int curY, int curT) {
if (flag) {//如果找到出口,直接返回
return;
} if (curT > T) {//如果当前花费的时间大于T,就返回
return;
} //花费的时间加上剩余需要的时间若大于T,返回
if (curT + abs(curX - ei) + abs(curY - ej) > T) {
return;
} //若到达出口最短距离和剩余时间的奇偶性不同,返回
if ((abs(curX - ei) + abs(curY - ej)) % 2 != abs(T - curT) % 2) {
return;
} if (curX == ei && curY == ej && curT == T) {
printf("YES\n");
flag = true;
return;
} int i;
for (i = 0; i < 4; ++i) {
int nextX = curX + dir[i][0];
int nextY = curY + dir[i][1]; if (checkEdge(nextX, nextY)) {
if (map[nextX][nextY] != 'X') {
map[nextX][nextY] = 'X';//使其不可达到
dfs(nextX, nextY, curT + 1);
map[nextX][nextY] = '.';//回溯时,恢复原来的状态
}
}
}
}
int main() {
while (scanf("%d%d%d", &N, &M, &T) != EOF, N || M || T) {
getchar();
int k = 0;
int i, j;
flag = false;
memset(map, 0, sizeof(map));
for (i = 1; i <= N; ++i) {
for (j = 1; j <= M; ++j) {
scanf("%c", &map[i][j]); if (map[i][j] == 'S') {
si = i;
sj = j;
map[i][j] = 'X'; //将开始位置置为不可达状态
} else if (map[i][j] == 'D') {
ei = i;
ej = j;
} if (map[i][j] != 'X') {
++k;
}
}
getchar();
} if (k < T) {//若可走的点数小于T,则输出NO
printf("NO\n");
continue;
} dfs(si, sj, 0); if (!flag) {
printf("NO\n");
}
}
}

(step4.3.1) hdu 1010(Tempter of the Bone——DFS)的更多相关文章

  1. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  2. 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 ...

  3. HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...

  4. hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...

  5. HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)

    需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...

  6. HDU 1010 Tempter of the Bone (DFS+可行性奇偶剪枝)

    <题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...

  7. HDOJ.1010 Tempter of the Bone (DFS)

    Tempter of the Bone [从零开始DFS(1)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tem ...

  8. 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 ...

  9. hdu 1010 Tempter of the Bone 奇偶剪枝

      如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...

随机推荐

  1. jquery.ajax提交多值(数组)

    偶尔会遇到类似复选框的一个属性存在多值情况,若使用ajax提交的化,设置data :{ids:[1,2,3,4]} 提交后,后台无法使用ids获取到数据. 这里可以用到ajax的 traditiona ...

  2. PHP 单链表

    <?php class Hero { public $no; public $name; public $nickname; public $next=null; public function ...

  3. C#获取磁盘列表与信息

    方法1:使用Environment //获取当前计算机逻辑磁盘名称列表 String[] drives = Environment.GetLogicalDrives(); Console.WriteL ...

  4. C# ZXing.Net生成二维码、识别二维码、生成带Logo的二维码(二)

    1.使用ZXint.Net生成带logo的二维码 /// <summary> /// 生成带Logo的二维码 /// </summary> /// <param name ...

  5. oracle存储过程写法。

    create or replace procedure testwzm(v_gdjdm in varchar2) isv_id varchar2(10);v_xlname varchar2(100); ...

  6. 一些SQL语句的问题

    1.getdate()函数问题 go create table table_1( id int primary key identity, name ) not null, daytime datet ...

  7. 如何在Eclipse中给main方法加参数

    在main方法中有一个args参数,那么如何给args参数赋值呢? public class TestMain { public static void main(String[] args) { f ...

  8. 你好,C++(14)如何描述“一个名叫陈良乔,年龄33岁,身高173厘米,体重61.5千克的男人”——3.8 用结构体类型描述复杂的事物

    3.8  用结构体类型描述复杂的事物 利用C++本身所提供的基本数据类型所定义的变量,只能表达一些简单的事物.比如我们可以用int类型定义nAge变量表示人的年龄,用string类型定义strName ...

  9. 利用google浏览器开发者工具调试网页(详)

    前端程序员或者在校大学生正在开发网页,如果想要测试或者通过测试优化网页结构,该怎么办呢?这就需要用到一款工具,chrome浏览器的开发者工具?本文写给尚不熟悉这个开发者工具的同学们或者同行们,话不多说 ...

  10. [转]用Objective-C实现简单的数学字符串公式的计算

    好友第一次用写技术分享,这么多年都没见他正经的写点东西.那天突然抬头问我,Objective-C有没字符串计算的.我说,没有.后来他默默实现了,特为他转发,表示支持. ================ ...