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 doorX,doorY;
 5 char ca[10][10];
 6 int to[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};
 7 int dfs(int x,int y,int cnt)
 8 {
 9     if(x>n||y>m||x<=0||y<=0)return 0;
10     if(cnt==t&&x==doorX&&y==doorY)
11         return 1;
12     int tem=t-cnt-abs(x-doorX)-abs(y-doorY);
13     if(tem<0 || tem&1 )return 0;
14     for(int i=0; i<4; i++)
15     {
16         if(ca[x+to[i][0]][y+to[i][1]]!=‘X‘)
17         {
18             ca[x+to[i][0]][y+to[i][1]]=‘X‘;
19             if(dfs(x+to[i][0],y+to[i][1],cnt+1))return 1;
20             ca[x+to[i][0]][y+to[i][1]]=‘.‘;
21         }
22     }
23     return 0;
24 }
25 int main()
26 {
27     while(scanf("%d%d%d",&n,&m,&t)!=EOF&&n+m+t)
28     {
29         int i,j,wall=0,stratX,stratY;
30         getchar();
31         for(i=1; i<=n; i++)
32         {
33             for(j=1; j<=m; j++)
34             {
35                 scanf("%c",&ca[i][j]);
36                 if(ca[i][j]==‘S‘)
37                     stratX=i,stratY=j;
38                 else if(ca[i][j]==‘X‘)
39                     wall++;
40                 else if(ca[i][j]==‘D‘)
41                     doorX=i,doorY=j;
42             }
43             getchar();
44         }
45         if(n*m-wall<=t)
46         {
47             printf("NO\n");
48             continue;
49         }
50         ca[stratX][stratY]=‘X‘;
51         if(dfs(stratX,stratY,0))
52             printf("YES\n");
53         else
54             printf("NO\n");
55     }
56     return 0;
57 }
时间: 2024-11-10 03:04:46

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 简单题 注意剪枝

题意:一只小狗要刚好在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}

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