书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

随笔分类 -  C DFS

DFS解决任意组合原来这样简单
摘要:HDU 2660http://acm.hdu.edu.cn/showproblem.php?pid=2660题意就不说了,直接走代码。View Code #include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;#define max(a, b) (a>b?a:b)int V[21], W[21], Ans, N, K, L, i;void DFS(int p, int vv, 阅读全文

posted @ 2012-03-28 18:45 More study needed. 阅读(203) 评论(0) 推荐(0) 编辑

回溯路径的记录
摘要:HDU 1016http://acm.hdu.edu.cn/showproblem.php?pid=1016题目大意:给定一个数N,从1到N的这些整数构成一个环,它的目的就是让你找出第相邻两个数都是素数的环。而且是所有的环。View Code #include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;int N;bool vis[21];int pre[21];bool prime[ 阅读全文

posted @ 2012-03-28 15:59 More study needed. 阅读(219) 评论(0) 推荐(0) 编辑

奇偶剪枝
摘要:这个奇偶剪枝相当牛B呀,佩服。HDU 1010http://acm.hdu.edu.cn/showproblem.php?pid=1010题目大意:就是让你来找一下,能否在限定的时间内从S到达D.Sample Input:4 4 5S.X...X...XD....Sample Output:NOView Code #include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>usi 阅读全文

posted @ 2012-03-27 23:33 More study needed. 阅读(205) 评论(0) 推荐(0) 编辑

再次说搜索
摘要:这次说的又有点新鲜的东西。那就是回溯了。本题是一个经典回溯例题,注意体会一下OK,那么我们说说他有什么用呢?其实很简单了,它的作用就是控制DFS的搜索流程。在本题简单一点来说,就是它能使得DFS符合题意。直接看例子吧。题目:HDU 1045 http://acm.hdu.edu.cn/showproblem.php?pid=1045题意:这个是一个八皇后的变种。不算难。Sample Input:4 .X.. .... XX.. .... Sample Output:5View Code #include<iostream>#include<cstring>#includ 阅读全文

posted @ 2012-03-26 22:39 More study needed. 阅读(207) 评论(0) 推荐(0) 编辑

又来说搜索
摘要:如果我们不好确定搜索的结束条件,那么我们可以假设一下他的搜索深度。将这个深度取到极限就可以了。也就是说,如果当小于这个深度有解了,那么停止搜索,得到答案;如果等于这个深度了,还没有解,那么我们就认为此种情况下无解,也停止搜索,也算是得到了答案,即无解。下面来看一个很简单的例子吧。题目:http://acm.swust.edu.cn/oj/problem/0823/View Code #include "iostream"#include "cstdio"#include "cstring"#include "string&q 阅读全文

posted @ 2012-03-23 22:08 More study needed. 阅读(171) 评论(0) 推荐(0) 编辑

DFS与最大匹配
摘要:其实就是个DFS。题目:http://acm.swust.edu.cn/oj/problem/0827/View Code #include "iostream"#include "string"#include "cstring"#include "algorithm"using namespace std;#define maxn 105#define INF 0xFFFFFFFint map[maxn][maxn], mid, k, N, M;int Link[maxn]; //记录行的状态int used[ 阅读全文

posted @ 2012-03-22 19:28 More study needed. 阅读(208) 评论(0) 推荐(0) 编辑

DFS遍历整个连通的区域
摘要:DFS在搜索的过程中,可以搜索一大片的连通的区域。题目:http://acm.swust.edu.cn/oj/problem/1/View Code #include<iostream>#include<string.h>using namespace std;int direction[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};int used[1005][85];char map[1005][85];int W, H, i, j, count, Max, tx, ty;void dfs(int i, int j){ int k; cou 阅读全文

posted @ 2011-12-01 12:11 More study needed. 阅读(305) 评论(0) 推荐(0) 编辑

DFS解决任意组合问题
摘要:描述在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码。 这些灯都连接到四个按钮:按钮1:当按下此按钮,将改变所有的灯:本来亮着的灯就熄灭,本来是关着的灯被点亮。 按钮2:当按下此按钮,将改变所有奇数号的灯。按钮3:当按下此按钮,将改变所有偶数号的灯。按钮4:当按下此按钮,将改变所有序号是3*K+1(K>=0)的灯。例如:1,4,7...一个计数器C记录按钮被按下的次数。当宴会开始,所有的灯都亮着,此时计数器C为0。你将得到计数器C(0<=C<=10000)上的数值和经过若干操作后某些灯的状态。写一个程序去找出所有灯最后 阅读全文

posted @ 2011-10-24 23:02 More study needed. 阅读(389) 评论(0) 推荐(0) 编辑

加上减去DFS解决USACO Healthy Holsteins
摘要:DescriptionFarmer John prides himself on having the healthiest dairy cows in the world. He knows the vitamin content for one scoop of each feed type and the minimum daily vitamin requirement for the cows. Help Farmer John feed his cows so they stay healthy while minimizing the number of scoops that 阅读全文

posted @ 2011-10-23 14:47 More study needed. 阅读(417) 评论(0) 推荐(0) 编辑

DFS解决USACO——Mother's Milk
摘要:DescriptionFarmer John has three milking buckets of capacity A, B, and C liters. Each of the numbers A, B, and C is an integer from 1 through 20, inclusive. Initially, buckets A and B are empty while bucket C is full of milk. Sometimes, FJ pours milk from one bucket to another until the second bucke 阅读全文

posted @ 2011-10-16 18:45 More study needed. 阅读(751) 评论(0) 推荐(0) 编辑

DFS解决POJ 1979
摘要:DescriptionThere is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles. Write a program to count the nu 阅读全文

posted @ 2011-09-23 22:38 More study needed. 阅读(146) 评论(0) 推荐(0) 编辑

DFS解决POJ 2386
摘要:DescriptionDue to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure 阅读全文

posted @ 2011-09-23 22:35 More study needed. 阅读(151) 评论(0) 推荐(0) 编辑

BFS解决POJ 2251
摘要:DescriptionYou are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid 阅读全文

posted @ 2011-09-23 22:34 More study needed. 阅读(182) 评论(0) 推荐(0) 编辑

更深层次的DFS 解决POJ 2362
摘要:DescriptionGiven a set of sticks of various lengths, is it possible to join them end-to-end to form a square?InputThe first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of 阅读全文

posted @ 2011-09-07 08:35 More study needed. 阅读(244) 评论(0) 推荐(0) 编辑

记忆化搜索解决POJ 1088
摘要:DescriptionMichael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子 1 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长... 阅读全文

posted @ 2011-09-05 08:01 More study needed. 阅读(1606) 评论(0) 推荐(0) 编辑

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

点击右上角即可分享
微信分享提示