
dfs
文章平均质量分 74
Non_Cease
这个作者很懒,什么都没留下…
展开
-
poj2488 骑士周游(深搜)
题意:给出p*q大小的棋盘,要求不重复的让骑士遍历整个棋盘,并以字典序输出经过的棋盘的位置。以数字为行(横坐标),字母为列(纵坐标),输出时是先输出行再输出列(即先输出横坐标在输 出纵坐标)。 因为要求字典序输出,只需方向数组按一定的顺序排列进行遍历,则最后骑士经过棋盘的原创 2011-09-15 12:49:32 · 835 阅读 · 0 评论 -
poj1416 dfs 碎纸机
题意:将一个数字m(不超过六位)剪成几段,要求得到的新的几个数之和不超过另外一个给定的数n,求可以得到的最接近n的数,具体输出看题。记录路径输出。#include #include #include #include using namespace std;#define M 6int d[M], tar, val, len, num[M][M];bool flag;原创 2012-03-06 22:26:10 · 2153 阅读 · 0 评论 -
poj2676 Sudoku 数独
记录所有空位置,三个数组(row[M][M], col[M][M], mat[3][3][M])hash判断当前空位置是否可以填某个数,然后直接DFS,注意从后往前搜索,时间比正向搜快很多。#include #include #include using namespace std;#define M 11struct point { int x, y; } p[100];原创 2012-03-02 20:52:18 · 1565 阅读 · 0 评论 -
poj2531 暴搜
给定无向图,将顶点分成两个集合,使得两个集合间的所有边权最大。#include #include #include using namespace std;int c[22][22], n, ans, set[22]; //最初所有节点都默认在集合0void DFS(int id, int sum){ int i, t; set[id] = 1;原创 2012-03-02 21:04:16 · 1233 阅读 · 0 评论 -
poj1129 dfs
题意:给定无向图,对点着色,使得相邻的结点颜色不同。直接搜索,没有用四色原理,用四色原理也是暴力,没有什么影响。#include #include #include using namespace std;#define M 26int n, ans, color[M]; bool map[M][M], isFind;bool ok(int x, int c) {原创 2012-03-02 20:43:07 · 3297 阅读 · 2 评论 -
poj1321 dfs
题意:略记录棋盘上能放棋子的位置用 col 和 row 数组记录某行某列是否已经有棋子了然后简单的深搜,但是剪枝不知道怎么剪,status好多0ms 我的代码是32ms#include #include #include using namespace std;struct point { int x, y; } pie[70];int n, k, am;char原创 2012-02-26 15:17:43 · 540 阅读 · 0 评论 -
poj3083 dfs + bfs
给定迷宫,深搜求向左搜索和向右搜索走出迷宫需要的步数,广搜求走出迷宫最少的步数。注意方向数组,还有横纵坐标。#include #include #include #include using namespace std;int w, h, dist[45][45];char maze[45][45];struct point { int x, y; } s;int dir原创 2012-02-25 15:10:00 · 697 阅读 · 0 评论 -
poj3009 dfs + 剪枝
题意:在矩形冰面进行滑石头游戏,只能上下左右滑,冰面上散步很多障碍,给定石头初始位置和需要到达的终点以及障碍的分布。 若石头与障碍物相邻,则石头不能向这个方向滑动; 若石头滑动起来后,便会一直沿这个方向滑动,若出界,则游戏失败,若碰到障碍物,则石头停下,障碍物消失, 若石头到达终点,则游戏结束。注意石头滑动次数不能超过10次,这是很重要的剪枝#inc原创 2012-02-26 11:22:44 · 924 阅读 · 0 评论 -
poj1011 sticks
我的第一道搜索剪枝的题目。题意:有一些 木棍,它们的长度相同。现在将木棍折断,最多折成64根。编程求木棍可能的最小长度。#include #include using namespace std;#define N 70int stick[N], n;bool used[N];bool compare(const int &a, const int &b){ re原创 2011-11-04 22:48:53 · 588 阅读 · 0 评论 -
poj1190 生日蛋糕 (搜索剪枝)
网上很多解题报告,基本一个样,我就不画蛇添足了。推一下第三个剪枝:如题, n为总体积, sumv为从下向上前k-1层的总体积,h为高,r为半径,ans为已经获得的最优的方案易得:n - sumv = h[ k ] * ( r[ k ] ^ 2 ) + h[ k + 1 ] * ( r[ k + 1 ] ^ 2 )…… + h[ m ] * ( r[ m ] ^ 2 )原创 2011-11-04 23:13:25 · 4187 阅读 · 0 评论 -
poj2255 根据二叉树的前序和中序遍历 求出树的后序遍历
http://poj.org/problem?id=2255前序遍历的第一个字符为树的根节点;找到根节点字符在中序遍历中的位置,则该位置的左边的字符串为左子树的中序遍历串,右边为右子树中序遍历串;根节点在中序遍历首位,无左子树根节点在中序遍历末位,无右子树#include #include #include using namespace std;const int原创 2013-02-28 19:51:57 · 2905 阅读 · 0 评论