随笔分类 - 图论--dfs
摘要:http://poj.org/problem?id=3083纠结了好久 被它的转向搞晕 了WA两次 加一else AC。。设置四个方向 初始化方向自己算出来 初始化在数组中 dfs+bfs左 就是当前方向向左 左走不动就逆时针走下一个方向 依次类推 右同样的方式View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 char s[50][50],so[2]; 6 int pr[7],dis[4][2]={-1,0,0,-1,
阅读全文
摘要:View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 int board[110][110]; 6 int di,dj,xi,xj,n,m,num,dis[4][2] = {0,1,0,-1,1,0,-1,0}; 7 void dfs(int v,int x,int y) 8 { 9 int i,tx,ty,flag = 0;10 if(v>10)11 return ;12 for(i = 0 ; i < 4
阅读全文
摘要:12 13打表而过View Code 1 #include <iostream> 2 /* 3 ID: your_id_here 4 PROG: checker 5 LANG: C++ 6 */ 7 #include<cstdio> 8 #include<cstring> 9 #include<cmath>10 using namespace std;11 int f[20][20],num[20],n,t;12 void dfs(int v,int x)13 {14 int j,g,i,ff = 0;15 if(v<=n)16 num[v
阅读全文
摘要:预处理出前4位的超级素数,再枚举后四位View Code 1 /* 2 ID: your_id_here 3 PROG: sprime 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include<cstdio> 8 #include<cstring> 9 #include<cmath>10 #include<algorithm>11 using namespace std;12 int num[10][1000],f[20],g,n,k[10],nn[10000],kk;13 int prime
阅读全文
摘要:dfs搜前5位数 根据前面的算后面的View Code 1 /* 2 ID: your_id_here 3 PROG: pprime 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include<cstdio> 8 #include<cstring> 9 #include<cmath> 10 #include<algorithm> 11 using namespace std; 12 int a,b,g; 13 int kk[10000],f[20][20],tt,ff[100000]; 14 c
阅读全文
摘要:刚开始理解错题意了,写了半天的dfs,直接delete。写的挺多,不是太复杂,对于每种状态枚举6种情况。View Code 1 /* 2 ID: your_id_here 3 PROG: milk3 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include<cstdio> 8 #include<cstring> 9 #include<algorithm> 10 using namespace std; 11 int a,b,c,num[50],f[30][30][30],ff[50],g; 12 void
阅读全文
摘要:http://poj.org/problem?id=1062poj计划里最短路题 用DFS做的。。弄了一下午 终于搜过了 注意 全局的最大跟最小值的差不能超过它所规定的限制View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 using namespace std; 5 int m,mi,xf[111],flag,minn,maxx; 6 struct node 7 { 8 int p,l,x; 9 int t[101],v[101];10 }q[101];11 v
阅读全文
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=598这题题意看了老半天 让求图形中的X的区域块数 每个区域有多少个X是不相连的 DFS搜有多少个区域 BFS标记 有每个区域里多少X区域块注意两点吧 WA了5次 第一个可能为X 就直接BFS 再DFS第二 BFS中遇到的*要保留起来 把相连的X标记完之后 继续搜 不然区域块数会被分开View Code 1 #include <iostream> 2 #include&l
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1181一次AC 没什么细节要注意 就是输入正确 加上dfs 可以 了View Code 1 #include<stdio.h> 2 #include<string.h> 3 typedef struct node 4 { 5 char c[101]; 6 int f; 7 }st; 8 st q[1001]; 9 int flag;10 void dfs(int x,int n)11 {12 int i,j;13 if(flag)14 return ;15 ...
阅读全文
摘要:参考着cz的写的判重问题 :保存每一层的找过的节点 再在这一层找的时候 要保证与之前没有相同的View Code 1 #include<stdio.h> 2 #include<string.h> 3 long count,f[11],s,x[11],a[11]; 4 void dfs(long v) 5 { 6 long j,flag = 0,g,w,q[11]; 7 if(v>9) 8 { 9 if(x[1]+x[2]+x[3]==x[4]+x[5]+x[6]&&x[4]+x[5]+x[6]==x[7]+x[8]+x[9]&&x[
阅读全文
摘要:http://poj.org/problem?id=1321由于忘记取消起点那里的标记 WA了一次 有点类似皇后View Code 1 #include <stdio.h> 2 #include<string.h> 3 int n, m,count,x[11],y[11]; 4 char c[10][10]; 5 void dfs(int i, int j, int v) 6 { 7 int p,q; 8 if(v == m) 9 count++;10 else11 {12 for(p = i+1 ; p <= n ; p++)13 ...
阅读全文
摘要:这题主要是重复问题怎么删除 由于是排好序的 有重复的 肯定是相邻的在同一层上的结点值 保证不相同就行 不同层可以相同 比如 4 = 2+1+1 可以有两个1具体看代码View Code 1 #include <stdio.h> 2 #include<string.h> 3 int a[1001],n,s,v,y,flag,num[1001],ch; 4 void dfs(int x, int i,int v) 5 { 6 int j,k; 7 y+= x; 8 num[v] = x; 9 if(y == s)10 {11 for...
阅读全文
摘要:http://poj.org/problem?id=2488小错误不断 数组开的a[8][2] 我居然循环到8 还一直纠结哪错了改完后 交了一次WA 看了看讨论里面 要根据字典序 所以移动坐标差a[8][2]只能那么定义 具体看代码View Code 1 #include <stdio.h> 2 #include <string.h> 3 int n,m,x[27][27],q[27],flag; 4 char w[27]; 5 int u[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1016这几天做题第一次一次性AC 自信心上涨简单dfs一个大圈 中有n个小圈 填进去数 相邻圈中数的和为素数View Code 1 #include <stdio.h> 2 #include<string.h> 3 int x[21][21],v,f,y[21],c[21],n; 4 int prime(int x) 5 { 6 int i,flag = 1; 7 for(i = 2; i < x ; i++) 8 if(x%i == 0) 9 {10 ...
阅读全文
摘要:http://poj.org/problem?id=2258dfs 边是双向的 节点可以走多次 开个二维数组标记边 因为可以从任一节点出发所以n次dfsView Code 1 #include <stdio.h> 2 #include<string.h> 3 int x[26][26],v,w,n; 4 void dfs(int i,int v) 5 { 6 int j; 7 for(j = 0 ; j <= n-1 ; j++) 8 { 9 if(x[i][j] == 1)10 {11 x[i][j] = 0;...
阅读全文
摘要:刚开始把路径标记给取消了就一直TLEView Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<string.h> 4 int k[1001][1001],f[1001],n,m,a,b,flag,x; 5 void dfs(int x) 6 { 7 int j; 8 f[x] = 1; 9 if(flag == 1)10 return ;11 for(j = n ;j>=1 ; j-- )12 if(k[x][j] == 1&&f[j] == 0)13 ...
阅读全文
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1882搜索 递归回溯View Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<math.h> 4 int q[21][21],flag,a[21],f = 0; 5 void dfs(int i, int n) 6 { 7 int j,k,p,l,m; 8 if(f == 1) 9 return ;10 if(i>n)11 {12 .
阅读全文
摘要:原文地址http://princetonboy.ycool.com/post.2805302.html【摘要】本文讨论了搜索算法中“剪枝”这一常见的优化技巧.首先由回溯法解决迷宫问题展开论述,介绍了什么是剪枝;而后分析剪枝的三个原则正确、准确、高效,并分别就剪枝的两种思路:可行性剪枝及最优性剪枝,结合例题作进一步的阐述;最后对剪枝优化方法进行了一些总结.【关键字】搜索、优化、剪枝、时间复杂度引论在竞赛中,我们有时会碰到一些题目,它们既不能通过建立数学模型解决,又没有现成算法可以套用,或者非遍历所有状况才可以得出正确结果.这时,我们就必须采用搜索算法来解决问题.搜索算法按搜索的方式分有两类,一类
阅读全文
摘要:原本因为简单的dfs就可以 写完却发现不知道如何保留一条路的路径 不能保证一条路中的节点只被走一次 导致死循环还有各种剪枝代码算是跟别人一个模子刻出来的 递归 一直走那一条路 走过就标记上 回溯的时候再取消标记View Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<string.h> 4 #include<math.h> 5 int k,d,num,flag,n,m,t,a[6]; 6 char c[10][10]; 7 void dfs(int x, int y) 8 {
阅读全文

浙公网安备 33010602011771号