转载大佬的blog,很详细,学到了很多东西 奇偶剪枝:根据题目,dog必须在第t秒到达门口.也就是需要走t-1步.设dog开始的位置为(sx,sy),目标位置为(ex,ey).如果abs(ex-x)+abs(ey-y)为偶数,则abs(ex-x)和abs(ey-y)奇偶性相同,所以需要走偶数步: 当abs(ex-x)+abs(ey-y)为奇数时,则abs(ex-x)和abs(ey-y)奇偶性不同,到达目标位置就需要走奇数步.先判断奇偶性再搜索可以节省很多时间,不然的话容易超时.t-sum为到达目…
传送门: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1010 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1110 题目大意: 一只狗陷入了迷宫,他每秒必须走一步,并且不能重复走已经走过的地方,给定一个出口,要求在恰好为T的时间到达,如果可以,输出YES否则NO 不断的TLE,然后搜了下题解. 这题最漂亮的剪枝就在于奇偶的判断上.Orz DFS过程中,设终点的坐标为…
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数为T1,并记其他任意走法所需步数为T2,则T2-T1一定为偶数. 即若某一点到终点的最短步数为T1,且T3-T1为奇数,则一定无法话费T3步恰好到达终点. /*HDU 1010 ------ Tempter of the Bone DFS*/ #include <cstdio> #include…
  如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释: 这种方案学名为“奇偶剪枝”.我们已知了最短的步数就是直角三角形的两条直角边,实际上的路径却不一定非要沿着这两条边走的.仔细看看只要是移动方向一直是右.下,那么走到的时候总步数也一定是path的.然而由于墙的存在或许我们不可能一直右.下的走下去.为了避开墙,我们可能会向左走,向上走等等.但为了到达目的地…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He r…
题目链接: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: #includ…
http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心外,还需要强力的剪枝. 奇偶性剪枝:参考 http://www.cppblog.com/Geek/archive/2010/04/26/113615.html #include <iostream> #include <cstring> #include <cstdio>…
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 125945    Accepted Submission(s): 33969 Problem Description The doggie found a bone in an ancient maze, which fascinated him a…
http://acm.hdu.edu.cn/showproblem.php?pid=1010 题意:就是给出了一个迷宫,小狗必须经过指定的步数到达出口,并且每个格子只能走一次. 首先先来介绍一下奇偶性剪枝: 在这道题目中,如果使用剪枝的话,可以节省不少的时间. 在这道题目中,每次dfs循环时都可以判断一下小狗当前位置与终点所相差的步数,如果不为偶数的话,说明到达不了终点,就可以退出这个循环,不必继续dfs了. 在这道题目中,由于每个格子只能经过一次,所以经过一次后,可以把该点位置改为'X',然后…
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…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. #include <stdio.h> #include <string.h> #define max 1010 char a[max], b[max]; int main() { int i, k, s, c, T, len1, len2; scanf("%d", &T); ; k <…
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake…
题意: 必须在第t秒走到格子D上,S为起点,D为终点,点就是可以走,X就是墙. 思路: 将迷宫外围四面都筑墙‘X’.深度搜索+奇偶剪枝,再加一个剪枝“无法在指定时间内到达”. #include <iostream> #include <vector> #include <string> #include <math.h> using namespace std; vector<string> v; int n,m; int x_1,y_1,x_2…
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 58766    Accepted Submission(s): 15983 Problem Description The doggie found a bone in an ancient maze, which fascinated him a…
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 70032    Accepted Submission(s): 19286 Problem Description The doggie found a bone in an ancient maze, which fascinated him a…
来源:点击打开链接 看上去数据规模很小,但是必须要剪枝,否则直接爆TLE. 通过这个题可以练习奇偶剪枝. 另外:还有一个优化方式,如果所有步数走完了门还没关,则直接返回结果"NO". #include <iostream> #include <cstring> #include <cmath> #include <cstdlib> using namespace std; int n,m,tarstep; int tari,tarj; i…
题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:DFS+剪枝 代码如下: /* * 1010_3.cpp * * Created on: 2013年8月16日 * Author: Administrator */ #include <iostream> using namespace std; bool flag; int N, M, T; in…
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 88774    Accepted Submission(s): 24159 Problem Description The doggie found a bone in an ancient maze, which fascinated him a…
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried despe…
Tempter of the Bone Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 15   Accepted Submission(s) : 9 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description The doggie found a…
<题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要在某一时刻到达,所以需要用到可行性剪枝中的奇偶剪枝,如果在某一点,它所剩的步数与到终点的最短距离之差是偶数,说明这种情况有可能恰好在规定时刻到达终点,否则不可能,将其剪去. #include <bits/stdc++.h> using namespace std; ][]; ][]; int n,…
题目链接 Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 用STL 中的 Map 写的 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <map> using namespace std; map<string, int>…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2066 题目意思:给出T条路,和草儿家相邻的城市编号,以及草儿想去的地方的编号.问从草儿家到达草儿想去的地方的最短时间是多少. 一开始自己写的只能处理单边出发的情况,对于以下这幅图,只能处理箭头所示的方向,不能向下,于是不知道为什么出现了百年难得一遇的Runtime Error(ACCESS_VIOLATION)! 10 2 31 3 53 8 42 5 25 8 31 4 74 9 129 10 2…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不要写成 map[i][j] = map[j][i] = X. #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; #de…
题意:有一副二维地图'S'为起点,'D'为终点,'.'是可以行走的,'X'是不能行走的.问能否只走T步从S走到D? 题解:最容易想到的就是DFS暴力搜索,,但是会超时...=_=... 所以,,要有其他方法适当的剪枝:假设当前所在的位置为(x,y),终点D的位置为(ex,ey); 那么找下规律可以发现: 当 |x-ex|+|y-ey| 为奇数时,那么不管从(x,y)以何种方式走到(ex,ey)都是花费奇数步:当为偶数时同理.  这即是所谓的奇偶性剪枝.这样剪枝就可以复杂度就变为原来暴力DFS的开…
的问题是,在测试修剪. 应该说是更先进的应用. 由于使用的heuristic(经验)修剪.总结这方面的经验法则,别easy.我说,这也是由于先进的在线报告中的应用程序没有分析太多太好的解决这个问题,计划给也很慢,只有失去了.从这个很多人不这样做的问题. 这里我须要更正一下网上流行的说法:奇偶剪枝法. 事实上本题使用奇偶剪枝法并不能太大提快速度,只能说只让使用奇偶剪枝过掉. 所以网上说本题使用奇偶剪枝的,事实上并不能提快速度. 原因: 奇偶剪枝仅仅能剪枝一次,不能在递归的时候剪枝,由于仅仅要初始化…
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 149833    Accepted Submission(s): 39945 Problem Description The doggie found a bone in an ancient maze, which fascinated him a…
题意:从开始位置走到结束位置,恰好走 t 步 YES 否则 NO 搜索题,由于是恰好走到,所以用到了奇偶剪枝 什么是奇偶剪枝,我也是刚知道 所给步数为 t ,起始位置坐标 (begin_x,begin_y), 结束位置坐标 (end_x,end_y) 两位置最短距离为 ju = abs(end_x - begin_x) + abs(end_y - begin_y) 若 t - ju 为奇数,则无论如何不能恰好走到 为偶数才有可能恰好走到 代码中 pos + dis 即 ju 代码如下: #inc…
题意:从S走到D,能不能恰好用T时间. 析:这个题时间是恰好,并不是少于T,所以用DFS来做,然后要剪枝,不然会TEL,我们这样剪枝,假设我们在(x,y),终点是(ex,ey), 那么从(x, y)到(ex, ey),要么时间正好是T-你已经走过的时间,要么要向别的地方先拐一下,以凑出这个正好时间,既然要拐一下,那么一定要回来, 所以时间肯定得是偶数,要不然完不成(回不来), 所以(t - abs(ex-x) - abs(ey-y) - cnt ),如果是奇数就剪枝.然而用C++交就TLE,用G…