随笔分类 - 搜索
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 6 int ascore, bscore; 7 8 int DFS(int m, int n, int p) 9 {10 if(ascore == 1) return 0; 11 if(m==1 && n==1)12 {13 bscore = 1;14 ascore = 1;15 return 0; 16 } ...
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 6 char a[5002],b[4]="NOP"; 7 8 int ok(int l) 9 {10 int i, j;11 for(i = 0; i < (l+1)/2; i++) 12 {13 for(j = 0; j <= i; j++) 14 {15 if(a[l-i+j] != a[l-2*i+j-1])16 ...
阅读全文
摘要:咋一看此题有点不知所措,后来看了别人的题解,感悟良多用你自己的四轮现在草稿纸上画画,边入栈边匹配,匹配则出栈,不匹配则入栈,如此下去。。。View Code 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<stack> 6 #include<iostream> 7 #include<string> 8 9 using namespace std;10 11 int i,j,k,ans;
阅读全文
摘要:刚看到这题时本想着拿它来练习下DFS来着可是被坑了一下午让我与苦无泪,回来检查代码时,发现自己吧0这个特殊的数忘记考虑了,T_T。。。。思路:用DFS搜索出所有可能的情况然后打表输出。View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define N 1000005 5 6 int fac[11] = {0,1,1,2,6,24,120,720,5040,40320,362880};//这里数组要注意。。fac[0] = 0,fac[1]代表了0的阶乘,fac
阅读全文
摘要:题目叙述的有问题,咒语应该是以b开头m结尾的,一开始看题意就是看不懂,搜了别人的代码,才发现题意的坑了,在网上看到了了别的解法貌似构图Floyd算法,不过只是要拿此题练习DFS仅此而已。思路:先找到以b开头的单词在开始搜索,寻找首位相连的单词,输出的时候容易错,有点坑爹。View Code 1 #include<cstdio> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #define N 1001 6 7 using namespace std; 8 9 stru
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 以前用BFS写过,这次改用DFS写发现DFS掌握的不好,挑了好久才过,今下午才AC了两道题,还要加紧练习DFS! 5 int row,col,count,vis[21][21]; 6 char map[21][21]; 7 int dx[] = {0,0,-1,1}; 8 int dy[] = {1,-1,0,0}; 9 void DFS(int x,int y)10 {11 int sx,sy,i;12 vis
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 从第一种开始搜索,搜索完毕后回溯,搜索下一个 4 int n,m,count,sum,val[11],num[11]; 5 void DFS(int x) 6 { 7 int i; 8 if(sum == n) 9 {10 count++; 11 return ;12 }13 if(sum > n || x > m) 14 return ; 15 for(i=1; i<=num[x]...
阅读全文
摘要:思路很简单,一开始没有用优先队列,WA了几次后来改了,想一想走迷宫停顿时应该用优先队列。if(u.btime==1) continue;此句不能省略当时间为1时,这一步无法出去,只有重新来过View Code 1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #include<cstring> 5 #include<queue> 6 7 using namespace std; 8 int N,M,map[10][10]; 9 int dx[] = {0,0,-
阅读全文
摘要:思路很简单,给你n个数和一个sum要求从n个数中选出所有加和为sum的数值串:肯定是先用DFS找出所有的情况然后再删掉重复的;这里我从课件里学到一种判断重复的方法,把核心代码附上:void findSum (int sum, int iEnd, int numSkipped) { if (iEnd == listlength) return; int newsum = sum - list[iEnd]; //取第iEnd个数 if ((numSkipped != list[iEnd])&&(newsum >= 0)) { used[iEnd] = 1; //标记第iEnd
阅读全文
摘要:求周长,一开不知道怎么求,最后钻到POJ讨论区内,看到一种方法,就是‘X"周围全换成0,最后只要求0的个数就好了嗯,这种方法真好,反正我们想起来,膜拜啊!这题过了从网上搜了下发现可以用BFS做,想想也是,本身用那一种都行View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 #include<ctype.h> 6 7 int map[25][25],f[25][25],visit[25][25],
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<math.h> 5 #include<ctype.h> 6 7 int len; 8 long n,num; 9 char s[15]; 10 void DFS(int t,long ans)11 {12 int i; 13 if(t == len)14 { 15 if(ans == n) 16 num++; 17 return ; 18 }...
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 #include<ctype.h> 6 7 char map[101][101]; 8 int visit[101][101],m,n; 9 int dx[] = {0,1,-1, 0,1, 1,-1,-1}; 10 int dy[] = {1,0, 0,-1,1,-1,-1, 1};11 12 void DFS(int x,int y)13 {14
阅读全文
摘要:View Code 1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #include<queue> 5 #include<malloc.h> 6 #include<cstring> 7 #define N 100001 8 9 using namespace std;10 11 typedef struct city12 {13 int v;14 struct city *next;15 }City;16 17 City c[N];18 int n
阅读全文
摘要:解题思路:1 如果图中所有的点连通且度都为偶数则可以一笔画成。 2 如果图中有不超过2个点的度为奇数则可以一笔画。 3做法显然先通DFS判断图是否连通过,然后在判断图中奇数点度的个数即可。 View Code 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #define N 1010 6 7 using namespace std; 8 9 int G[N][N], vis[N], num[N];10 int P, Q, ok;1
阅读全文
摘要:View Code 1 #include<cstdio> 2 #include<cstdlib> 3 #include<queue> 4 #include<iostream> 5 #include<cstring> 6 7 using namespace std; 8 9 typedef struct 10 { 11 int x, y; 12 int step; 13 }Point; 14 15 priority_queue<Point> q; 16 bool operator<(Point a, Point b)
阅读全文
摘要:题目的本质就是叫你求图中连通分支的数目。 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 5 int dx[4] = {1,-1, 0, 0}; 6 int dy[4] = {0, 0,-1, 1}; 7 int map[101][101]; 8 int visit[101][101],num,col,row; 9 10 void DFS(int x,int y)//求出图中所有的连通分支简单DFS遍历11 {12 int i, j, x1, y1;13 if(visit[x][
阅读全文

浙公网安备 33010602011771号