HDU1010 DFS+剪枝

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 110290    Accepted Submission(s): 29967

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
 
Source
题意:
从地图中的S点经过T时间是否能够恰好到达D点;
代码:
 //很明显是一道dfs,但普通的dfs会超时,看了题解才明白原来可以奇偶剪枝。从起点到终点的距离如果是奇数t也是奇数才能到达,
//从起点到终点的距离如果是偶数t是偶数才能到达。从起点到终点的最短距离是两点的坐标之差,如果不走这条最短路,只有多走的步数是偶数
//步时才能到达终点。因此可以排除很多情况。可以自己画图看看。
//如果地图上可走的点少于t也不行。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,m,t;
int ans,tim;
int dir[][]={,,-,,,,,-};
bool vis[][];
char map[][];
void dfs(int sx,int sy)
{
if(tim>t)
return;
if(map[sx][sy]=='D')
{
if(tim==t)
ans=;
return;
}
for(int i=;i<;i++)
{
int x=sx+dir[i][],y=sy+dir[i][];
if(x<||x>=n||y<||y>=m) continue;
if(vis[x][y]) continue;
if(map[x][y]=='X') continue;
vis[x][y]=;
tim++;
dfs(x,y);
vis[x][y]=;
tim--;
if(ans==) return;
}
}
int main()
{
int sx,sy,ex,ey;
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
if(n==&&m==&&t==) break;
int cnt=;
for(int i=;i<n;i++)
{
scanf("%s",map[i]);
for(int j=;j<m;j++){
if(map[i][j]=='S')
{
sx=i;sy=j;
}
if(map[i][j]=='D')
{
ex=i;ey=j;
}
if(map[i][j]=='.')
cnt++;
}
}
int tem1=fabs(sx+sy-ex-ey);
ans=;
if(tem1%==t%&&cnt+>=t){
memset(vis,,sizeof(vis));
vis[sx][sy]=;
tim=;
dfs(sx,sy);
}
if(ans==) printf("YES\n");
else printf("NO\n");
}
return ;
}
上一篇:连接慢的主要原因是DNS解析导致


下一篇:[C#网络编程系列]专题一:网络协议简介