http://acm.hdu.edu.cn/showproblem.php?pid=1010

给出一个n*m的迷宫 X为墙 .为空地 S为起点 D为终点 给出时间T

每走一步花费一单位的时间 走过的空地会消失不能再次经过

问能不能刚好花费T单位时间到达终点(在T时间前到达终点也算失败)

典型深搜 为减少时间花费加点剪枝

1.奇偶剪枝:给出一个01矩阵如下

从0到0或从1到1 必然要经过偶数步 从0到1或从1到0 必然要经过奇数步 所以可以通过比较起点终点的属性 判明是否已经是不可能到达的

2.如果总的地板数 小于(T - 1)个 也不可能到达

# include <stdio.h>
# define MAX 10
# define X x+dir[i][0]
# define Y y+dir[i][1] int n, m, time, sx, sy, ex, ey, dir[4][2] = {{0,-1},{1,0},{0,1},{-1,0}};
char pic[MAX][MAX];
int sign[MAX][MAX];//奇偶矩阵 bool is_ok()//奇偶剪枝
{
if(time % 2 == 0)
{
if(sign[sx][sy] == sign[ex][ey]) return true;
else return false;
}
else
{
if(sign[sx][sy] != sign[ex][ey]) return true;
else return false;
}
} bool in_map(int a, int b)//判断边界
{
return a >= 0 && a < n && b >= 0 && b < m;
} bool dfs(int x, int y, int step)
{
if(x == ex && y == ey)//到达出门 若门刚好打开 返回成功
{
if(step == time) return true;
else return false;
} for(int i = 0; i < 4; i++)//四向拓展
{
if(in_map(X, Y) && pic[X][Y] != 'X')
{
pic[X][Y] = 'X';
if(dfs(X, Y, step + 1)) return true;//若返回的是成功 则说明已找到路 退出
pic[X][Y] = '.';
}
}
return false;
} int main()
{
for(int i = 0; i < MAX; i++)//初始化奇偶矩阵
{
sign[i][0] = i % 2;
for(int j = 1; j < MAX; j++)
sign[i][j] = (sign[i][j - 1] + 1) % 2;
} while(scanf("%d %d %d", &n, &m, &time) && (n || m || time))
{
int groud = 0;//空地计数器
for(int i = 0; i < n; i++)
{
scanf("%s",pic[i]);
for(int j = 0; j < m; j++)
{
if(pic[i][j] == 'S')//起点
sx = i, sy = j, pic[i][j] = 'X';
else if(pic[i][j] == 'D')//终点
ex = i, ey = j, pic[i][j] = '.';
else if(pic[i][j] == '.')
groud++;
}
} if(is_ok() && groud + 1 >= time && dfs(sx, sy, 0)) printf("YES\n");
else printf("NO\n");
} return 0;
}
/*
/*
测试数据:
1 5 4
S...D
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
2 2 1
S.
.D
8 8 7
.DXS...X
........
XX..XX..
.X.X.X.X
..X.....
X....X..
........
XXXX....
5 4 18
S...
....
....
....
...D
6 6 10
S.....
......
......
......
......
.....D
2 3 2
SDX
..X
2 2 3
SD
..
2 2 2
SD
XX
4 4 6
.S..
XXX.
XXX.
XXXD
5 4 8
S...
.XX.
.X..
.X.X
....
3 3 3333
.S.
...
...
2 2 1
SD
..
1 5 4
S...D
4 5 5
.S...
..X..
.XDX.
..X..
2 4 7
SD..
....
2 2 3
S.
D.
4 4 9
S..X
X.X.
..XD
.... 输出:
YES
NO
YES
NO
NO
NO
YES
NO
YES
NO
NO
NO
NO
YES
YES
NO
YES
YES
YES
*/

  

HDOJ-1010 Tempter of the Bone(dfs+剪枝)的更多相关文章

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

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

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

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

  3. hdoj 1010 Tempter of the Bone【dfs查找能否在规定步数时从起点到达终点】【奇偶剪枝】

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

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

  5. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

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

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

  7. (step4.3.1) hdu 1010(Tempter of the Bone——DFS)

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

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

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

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

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

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

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

随机推荐

  1. vs2005中无法修改控件ID

    方法一:撤换到源代码模式下,通过代码更改id 方法二: 1.关闭VS2005: 2.删除目录 C:\Documents and Settings\Administrator\Local Setting ...

  2. c++智能指针《一》 auto_ptr

    转载http://www.cnblogs.com/gnagwang/archive/2010/11/19/1881811.html C++的auto_ptr auto_ptr所做的事情,就是动态分配对 ...

  3. android滑动基础篇 TouchView

    效果图: 代码部分: activity类代码: package com.TouchView; import android.app.Activity; import android.os.Bundle ...

  4. pyqt 图片(label上显示

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from decimal import * from PyQt4.Q ...

  5. pyqt字符串分离开,放入列表中

    string1 = ''''' the stirng Has many line In THE fIle ''' list_of_string = string1.split() print list ...

  6. python学习之路-5 基础进阶篇

    本篇涉及内容 双层装饰器字符串格式化 双层装饰器 装饰器基础请点我 有时候一个功能需要有2次认证的时候就需要用到双层装饰器了,下面我们来通过一个案例详细介绍一下双层装饰器: 执行顺序:自上而下 解释顺 ...

  7. C#中的一种按日期分文件夹的日志写法

    众所周知,日志是调试程序的有效途径,有一个好的日志代码,是一个程序小猿梦寐以求的. 以下是我结合网上资源自己总结的一小段代码,请笑纳: 转载请注明来源: http://www.cnblogs.com/ ...

  8. HttpWebRequest上传文件(Excel等)

    //上传代码/// <summary> /// 文件上传 /// </summary> /// <param name="strAddress"> ...

  9. Web 前沿——HTML5 Form Data 对象的使用(转)

    XMLHttpRequest Level 2 添加了一个新的接口——FormData.利用 FormData 对象,我们可以通过 JavaScript 用一些键值对来模拟一系列表单控件,我们还可以使用 ...

  10. startActivityForResult中回调setResult注意事项

    读 http://www.cnblogs.com/lijunamneg/archive/2013/02/05/2892616.html 有感 文中提出了一个核心问题: Android activity ...