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

ZJCPC2004

题意:

从地图中的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 }
时间: 2024-07-31 05:20:07

HDU1010 DFS+剪枝的相关文章

ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字.现在要求重排方块,使得每两个有边相连的方块对应的数字相同. 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下来,但是这个剪枝并没有起很大的作用,还是T了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

UVA 10318 - Security Panel dfs 剪枝

UVA 10318 - Security Panel dfs 剪枝 ACM 题目地址:UVA 10318 - Security Panel 题意: 这题跟点灯的题目很像,点灯游戏选择一盏灯时会让它以及四周的灯改变状态. 但是我们有特殊的开开关技巧,它给出了改变状态的位置,而不是四周都改变. 问你从全部关着变成全部开着的最小开关步骤. 分析: 很明显,在一个位置上点两次或更多次是没有必要的,所以一个位置只有选择与不选择,用dfs即可,但如果暴力所有可能,复杂度是2^25,会超时,所以要剪枝. 由于

Cubes(DFS+剪枝)

题意:给一个数N,求N最少由多少个数的立方构成,并输出这些数. 做法:DFS + 剪枝,剪枝的边界很很很重要! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <stdio.h> int cub[400]; int ans[300]; int tp[300]; int n; int sum = 0x3f3f3f3f; //个数 void dfs(int le

hdu 4109 dfs+剪枝优化

求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己增加的)出发,0到1~n个节点之间的距离为1,mt[i]表示从0点到第i个节点目前所得的最长路径 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> using namespace std; const

POJ 1564 Sum It Up (DFS+剪枝)

 Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5820   Accepted: 2970 Description Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4

EOJ1981 || POJ1011 经典dfs+剪枝+奇怪的数据

题目:EOJ1981 || POJ1011   经典dfs+剪枝+奇怪的数据 Description George took sticks of the same length and cut them randomly until all partsbecame at most 50 units long. Now he wants to return sticks to the originalstate, but he forgot how many sticks he had origi

HDOJ 5113 Black And White DFS+剪枝

DFS+剪枝... 在每次DFS前,当前棋盘的格子数量的一半小于一种颜色的数量时就剪掉 Black And White Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 194    Accepted Submission(s): 50 Special Judge Problem Description In mathematics,

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

DFS + 剪枝策略

一:简介 (1)相信做过ACM的人,都很熟悉图和树的深度优先搜索:算法里面有蛮力法 -- 就是暴力搜索(不加任何剪枝的搜索): (2)蛮力搜搜需要优化时,就是需要不停的剪枝,提前减少不必要的搜索路径,提前发现判断的过滤条件: (3)剪枝的核心问题就是设计剪枝判断方法,哪些搜索路径应当舍弃,哪些搜索路径不能舍弃(保留): (4)高效的剪枝过滤条件需要从局部和全局来考虑问题,发现内在的规律. (5)详细的剪枝算法,请见剪枝算法(算法优化) 二:示例验证 (1)题目来源于 poj 1011 Stick