HDU 1010--DFS第二炮

        HDU 1010 

                               --DFS第二炮

题目:

                                                                                       

                                                                       Tempter of the Bone

     

                                   

                                                                 

                                                                                                                       

                                                                        time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

       

                   

                                                                                            Total Submission(s): 29454 Accepted Submission(s): 8029

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

分析:

        

此题讲的是一只狗要恰好为t秒时走出迷宫,求其可能性。开始时写了一个DFS,悲剧的TLE了。后来才知道有个奇偶性剪枝,对于奇偶性剪枝

        可以把map看成这样:
         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或1->0 必然是奇数步 ,0->0 走1->1 必然是偶数步
         所以当遇到从 0 走向 0 但是要求时间是奇数的,或者, 从 1 走向 0 但是要求时间是偶数的 都可以直接判断不可达!

         所以

         v=t-time-abs(dx-x)-abs(dy-y); 在这里 判断当前位置步数的奇偶性

         if(v<0||v&1)  

return;

         还有一个地方就是

         if(flag) return; 不写它的话会超时,写上这几句话就能及时的返回结果,同时也说明了DFS的层数之多,导致超时,需注意这一点。

代码:

 1 #include<stdio.h>
2 #include<math.h>
3
4 char map[10][10];
5 int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
6 int m,n,t,sx,sy,dx,dy,flag;
7 void dfs(int x,int y,int time)
8 {
9 int i,x1,y1,v;
10 if(flag)
11 return;
12 v=t-time-abs(dx-x)-abs(dy-y);
13 if(v<0||v&1)
14 return;
15 if(x==dx&&y==dy&&t==time)
16 flag=1;
17 for(i=0;i<4;i++)
18 {
19 x1=x+dir[i][0];
20 y1=y+dir[i][1];
21 if(x1>=0&&x1<m&&y1>=0&&y1<n&&map[x1][y1]!='X')
22 {
23 map[x1][y1]='X';
24 dfs(x1,y1,time+1);
25 map[x1][y1]='.';
26 }
27 }
28 }
29 int main()
30 {
31 int i,j,wall;
32 char temp;
33 while(scanf("%d%d%d",&m,&n,&t)!=EOF&&(m!=0&&n!=0&&t!=0))
34 {
35 flag=0;
36 wall=0;
37 scanf("%c",&temp);
38 for(i=0;i<m;i++)
39 {
40 for(j=0;j<n;j++)
41 {
42 scanf("%c",&map[i][j]);
43 if(map[i][j]=='S')
44 {
45 sx=i;
46 sy=j;
47 map[i][j]='X';
48 }
49 if(map[i][j]=='D')
50 {
51 dx=i;
52 dy=j;
53 }
54 if(map[i][j]=='X')
55 wall++;
56 }
57 scanf("%c",&temp);
58 }
59 if(n*m-wall>=t)
60 dfs(sx,sy,0);
61 if(flag==1)
62 printf("YES\n");
63 else
64 printf("NO\n");
65 }
66 return 0;
67 }

ps.有时候就得无所谓点,不能太较真哪~~~

posted @ 2012-01-26 18:54  hankers  阅读(240)  评论(0编辑  收藏  举报