
Dfs
stdwal
天演在化,功成在学。知海无涯,见花问道。
展开
-
POJ1979-Red and Black
图的深搜。#include #include const int MAXN = 20;char map[MAXN+2][MAXN+2];int cnt = 0;int w, h;void Dfs(int x, int y){ cnt++; map[x][y] = '#'; if (x - 1 >= 0 && map[x-1][y] == '.原创 2016-03-31 17:02:49 · 293 阅读 · 0 评论 -
Ural1005-Stone Pile
给出n个数,要求将其分成两堆,求两堆数字总和的差值最小。 一开始傻傻的用next_permutation,第二个数据就TLE了…. 其实搜索每个元素,先放在一堆搜索,再回溯放在另一堆搜索,复杂度为2^20.#include <cstdio>#include <cmath>#include <cstdlib>const int INF = 20 * 100000;int a[25];bool原创 2016-11-15 14:02:54 · 435 阅读 · 0 评论 -
Ural1106-Two Teams
二分图染色。#include <cstdio>#include <cstring>#include <cctype>#include <vector>using namespace std;const int maxn = 105;vector<int> G[maxn];vector<int> U;int mark[maxn];void dfs(int v) { if (mark[v原创 2016-11-17 20:28:30 · 420 阅读 · 0 评论 -
Ural2005-Taxi for Programmers
一开始没想到是深搜,以为是最短路,后来用脑洞大开想到了最小费用流… 其实只要dfs一下,如果当前点为终点的时候,判断是否所有点都已被访问过,再比较cost和ans,如果cost比ans小,记录下所有点的前驱点,再路径还原即可。#include <cstdio>#include <cstring>#include <vector>#include <algorithm>bool vis[6];原创 2016-11-17 09:45:27 · 350 阅读 · 0 评论 -
POJ3373-Changing Digits
Dfs+强剪枝。 discuss里有一组很好的数据: 535064 9084 答案为:535956 先从m < n开始搜,再从n > m搜索,并用f数组记录剪枝。#include <cstdio>#include <cstring>char n[105];int k;int m[105];int mod[105][10];int f[105][10000+5]; //f[p原创 2016-10-26 16:27:34 · 320 阅读 · 0 评论 -
UVa11520-Fill the Square
给定一个n*n的方阵要求往里面填充字符使得字典序最小。 暴力搜索,从上到下从左至右选择最小的字符填充即可。#include <cstdio>char grid[12][12];int dx[4] = {-1, 0, 1, 0};int dy[4] = {0, 1, 0, -1};int n;void Dfs(int x, int y) { if (y >= n) { Df原创 2016-09-28 19:36:25 · 320 阅读 · 0 评论 -
POJ1691-Painting A Board
很有趣的搜索题。 将每个矩阵作为一个顶点,矩阵i在矩阵j的上方就从i到j连边,并且记录每个顶点的入度。 Dfs一个矩阵时,将它下方的矩阵入度减一,入度为0时即可以涂色的位置。 特么这题输入的是y和x!#include <cstdio>#include <cstring>struct Rectangle { int lx, ly, rx, ry, color;};Rectangle原创 2016-10-24 20:16:12 · 404 阅读 · 0 评论 -
POJ3411-Paid Roads
中级搜索题。 判断没有重复的回路即可,由于一共10个顶点,最少3个顶点1个回路,设想有一个顶点,图中所有的回路都与它有关,那么这个顶点最多被经过4次,因此搜索时只要记录每个点路过的次数并判断即可。#include <cstdio>const int INF = 12345678;struct Road { int a, b, c, p, r;};Road way[12];int vis原创 2016-10-23 19:12:27 · 319 阅读 · 0 评论 -
POJ2531-Network Saboteur
题目要求将一个图分开成A集合和B集合使得连接两个部分的边权值最大… 暴搜! 将图分成两个集合,搜索每个点在A集合和B集合,回溯。//POJ2531#include <cstdio>#include <algorithm>using namespace std;const int maxn = 20 + 5;int C[maxn][maxn];bool mark[maxn];int n;in原创 2016-09-01 20:44:11 · 347 阅读 · 0 评论 -
HDU1078-FatMouse and Cheese
记忆化搜索即可。#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100 + 10;const int dx[4] = {-1, 0, 1, 0};const int dy[4] = {0, -1, 0, 1};int grid[maxn][maxn];int原创 2016-09-10 22:16:22 · 244 阅读 · 0 评论 -
HDU1010-Tempter of the Bone
这道题考查了深搜和剪枝,首先要恰好在t的时间内到达,从起点(bx, by)到终点(ex, ey)的最短距离为abs(ex-bx)+abs(ey-by),如果t小于最小距离就是NO。 另外t与最短距离之差如果是奇数也一定是NO。#include <cstdio>#include <cstring>#include <cstdlib>char maze[8][8];int dx[4] = {-1原创 2016-08-03 15:14:56 · 212 阅读 · 0 评论 -
POJ1164-The Castle
将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(V){ if (v访问过) { return; } 将v标记为访问过; 对和v相邻的每个点u:Dfs(u);}int main(){ while (在图中能找到未访问过的点k) { Dfs(k); }}Poj原创 2016-03-20 13:13:57 · 621 阅读 · 0 评论 -
Dfs回溯-八皇后问题
八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例。该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。将棋盘的每一行作为搜索树的节点。建立一个数组IsLegal[3][18]分别记录列,主对角线,辅对角线的情况,判段皇后的位置是否合法。主对角线方向上原创 2016-03-23 21:26:50 · 527 阅读 · 0 评论 -
HDUOJ2553-N皇后问题
对于N皇后问题首先的代码#include int IsLegal[3][25];int cnt = 0;int cmd;void Dfs(int n){ if (n == cmd + 1) cnt++; else { for(int i = 1; i <= cmd; i++) { if(IsLegal[0][原创 2016-03-24 21:33:14 · 472 阅读 · 0 评论 -
POJ2386-Lake Counting
#include const int MAXN = 100;char field[MAXN+2][MAXN+2];int n, m;void Dfs(int x, int y){ if (field[x][y] == 'W') { field[x][y] = '.'; for (int dx = -1; dx <= 1; dx原创 2016-03-30 16:47:41 · 294 阅读 · 0 评论 -
POJ3009-Curling 2.0
外星人打冰球的故事。这是一道深搜的题,与一般的题不同之处在于它并不是走一格,而是走一条直线路径。#include #include #include using namespace std;const int MAXN = 20;int map[MAXN+2][MAXN+2];int dx[4] = {1, 0, -1, 0};int dy[4] = {0, 1,原创 2016-03-31 21:22:20 · 484 阅读 · 0 评论 -
POJ3050-Hopscotch
题目除了题意难以理解,总的实现还是简单的。一群牛在进行一个跳房子的游戏。牛可以在任意一点上向前后左右四个方向跳格子,跳五次后得到含有六个数字的数字串。问这样的数字串有多少个。#include #include using namespace std;int grid[6][6];int dx[4] = {-1, 0, 1, 0};int dy[4] = {0, -1,原创 2016-04-04 11:13:56 · 1185 阅读 · 0 评论 -
Ural1044-Lucky Tickets. Easy!
给定一个n,表示n位编号,求n位编号中前半部分和后半部分之后相等的编号个数。 网上的题解大都只写出最后答案而不指出过程,而我用的是暴搜预处理,按位从0到9赋值,搜索出个数。#include <cstdio>#include <cmath>int a[10];int c[10];void dfs(int x, int n) { if (x == n + 1) { int原创 2016-11-15 17:51:13 · 394 阅读 · 0 评论