博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1010 dfs搜索
阅读量:5038 次
发布时间:2019-06-12

本文共 4081 字,大约阅读时间需要 13 分钟。

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

Problem Description
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

 题目大意是要在那个时间点找到D点,典型的DFS问题,重点是要求奇偶减枝

 

#include 
#include
#include
#include
using namespace std;#define Maxn 100int 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"); } }}

  

转载于:https://www.cnblogs.com/yakoazz/p/5723292.html

你可能感兴趣的文章
[BZOJ1083] [SCOI2005] 繁忙的都市 (kruskal)
查看>>
吴裕雄 python 机器学习——数据预处理嵌入式特征选择
查看>>
Centos6.4安装JDK
查看>>
201521123069 《Java程序设计》 第4周学习总结
查看>>
线性表的顺序存储——线性表的本质和操作
查看>>
【linux】重置fedora root密码
查看>>
数组的扩展
查看>>
关于空间背景颜色的操作
查看>>
HDU 6237 - A Simple Stone Game ( 分解质因数 )
查看>>
用swing做一个简单的正则验证工具
查看>>
百度坐标(BD-09)、国测局坐标(火星坐标,GCJ-02)和WGS-84坐标互转
查看>>
pig自定义UDF
查看>>
输入名字显示其生日,没有则让输入生日,做记录
查看>>
爬虫综合大作业
查看>>
HTML canvas原生js实现鼠标画图
查看>>
《程序设计入门——C语言》翁恺老师 第一周编程练习记录
查看>>
IE8兼容性视图问题
查看>>
Kubernetes 运维学习笔记
查看>>
Centos6.9下RabbitMQ集群部署记录
查看>>
Python之基本的日期与时间转换 datetime、 dateutil模块
查看>>