HDOJ1010 Tempter of the Bone
来源:互联网 发布:C语言如何创建线程同步 编辑:程序博客网 时间:2024/06/08 18:40
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
非常经典的深搜题目,这里需要用到奇偶剪枝。
奇偶剪枝介绍的链接:http://blog.csdn.net/pengrui18/article/details/68073213
不多说,贴代码:
#include <iostream>#include <cmath>#include <string.h>#include <algorithm>#include <stdio.h>using namespace std;int n,m,time;int sx,sy,ex,ey;char map[10][10];bool save;void dfs(int x,int y,int t){ if (save) return; if (x<1 || x>n || y<1 || y>m) return; if (t<=0 && (x!=ex || y!=ey)) return; int tmp=t-abs(x-ex)-abs(y-ey); //奇偶剪枝 if (tmp<0 || tmp&1) return; if (x==ex && y==ey && t==0) { save=true; return; } //结束条件和边界条件写最前面 map[x][y]='X'; //作标记,表示已走过 if (map[x+1][y]!='X') { dfs(x+1,y,t-1); if (save) return; } if (map[x-1][y]!='X') { dfs(x-1,y,t-1); if (save) return; } if (map[x][y+1]!='X') { dfs(x,y+1,t-1); if (save) return; } if (map[x][y-1]!='X') { dfs(x,y-1,t-1); if (save) return; } map[x][y]='.'; //恢复现场}int main(){ while (cin>>n>>m>>time){ if (n==0 && m==0 && time==0) break; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++) { cin>>map[i][j]; if (map[i][j]=='S'){ sx=i; sy=j; } if (map[i][j]=='D'){ ex=i; ey=j; } } } save=false; //表示还没拯救成功 dfs(sx,sy,time); if (!save) printf("NO\n"); else printf("YES\n"); } return 0;}
阅读全文
0 0
- HDOJ1010.Tempter of the Bone
- HDOJ1010 Tempter of the Bone
- hdoj1010-【Tempter of the Bone】
- HDOJ1010 Tempter of the Bone
- HDOJ1010 Tempter of the Bone(DFS)
- HDOJ1010 Tempter of the Bone(DFS)
- hdoj1010 Tempter of the Bone(深度优先搜索)
- HDOJ1010 Tempter of the Bone(DFS,奇偶剪枝)
- HDOJ1010 Tempter of the Bone(DFS+奇偶剪枝)
- hdoj1010 Tempter of the Bone(迷宫问题dfs,奇偶剪枝)
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- Tempter of the Bone
- maven项目报maven包错误
- room的使用-以demo为例
- 凄惨
- Dubbo注册中心安装教程(单节点)
- 夜间模式切换
- HDOJ1010 Tempter of the Bone
- 五子棋之Qt实现
- 5.validate
- HDOJ5047-Sawtooth
- windows2008创建域控
- BaseActivity中默认添加ToolBar
- javascript写滑动图片
- leetcode--Majority Element
- 【Unity3D技术文档翻译】第1.0篇 AssetBundles