hdu 1010 Tempter of the Bone(dfs)

Tempter of the Bone

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

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

ZJCPC2004

Recommend

JGShining   |   We have carefully selected several similar problems for you:  1072 1015 1372 1258 1239

很好的搜索,需要强大的剪枝避免超时,参考了网上大神的解释。

第一个剪枝我们可以想到,当剩下的步数大于剩下的时间的时候,狗是不能走到的;

接下来我们来第二个剪枝:

我们把map的奇偶性以01编号:

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。

也就是说,如果当前的狗所在的坐标与D的坐标奇偶性不一样,那么狗需要走奇数步。

同理,如果狗所在坐标与D的坐标奇偶性一样,那么狗需要走偶数步数。

也就是说,狗的坐标x、y和对2取余是它的奇偶性,Dxy和对2取余是D的奇偶性。

两个奇偶性一加再对2取余,拿这个余数去与剩下时间对2取余的余数作比较即可。

题意:输入一个n*m的矩阵以及时间T。矩阵中有‘X‘:墙,不能走;‘.‘:通路,可以走;‘S‘:开始点。‘D‘:门,结束点。要求从开始点开始走,每秒一步,走过的路不能再走,走到D的时候正好花费时间T。输出“YES”或“NO”表示能否在时间T的时候走到D。

附上代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 int n,m,T;
 6 char map[10][10],fail,a2,b2;
 7 int visit[10][10];
 8 int f[4][2]= {1,0,-1,0,0,1,0,-1};
 9 int abs(int a)  //绝对值函数
10 {
11     return a>=0?a:-a;
12 }
13 void DFS(int x,int y,int k)
14 {
15     int i,nx,ny;
16     if(x==a2&&y==b2)   //无论是否时间刚好满足题意,此时都需要返回上一层
17     {
18         if(k==T)    //时间相等
19             fail=1;
20         return;
21     }
22     if(k>=T)   //超过时间的剪枝
23         return;
24     for(i=0; i<4; i++)
25     {
26         nx=x+f[i][0];
27         ny=y+f[i][1];
28         if(nx>=1&&ny>=1&&nx<=n&&ny<=m&&map[nx][ny]!=‘X‘&&!visit[nx][ny])
29         {
30             visit[nx][ny]=1;
31             DFS(nx,ny,k+1);
32             visit[nx][ny]=0;
33             if(fail) return;
34         }
35     }
36 }
37 int main()
38 {
39     int i,j,a1,b1;
40     while(~scanf("%d%d%d",&n,&m,&T))
41     {
42         if(n==0&&m==0&&T==0)
43             break;
44         for(i=1; i<=n; i++)
45         {
46             for(j=1; j<=m; j++)
47             {
48                 cin>>map[i][j];
49                 if(map[i][j]==‘S‘)   //记录起点位置
50                 {
51                     a1=i;
52                     b1=j;
53                 }
54                 else if(map[i][j]==‘D‘)  //终点位置
55                 {
56                     a2=i;
57                     b2=j;
58                 }
59             }
60         }
61         if((abs(a2-a1)+abs(b2-b1)>T)||((a1+a2+b1+b2+T)%2==1))  //剪枝,步数不够和奇偶剪枝
62         {
63             printf("NO\n");
64             continue;
65         }
66         memset(visit,0,sizeof(visit));  //标记步数是否走过
67         visit[a1][b1]=1;
68         fail=0;
69         DFS(a1,b1,0);
70         if(fail)  printf("YES\n");
71         else  printf("NO\n");
72     }
73     return 0;
74 }
时间: 2024-10-21 01:31:09

hdu 1010 Tempter of the Bone(dfs)的相关文章

HDU 1010 Tempter of the Bone (dfs)

#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; int n,m,t; int sx,sy; int ex,ey; int ok; char mat[10][10]; int vis[10][10]; int op[4][2]={1,

hdu 1010 Tempter of the Bone (DFS+剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 68206    Accepted Submission(s): 18719 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

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

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 70665    Accepted Submission(s): 19487 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdu 1010 Tempter of the Bone(dfs暴力)

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 despe

HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 125945    Accepted Submission(s): 33969 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of the Bone(深度+剪枝)

http://acm.hdu.edu.cn/showproblem.php?pid=1010 题意:就是给出了一个迷宫,小狗必须经过指定的步数到达出口,并且每个格子只能走一次. 首先先来介绍一下奇偶性剪枝: 在这道题目中,如果使用剪枝的话,可以节省不少的时间. 在这道题目中,每次dfs循环时都可以判断一下小狗当前位置与终点所相差的步数,如果不为偶数的话,说明到达不了终点,就可以退出这个循环,不必继续dfs了. 在这道题目中,由于每个格子只能经过一次,所以经过一次后,可以把该点位置改为'X',然后

【解题报告】Tempter of the Bone(DFS)

解题报告——Tempter of the Bon 问题描述 Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Java class name: Main The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to

HDU 1010 Tempter of the Bone dfs+剪枝

给你一个迷宫一个起点和一个终点,问你能否走T步刚好到达终点,不能重复走,并且只有4个方向 显然这是一个dfs,虽然N最大只有7,但是裸的dfs复杂度还是太高了,因此要进行一些剪枝 1.如果T比图上所有的可走点还要大,肯定是不可行的.这个可以避免dfs整张图. 2.奇偶剪枝,有性质当前点(x,y)到目标点(tx,ty)的所有路径的长度的奇偶性一定和|x-tx|+|y-ty|一样. #include <cstdio> #include <iostream> #include <c

【深搜加剪枝五】HDU 1010 Tempter of the Bone

Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 64326    Accepted Submission(s): 17567 Problem Description The doggie found a bone in an ancient maze, which fascinated him a l