码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1010 DFS+剪枝

时间:2016-10-26 22:24:29      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:代码   more   key   hdu   one   period   cstring   limit   layout   

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点;
代码:
 1 //很明显是一道dfs,但普通的dfs会超时,看了题解才明白原来可以奇偶剪枝。从起点到终点的距离如果是奇数t也是奇数才能到达,
 2 //从起点到终点的距离如果是偶数t是偶数才能到达。从起点到终点的最短距离是两点的坐标之差,如果不走这条最短路,只有多走的步数是偶数
 3 //步时才能到达终点。因此可以排除很多情况。可以自己画图看看。
 4 //如果地图上可走的点少于t也不行。
 5 #include<iostream>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<cmath>
 9 using namespace std;
10 int n,m,t;
11 int ans,tim;
12 int dir[4][2]={1,0,-1,0,0,1,0,-1};
13 bool vis[10][10];
14 char map[10][10];
15 void dfs(int sx,int sy)
16 {
17     if(tim>t)
18     return;
19     if(map[sx][sy]==D)
20     {
21         if(tim==t)
22         ans=1;
23         return;
24     }
25     for(int i=0;i<4;i++)
26     {
27         int x=sx+dir[i][0],y=sy+dir[i][1];
28         if(x<0||x>=n||y<0||y>=m) continue;
29         if(vis[x][y]) continue;
30         if(map[x][y]==X) continue;
31         vis[x][y]=1;
32         tim++;
33         dfs(x,y);
34         vis[x][y]=0;
35         tim--;
36         if(ans==1) return;
37     }
38 }
39 int main()
40 {
41     int sx,sy,ex,ey;
42     while(scanf("%d%d%d",&n,&m,&t)!=EOF)
43     {
44         if(n==0&&m==0&&t==0) break;
45         int cnt=0;
46         for(int i=0;i<n;i++)
47         {
48             scanf("%s",map[i]);
49             for(int j=0;j<m;j++){
50             if(map[i][j]==S)
51             {
52                 sx=i;sy=j;
53             }
54             if(map[i][j]==D)
55             {
56                 ex=i;ey=j;
57             }
58             if(map[i][j]==.)
59             cnt++;
60             }
61         }
62         int tem1=fabs(sx+sy-ex-ey);
63         ans=0;
64         if(tem1%2==t%2&&cnt+1>=t){
65         memset(vis,0,sizeof(vis));
66         vis[sx][sy]=1;
67         tim=0;
68         dfs(sx,sy);
69         }
70         if(ans==1) printf("YES\n");
71         else printf("NO\n");
72     }
73     return 0;
74 }

 

HDU1010 DFS+剪枝

标签:代码   more   key   hdu   one   period   cstring   limit   layout   

原文地址:http://www.cnblogs.com/--ZHIYUAN/p/6001954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!