HDU 1010 Tempter of the Bone DFS 简单题 注意剪枝

题意:一只小狗要刚好在t时刻从起点都到终点,问可不可以。

注意剪枝。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 using namespace std;
 5 int maze[9][9];
 6 bool vis[9][9];
 7 int n,m,t;
 8 bool ans;
 9 struct Point
10 {
11     int x,y;
12 };
13 int dx[4]={0,0,-1,1};
14 int dy[4]={1,-1,0,0};
15 Point p,e;
16 void dfs(Point cur,int dep)
17 {
18     vis[cur.x][cur.y]=true;
19     if(!ans&&cur.x==e.x&&cur.y==e.y&&dep==t)
20     {
21         ans=true;
22         return ;
23     }
24     if(!ans&&dep>t)
25         return ;
26     for(int i=0;i<4;i++)
27     {
28         Point dcur;
29         dcur.x=cur.x+dx[i];
30         dcur.y=cur.y+dy[i];
31         if(dcur.x<1||dcur.x>n||dcur.y<1||dcur.y>m)
32             continue;
33         if(vis[dcur.x][dcur.y])
34             continue;
35         if(!maze[dcur.x][dcur.y])
36             continue;
37         if(!ans)
38         {
39             dfs(dcur,dep+1);
40             vis[dcur.x][dcur.y]=false;//注意回溯
41         }
42     }
43 }
44 int main()
45 {
46     while(scanf("%d%d%d",&n,&m,&t))
47     {
48         if(n==0&&m==0&&t==0)
49             break;
50         char s[15];
51         for(int i=1;i<=n;i++)
52         {
53             scanf("%s",s+1);
54             for(int j=1;j<=m;j++)
55             {
56                 if(s[j]==‘X‘)
57                     maze[i][j]=0;
58                 else if(s[j]==‘.‘)
59                     maze[i][j]=1;
60                 else if(s[j]==‘S‘)
61                 {
62                     p.x=i;
63                     p.y=j;
64                     maze[i][j]=1;
65                 }
66                 else
67                 {
68                     e.x=i;
69                     e.y=j;
70                     maze[i][j]=1;
71                 }
72             }
73         }
74         ans=false;
75         memset(vis,false,sizeof(vis));
76         int a=abs(e.x+e.y-p.x-p.y);
77         if(a>t)    //剪枝
78             ans=false;
79         else if(a%2!=t%2)   //剪枝
80             ans=false;
81         else
82             dfs(p,0);
83         if(ans)
84             printf("YES\n");
85         else
86             printf("NO\n");
87     }
88     return 0;
89 }

提交代码

时间: 2024-10-26 04:30:20

HDU 1010 Tempter of the Bone DFS 简单题 注意剪枝的相关文章

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 (DFS 奇偶剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 75141    Accepted Submission(s): 20531 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): 129289    Accepted Submission(s): 34906 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): 82702    Accepted Submission(s): 22531 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): 78390    Accepted Submission(s): 21395 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) &amp;&amp; hdu-1015 Safecracker(简单搜索)

http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心外,还需要强力的剪枝. 奇偶性剪枝:参考 http://www.cppblog.com/Geek/archive/2010/04/26/113615.html 1 #include <iostream> 2 #include <cstring> 3 #include <cstdi

HDU 1010 Tempter of the Bone(DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四类元素组成. S'代表是开始位置: 'D'表示结束位置:'.'表示可以走的路:'X'表示是墙. 问:从‘S’  能否在第 t 步 正好走到 'D'. 解题思路: 1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,t; 4 int

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