DFS深度优先搜索结合剪枝的应用
来源:互联网 发布:数据库视图和表的区别 编辑:程序博客网 时间:2024/05/16 19:55
题目大意:老鼠的起点在S,出后在D,X是墙壁不能通过,.可以通过。出口在第T时刻开启,老鼠必须在这这个时刻到达出口,中途不能停留。
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.
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
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
'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
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
YES
这个题难的到不是DFS,而是剪枝的应用。刚开始总是超时,后来还是再网上看了些代码,加了一些剪枝条件才没超时。
#include <iostream>#include <cstdio>#include <cmath>using namespace std;char map[7][7];int m, n, t;int startx, starty, endx, endy; //定义开始点,和结束点int dir[4][2] = { { 0, -1 }, { 1, 0 }, { -1, 0 }, { 0, 1 } };int escape; //是否逃脱标识//求绝对值。int myabs(int a) {return a < 0 ? -a : a;}void dfs(int x, int y, int time) {//重要的剪枝,减少搜索量。 否则超时//相隔的距离不能太大,否则不可达。相隔的距离和剩余时间,应该同为奇数或偶数。int temp = myabs(x - endx) + myabs(y - endy) - (t - time);if (temp > 0 || temp % 2)return;int i;for (i = 0; i < 4; i++) {int tempx = x + dir[i][0];int tempy = y + dir[i][1];if (map[tempx][tempy] == 'D' && time == t - 1) {escape = 1;return;}if (map[tempx][tempy] == '.' && tempx >= 0 && tempx < n && tempy >= 0&& tempx < n) {map[x][y] = 'X';dfs(tempx, tempy, time + 1);map[x][y] = '.';if (escape) //如果已经逃脱,就直接返回。return;}}}int main() {while (1) {scanf("%d %d %d", &m, &n, &t);if (0 == m)break;getchar();int wall = 0;int i,j;for ( i = 0; i < m; i++) {for ( j = 0; j < n; j++) {scanf("%c", &map[i][j]);if ('S' == map[i][j]) {startx = i;starty = j;} else if ('D' == map[i][j]) {endx = i;endy = j;}else if('X' == map[i][j]){wall++;}}getchar();}//第一次剪枝if(n*m - wall <= t){printf("NO\n");continue;}escape = 0;dfs(startx, starty, 0);if (escape)printf("YES\n");elseprintf("NO\n");//break;}return 0;}
- DFS深度优先搜索结合剪枝的应用
- poj1011 Sticks DFS深度优先搜索+剪枝
- poj3172 Scales DFS深度优先搜索+剪枝
- poj1979 Red and Black DFS深度优先搜索+剪枝
- poj1321 棋盘问题 DFS深度优先搜索+剪枝
- DFS深度优先搜索(6)--hdu1455(经典深搜+剪枝)
- 第七章 深度优先搜索(dfs+可行性剪枝)
- 一个简单的DFS(深度优先搜索)算法加剪枝技术的程序演示
- 深度优先搜索剪枝--xiaozhui
- 深度优先搜索 DFS
- 深度优先搜索 DFS
- DFS 深度优先搜索
- DFS 深度优先搜索
- 深度优先搜索(DFS)
- 深度优先搜索(DFS)
- 深度优先搜索DFS
- DFS深度优先搜索
- 深度优先搜索(DFS)
- Hibernate映射的基本操作
- PHP 转义与反转义函数(入库操作)
- 使用ArcEngine 对遥感影像分类时错误解决
- 数组指针&指针数组
- Linux上的free命令详解
- DFS深度优先搜索结合剪枝的应用
- org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
- 两分钟学会在GitHub托管代码
- //@JSON(serialize=false) //@JSON(serialize=false)打开来加载 否则会报alert(data)错误JSONExceprion
- 1_JAVA概述
- 黑马程序员-多态
- poi解析excel内存溢出
- Oracle带输入、输出参数的存储过程 返回受影响的行数 SQL%ROWCOUNT
- Android输入法挤乱布局问题