hdu - 1010 dfs + 奇偶剪枝
problem - 1010
找有多少条路可以到达目的地。如果能到达的话必须在t时刻。先统计有多少个不能走的点,如果n*m-wall,(wall表示’X’点数量 )<= k 的话,一定无法在t时刻到s点。接下来就考虑如何剪枝了。如果当前点有一条路可以在t时刻到达的话,那么这条路花时间的奇偶性和当前剩余时间的奇偶性是相同的。而当前这条路的奇偶性与这个点到终点的最短路径相同。所以只需要把当前点与终点的曼哈顿距离求出与当前剩余时间相减。一定得是偶数,如果是奇数就return。因为奇数-奇数=偶数。偶数-偶数=偶数。
#include<iostream>
#include<cstdio>
#include<cstring>using namespace std;
int n,m,k;
int x,y;
int l,r;
char s[10][10];
int st[10][10];
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
int flag ;void dfs(int xx , int yy , int t)
{if(s[xx][yy] == 'D' && t == k){cout<<"YES"<<endl;flag = 1;return;}int temp;temp = (k - t) - abs(l-xx) - abs(r-yy);if(temp < 0 || temp&1) return;for(int i = 0 ; i < 4 ; i++){int xi = xx+dx[i];int yi = yy+dy[i]; if(s[xi][yi] != 'X' && xi <= n && xi >= 1 && yi <= m && yi >= 1 && !st[xi][yi]){ st[xi][yi] = 1;dfs(xi,yi,t+1);if(flag == 1) return;st[xi][yi] = 0;} }}int main()
{ios :: sync_with_stdio(false);cin.tie(0),cout.tie(0);scanf("%d%d%d",&n,&m,&k);while(n != 0){int wall = 0;memset(st,0,sizeof st);flag = 0;for(int i = 1 ; i <= n ; i++){for(int j = 1 ; j <= m ; j++){cin>>s[i][j];if(s[i][j] == 'S') {st[i][j] = 1;x = i , y = j;}if(s[i][j] == 'D') l = i , r = j;if(s[i][j] == 'X') wall++;} }int mint = abs(l-x) + abs(r-y);if(mint > k || (mint-k)&1 || m * n - wall <= k){cout<<"NO"<<endl;}else{dfs(x,y,0);if(!flag) cout<<"NO"<<endl;}scanf("%d%d%d",&n,&m,&k);}return 0;}
hdu - 1010 dfs + 奇偶剪枝相关推荐
- Tempter of the Bone HDU - 1010(DFS + 奇偶剪枝)
题目 这个题思路不难,就是不用奇偶剪枝的话会出现TLE,奇偶剪枝说白了就是一个矩阵分成两种区域,从一种区域走到另一个区域必是奇数步数,从一种区域走回自己的区域必是偶数步数,就算出现了障碍物这个结论依旧 ...
- zoj(2110)Tempter of the Bone(DFS+奇偶剪枝)
剪枝很重要,可走的格数小于时间则减去,然后就是奇偶性剪枝 可以把map看成这样: 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 ...
- Tempter of the Bone(DFS + 奇偶剪枝,好题)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU1010 Tempter of the Bone(DFS奇偶剪枝)
传送门 Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. Howe ...
- HDU 1010 深搜+奇偶剪枝
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1010 贴个资料: http://acm.hdu.edu.cn/forum/read.php?tid=615 ...
- 回溯法+奇偶剪枝——Hdu 1010 Tempter of the Bone
1) 题目 Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 1010 Tempter of the Bone heuristic 剪枝法
本题就是考剪枝法了. 应该说是比较高级的应用了.因为要使用heuristic(经验)剪枝法.要总结出这个经验规律来,不容易.我说这是高级的应用也因为网上太多解题报告都没有分析好这题,给出的程序也很慢, ...
- HDOJ 1010 HDU 1010 Tempter of the Bone ACM 1010 IN HDU
MiYu原创, 转帖请注明 : 转载自 ______________白白の屋 题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述: 代码 ...
- HDU.1010 Tempter of the Bone
文章目录 一.题目解读 1.原题 2.分类 3.题意 4.输入输出格式 5.数据范围 二.题解参考 1.总体思路 2.思路① (1).分析 (2).AC代码 三.评价与后话 1.评价 2.奇偶剪枝0- ...
最新文章
- (最新最全)windows使用anaconda安装pytorch进行深度学习并使用GPU加速
- 如何在ant里import
- web前端的主要学习什么,2020年还有前途吗?一般工资是多少?
- spring 第一篇(1-3):鸟瞰spring蓝图
- 图像灰度化的三种方法(matlab、C++、Python实现)
- BAT机器学习面试1000题系列(第1~305题)
- 基于CameraLink的串行口通讯
- 自学编程的难度如何?
- 华为云学院-人人学loT学习笔记及扩展- 第一章 初识物联网
- 像素、英寸、厘米之间的换算关系
- Android App 启动时显示正在加载图片(讲解+源码)
- ABYY OCR 文字识别软件 V15. 安装教程
- Python的一点人生经验
- 看了《麦田里的守望者》
- if (Build.VERSION.SDK_INT = Build.VERSION_CODES.KITKAT)设置后的效果
- 乐动机器人 2D DTOF激光雷达 LD06、LD19驱动包开源仓库分享
- 弘辽科技:淘宝提升展现词好吗?淘宝展现词如何提升?
- Linux中的vim最小集、指令集及其配置
- jquery延时执行
- 人工智能工程师的三个层次