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'.
解题思路:
平常心态做dfs即可,稍微加个奇偶剪枝,第一次做没经验,做过一次下次就知道怎么做了。最后有代码注释解析。
AC Code:
- #include<bits/stdc++.h>
- using namespace std;
- int n,m,t;
- int doorX,doorY;
- char ca[][];
- int to[][] = {{,-},{,},{-,},{,}};
- int dfs(int x,int y,int cnt)
- {
- if(x>n||y>m||x<=||y<=)return ;
- if(cnt==t&&x==doorX&&y==doorY)
- return ;
- int tem=t-cnt-abs(x-doorX)-abs(y-doorY);
- if(tem< || tem& )return ;
- for(int i=; i<; i++)
- {
- if(ca[x+to[i][]][y+to[i][]]!='X')
- {
- ca[x+to[i][]][y+to[i][]]='X';
- if(dfs(x+to[i][],y+to[i][],cnt+))return ;
- ca[x+to[i][]][y+to[i][]]='.';
- }
- }
- return ;
- }
- int main()
- {
- while(scanf("%d%d%d",&n,&m,&t)!=EOF&&n+m+t)
- {
- int i,j,wall=,stratX,stratY;
- getchar();
- for(i=; i<=n; i++)
- {
- for(j=; j<=m; j++)
- {
- scanf("%c",&ca[i][j]);
- if(ca[i][j]=='S')
- stratX=i,stratY=j;
- else if(ca[i][j]=='X')
- wall++;
- else if(ca[i][j]=='D')
- doorX=i,doorY=j;
- }
- getchar();
- }
- if(n*m-wall<=t)
- {
- printf("NO\n");
- continue;
- }
- ca[stratX][stratY]='X';
- if(dfs(stratX,stratY,))
- printf("YES\n");
- else
- printf("NO\n");
- }
- return ;
- }
代码解析:
- #include<bits/stdc++.h>
- using namespace std;
- int n,m,t;//n*m 矩阵 和 走的步数 t
- int doorX,doorY;//门的位置
- char ca[][];//矩阵大小
- int to[][] = {{,-},{,},{-,},{,}};
- int dfs(int x,int y,int cnt)
- {
- if(x>n||y>m||x<=||y<=)return ;//位置越界
- if(cnt==t&&x==doorX&&y==doorY)//已走步数 == 要走的步数 t,且位置刚好是门的位置,成功找到一条路
- return ;
- //! { 奇偶性剪枝:
- int tem=t-cnt-abs(x-doorX)-abs(y-doorY);
- if(tem< || tem& )return ;
- // 剩余的步数小于0 || tem 为奇数 都不满足 return 0 即可!}
- //@{ 遍历八个方向 递归dfs 如果有条件满足 return 1,否则结束 return 0;
- for(int i=; i<; i++)
- {
- if(ca[x+to[i][]][y+to[i][]]!='X')
- {
- ca[x+to[i][]][y+to[i][]]='X';
- if(dfs(x+to[i][],y+to[i][],cnt+))return ;
- ca[x+to[i][]][y+to[i][]]='.';
- }
- }
- return ;
- // @}
- }
- int main()
- {
- while(scanf("%d%d%d",&n,&m,&t)!=EOF&&n+m+t)
- {
- int i,j,wall=,stratX,stratY;
- getchar();
- // 001 { 记录开始位置,墙的个数,以及门的位置
- for(i=; i<=n; i++)
- {
- for(j=; j<=m; j++)
- {
- scanf("%c",&ca[i][j]);
- if(ca[i][j]=='S')
- stratX=i,stratY=j;
- else if(ca[i][j]=='X')
- wall++;
- else if(ca[i][j]=='D')
- doorX=i,doorY=j;
- }
- getchar();
- }
- // 001 }
- //002 {剪一下枝: 如果墙的个数+走的步数>=矩阵单元的个数,则肯定无法完成此任务,因为还要有开始和门的位置存在
- if(n*m-wall<=t)
- {
- printf("NO\n");
- continue;
- }
- //002 }
- ca[stratX][stratY]='X';//003 { 将开始位置置为墙不可再次访问 }
- if(dfs(stratX,stratY,))
- printf("YES\n");
- else
- printf("NO\n");
- }
- return ;
- }
HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)的更多相关文章
- hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- HDU 1010 Tempter of the Bone (DFS+可行性奇偶剪枝)
<题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...
- hdu 1010 Tempter of the Bone 深搜+剪枝
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)
需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...
- (step4.3.1) hdu 1010(Tempter of the Bone——DFS)
题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...
随机推荐
- Android Toast效果设置
Android Toast效果设置 Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失.总 ...
- [转]使用Sencha Ext JS 6打造通用应用程序
原文地址:http://www.uedsc.com/using-sencha-ext-js-6-to-build-universal-apps.html 在Sencha和整个Ext JS团队的支持下, ...
- ivy,ivyde插件-eclipse
官方共享更新磁盘下载 http://www.apache.org/dist/ant/ivyde/updatesite/ http://ant.apache.org/ivy/download.cgi 简 ...
- Spring Security3学习实例
Spring Security是什么? Spring Security,这是一种基于Spring AOP和Servlet过滤器的安全框架.它提供全面的安全性解决方案,同时在Web请求级和方法调用级处理 ...
- 普通用户安装 R 包
转自 http://bnuzhutao.cn/archives/901 一般 R 语言的书籍上,介绍安装 R 包的方法都是这样的: install.packages("packagename ...
- camerc文件播放
下载CamtasiaStudio 5中文版,打开CamtasiaStudio.exe在左边有生成选项,点左边工具框内添加/导入媒体,则选择下面的批量生成.弹出添加文件的菜单点添加文件,加入你要转换的c ...
- 57. Android之程序调试LogCat (转)
无论什么样的程序开发过程中,出现错误都是不可避免的,一般情况下,语法错误会被开发环境检测到,并能及时的提示我们错误的位置以及修改的方法,但是逻辑错误就不是那么容易被发现了,通常逻辑错误的定位和分析是一 ...
- Python:Sqlmap源码精读之解析xml
XML <?xml version="1.0" encoding="UTF-8"?> <root> <!-- MySQL --&g ...
- Hadoop启动报Error: JAVA_HOME is not set and could not be found解决办法
Hadoop安装完后,启动时报Error: JAVA_HOME is not set and could not be found. 解决办法: 修改/etc/hadoop/hadoop-env.sh ...
- 【poj1017】 Packets
http://poj.org/problem?id=1017 (题目链接) 题意 一个工厂制造的产品形状都是长方体盒子,它们的高度都是 h,长和宽都相等,一共有六个型号,分别为1*1, 2*2, 3* ...