随笔分类 - DFS 与 BFS
摘要:题意:就是货币兑换;问你经过几次兑换时候比原来的钱数是否多;N -货币种类, M - 兑换的方式, S -Nick拥有的币种 ,V - Nick该种币种的数值数;注意:货币的兑换可以循环使用多次;View Code #include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>using namespace std;class Node{public: int num; double rate,com,sum; Node *ne
阅读全文
摘要:题意:给出一个长度为N(N <= 100000)的数列,然后是两种操作:U A B: 将第A个数替换为B (下标从零开始)Q A B: 输出区间[A, B]的最长连续递增子序列;这里注意:操作数可为100000;如果单纯的用暴力的方法可定超时,这里只是区间的操作,那么我们可以用线段数来做;我们知道在一个区间如果左区间的右值比右区间的左值小,那么这两个区间可以合并成一个区间,在与左区间的最大值和右区间的最大值做比较,找出最大值;如果小于那么我们就直接可以把左区间的最大值和右区间的最大值做比较,找出最大值;这里就要设立几个变量:l_value 区间的最大连续递增序列的左值;r_value区间
阅读全文
摘要:题意:在一个8×8的棋盘中,给定你4个棋子A,再给你4个棋子B,问你在8步之内能不能够从A位置移动到B位置;规则:棋子能能上下左右移动,同时能跳过相邻的棋子到达相邻棋子的空地方;这个题要用双向搜索;同时我用一个8维的数组hash来构造一个图;当hash标记为1时表示A到过该点,标记为2时表示B到过该点;这里要注意就是A,B同时搜索时他们分别最多只能走4步,如果多于4步,那么他们步数之和就可能大于8;这里还要用到排序,这个很重要,因为4个点是不分形状大小的,那么我们就不会重复走;View Code #include<iostream>#include<queue>
阅读全文
摘要:这是一个双向搜索题,我用BFS把所有的点都搜索到;再进行两次搜索的比较;#include<cstdio>#include<cstring>#include<iostream>using namespace std;class Node{public: int x , y ; int step; int num; };Node queue[80024];int step[2][204][204],N,M;int d[4][2] = { 0,1,1,0,-1,0,0,-1 };char map[204][204];int BFS( int x1,int y1,
阅读全文
摘要:这题是记忆化搜索:#include<iostream>#include<cstdio>#include<cstring>using namespace std;int map[124][124];int ans[124][124];int N,M;int DFS( int x ,int y ){ int sum = 0; if( x == N && y == M ) return 1; if( ans[x][y]>=0 ) return ans[x][y]; for( int i=0; i<= map[x][y] ; i++ )
阅读全文
摘要:刚开始用暴搜一直RE了,后来加了一个标记就是把字符串转化成数字,如果出现过就标记它;有一组测试数据就是1111与5555是16;#include<iostream>#include<cstdio>#include<cstring>using namespace std;class Node{public: int step; char c[6]; Node() { memset( c , 0, sizeof( c ) ); } };char num1[6],num2[6];Node queue[10024];bool hash[10000];bool pus
阅读全文
摘要:简单的搜索题:#include<iostream>#include<cstdio>#include<cstring>using namespace std;int flag,Count,step;char map[15][15];int STEP[15][15];bool hash[15][15];void DFS( int x, int y, int cnt ){ if( hash[x][y] ) {// flag =2; Count = cnt - STEP[x][y]; step = STEP[x][y...
阅读全文
摘要:编辑器加载中...
阅读全文
摘要:#include<iostream>#include<cstdio>#include<cstring>using namespace std;int num,hash[14],flag[14]={0};void DFS( int cnt,int n ){ if( cnt == n+1 ) { num++; return ; } for( int i = 1; i<= n ; i++ ) { if( !flag[i] ) { hash[cnt]=i; int Flag = 0;...
阅读全文
摘要:#include<iostream>#include<cstdio>using namespace std;int num[1024],flag,save[1024],number,N;void DFS( int n, int sum ,int cnt){ if( sum == number ) { flag = 1; for( int i = 0; i< cnt ; i++ ) { printf( i==0?"%d":"+%d",save[i] ); } puts( "" ); ret...
阅读全文
摘要:简单搜索题:#include<iostream>#include<cstdio>#include<cstring>using namespace std;class Node{public: int x,y;};Node queue[424];int d[4][2]={0,-1,1,0,0,1,-1,0};char map[24][24];int BFS( int x, int y ){ int end =0 ,first = 0; Node t; t.x = x; t.y = y; queue[end]=t; end++; while( first <
阅读全文
摘要:该题用广搜比较好,这里要处理好就是X*2的时候,我们知道如果一步一步地走也就最多终点与起点相减的绝对值,那么我们就不能超过终点加上他们的绝对值;#include<iostream>#include<cstdio>#include<cstring>using namespace std;class Node{public: int time,number;};Node queue[200024];int BFS( int A, int B ){ bool hash[200024]={0}; int dis = abs( A - B ); int first=0
阅读全文
摘要:一道典型的搜索题。#include<iostream>#include<cstdio>#include<cstring>using namespace std;char map[124][124];int n,m;int d[8][2] = { -1,-1,0,-1,1,-1,-1,0,1,0,-1,1,0,1,1,1 };void DFS( int x, int y ){ for( int i = 0 ; i< 8 ; i++ ) { int dx = x + d[i][0]; int dy = y + d[i][1]; if( map[dx][dy
阅读全文
摘要:该题与HDU 1728 逃离迷宫是一样的解题思路http://acm.hdu.edu.cn/showproblem.php?pid=1728;这里我采取的方法还是一直走到底,如果没有路了就代表一定要转完了。#include<stdio.h>#include<stdlib.h>#include<string.h>struct T{ int x,y,turn;}q[1000024];int map[1024][1024],n,m,k;int d[4][2]={ 0,1,1,0,0,-1,-1,0 };int hash[1024][1024];int inline
阅读全文
摘要:该题是一道典型的搜索题,#include<stdio.h>#include<stdlib.h>#include<string.h>struct Node{ int x, y; int time; int flag;}q[100024];int d[4][2]={ 0,1,1,0,0,-1,-1,0 };int N,M;char map[2][13][13];void getxy( int &X,int &Y,int &K ){ for( int k=0;k<2;k++ ) for( int i=1;i<=N;i++ ) f
阅读全文
摘要:这题就是有一点要注意:要开一个三维数组来保存每个点的步数状态,因为那些障碍物会在第k的倍数消失,所以在该点来走过也是可以再走的,所以要开一个三维数组保存步数状态,以为每个时间段的步数是不同的。#include<stdio.h>#include<stdlib.h>#include<string.h>const int inf=0x7fffffff;struct T{ int x,y; int step; }q[100024];int hash[124][124][16],n,m,k,X,Y;int d[4][2]={0,1,1,0,0,-1,-1,0};cha
阅读全文
摘要:这个题伤到我了!!!!搞了我好久才把他给弄出来;这里采取的方法是当选取一个方向是就一直走到底,如果没有到终点,那么一定会要转弯;#include<stdio.h>#include<stdlib.h>#include<string.h>const int inf=0x7fffffff; struct T{ int x,y; int turn; }q[100024];int d[4][2]={0,1,1,0,0,-1,-1,0};int hash[124][124],n,m,x1,x2,y1,y2,k;char map[124][124];void init(
阅读全文
摘要:典型的BFS题,这里要注意的就是边界条件,就是2*X不要超过人与牛的距离绝对值之差加上牛的距离,应为超过就没意义了,因为最长时间为人与牛的距离绝对值之差;#include<stdio.h>#include<stdlib.h>#include<string.h>struct T{ int place,time; }q[200000];int hash[200024];int BFS( int pointA,int pointB ){ int number=abs( pointA-pointB ); memset( hash,0,sizeof( hash ) )
阅读全文
摘要:这题重要的走过的点可以走第二次,但是我们会发现当走过4时我们可以标记为0,因为回走是没必要#include<stdio.h>#include<stdlib.h>#include<string.h>struct T{ int x,y,time,step; }q[10024];int map[12][12],N,M,x,y;int d[4][2]={ 1,0,0,1,-1,0,0,-1 };int BFS( ){ int first=0,end=0; T t; t.step=0;t.time=6; t.x=x;t.y=y; q[end++]=t; wh...
阅读全文
摘要:该题是一道典型的BFS题,如果你用DFS你就会要把所有的肯能枚举,而BFS就不需要这样。#include<stdio.h>#include<stdlib.h>struct T{ int x, n; }q[424];int BFS( int n,int k[],int start,int end ){ int first=0,tail=0,hash[224]={0}; T t; hash[start]=1; t.x=start;t.n=0; q[first++]=t; while( first>tail ) { int count=q[t...
阅读全文

浙公网安备 33010602011771号