传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=1010
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 144191 Accepted Submission(s): 38474
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
Author
ZHANG, Zheng
题目意思:
给你一个图,给定起点和终点
问你能不能恰好在k步内到达终点
X:不能走
.:可以走
S:起点
D:终点
分析:
路不能重复走,时间要恰好是k
不能用BFS,因为bfs求的是最短路,而这个题最短路不一定符合要求
得用dfs
先说一下需要用到的剪枝:
1.如果当前步数大于等于k且还没有到D点,则剪掉
2.最短距离都大于k的直接输出no
3.奇偶剪枝
涉及到奇偶剪枝(说一下自己的理解):
把矩阵看成如下形式:
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
从为 0 的格子走一步,必然走向为 1 的格子 。
从为 1 的格子走一步,必然走向为 0 的格子 。
即:
从 0 走向 1 必然是奇数步,从 0 走向 0 必然是偶数步。
所以当遇到从 0 走向 0 但是要求时间是奇数的或者 从 1 走向 0 但是要求时间是偶数的,都可以直接判断不可达!
code:
#include<bits/stdc++.h> char s[10][10]; int ax,ay,bx,by,n,m,k; int t[4][2]={1,0,-1,0,0,1,0,-1};//方向引导数组 int vist[10][10],flag; void dfs(int x,int y,int c) { int i,mx,my; if(x==bx&&y==by)//找到终点 { if(k==c)//恰好在规定时间找到终点则标志位置1 flag=1; return; } if(c>=k)//超出规定时间,剪掉 return; if(s[x][y]!=‘X‘)//可走点 { for(i=0;i<4;i++) { mx=x+t[i][0]; my=y+t[i][1]; if(s[mx][my]!=‘X‘&&mx>=1&&mx<=n&&my>=1&&my<=m&&!vist[mx][my])//判断能不能往这个方向走 { vist[mx][my]=1; dfs(mx,my,c+1); vist[mx][my]=0;//回退 if(flag) //注意,在找到了目标之后,就不需要再找!以往编写dfs时,没有注意这点 return; } } } } int main() { while(scanf("%d%d%d",&n,&m,&k)>0&&(n+m+k)) { int i,c; for(i=1;i<=n;i++) { getchar(); for(int j=1;j<=m;j++) { scanf("%c",&s[i][j]); if(s[i][j]==‘S‘) { ax=i;//起点 ay=j; } if(s[i][j]==‘D‘) { bx=i;//终点 by=j; } } } getchar(); memset(vist,0,sizeof(vist)); if(abs(ax-bx)+abs(ay-by)>k||(ax+bx+ay+by+k)%2==1) // 最短距离都大于k的剪枝和奇偶剪枝 { printf("NO\n"); continue; } vist[ax][ay]=1; flag=0; c=0; dfs(ax,ay,c); if(flag==1) printf("YES\n"); else printf("NO\n"); } return 0; }
原文地址:https://www.cnblogs.com/yinbiao/p/9313239.html