
搜索
sepNINE
it is written
展开
-
poj 2286 The Rotation Game 迭代加深搜索
//poj 2286//sep9#include using namespace std;int move[8][9]={{0,2,6,11,15,20,22}, {1,3,8,12,17,21,23}, {10,9,8,7,6,5,4}, {19,18,17,16,15,14,13}, {23,21,17原创 2017-08-24 19:18:44 · 514 阅读 · 0 评论 -
poj 1650 Integer Approximation “追赶法”搜索
题意:给a,l,求n,d分析:两个需要枚举的变量,由于两个变量有依赖关系,可采取“追赶法”,能在O(n)时间解决问题。代码://poj 1650//sep9#include #include using namespace std;int main(){ int n,d,ansn,ansd,l; double a,minx,now,tmp; while(sc原创 2015-12-14 15:45:17 · 887 阅读 · 0 评论 -
poj 2818 Making Change 枚举
//poj 2818//sep9include using namespace std;int Q,D,N,P,C;struct NODE{ int a,b,c,d;};pair mymin(pair x,pair y){ if(x.first<y.first) return x; return y;}void solve(){ NODE x; pair ans=原创 2015-12-11 17:06:23 · 621 阅读 · 0 评论 -
poj 2558 Genetic Code 深搜+打表
题意:求一个长度为n的串n分析:一开始各种构造,后来发现可以深搜,输出结果的时候打表就可以了,原来O(3^5000)的程序也不是一定会超时的。。只要剪枝吊,照样可以搞。。代码://poj 2558//sep9#include using namespace std;const int maxN=5000;int n,ok,a[maxN+10];bool check原创 2015-11-17 17:15:22 · 765 阅读 · 0 评论 -
poj 1315 Don't Get Rooked 暴力枚举
//poj 1315//sep9#include using namespace std;int n;char g[8][8];int tmp[8][8];bool check(){ for(int i=0;i<n;++i){ int flag=0; for(int j=0;j<n;++j){ if(g[i][j]=='X'&&tmp[i][j]==0){原创 2015-11-13 16:55:41 · 648 阅读 · 0 评论 -
poj 1980 Unit Fraction Partition 深搜剪枝
#include using namespace std;int p,q,a,n,ans;int gcd(int x,int y){ return x%y==0?y:gcd(y,x%y);}int lcm(int x,int y){ return x/gcd(x,y)*y; }void dfs(int cur,int pre,int up,int down,int pr原创 2015-11-09 12:02:15 · 878 阅读 · 0 评论 -
poj 1010 STAMPS 枚举组合
//poj 1010//sep9#include #include using namespace std;int stamp[128],n,sum,ok,tie;struct SOL{ int types; int high; int num; int v[8];}best,now;int vis[128];void judge(){ if(now.types=原创 2015-10-21 15:48:24 · 557 阅读 · 0 评论 -
poj 3369 Grocery store 枚举
题意:求a,b,c,d满足a+b+c+d==a*b*c*d分析:本地打表后可以适当减小枚举范围,否则超时。代码://poj 3369//sep9#include using namespace std;typedef int INT;int main(){ INT x,y,d; for(INT a=1;a<=125;++a) for(INT b=a;b<=6原创 2015-11-06 16:24:12 · 552 阅读 · 0 评论 -
poj 1775 Sum of Factorials 枚举子集
//poj 1775//sep9#include using namespace std;const int MAXN=1000024;int f[20];int d[MAXN+10];int main(){ int n; f[0]=f[1]=1; for(int i=2;i<=9;++i) f[i]=f[i-1]*i; memset(d,0,sizeof(d));原创 2015-10-20 17:23:24 · 478 阅读 · 0 评论 -
poj 2049 Finding Nemo 建图技巧型最短路
题意:给一个由墙和门组成的迷宫,求从原点到目标点最少要经过多少道门。分析:有人用bfs来,结果写的是介于bfs和最短路中间不伦不类的算法。。这题边权为0或1,用bfs求最短路靠谱么??bfs是只能求边权恒定的最短路,这题用bfs过的是因为把bfs写成了spfa(spfa本来就是一种优化的bfs算法),bfs是什么?搜索,搜索的含义是搜到一个状态以后就不会再经过这个状态了,故在bfs里对原创 2015-09-10 19:38:16 · 724 阅读 · 0 评论 -
poj 2157 Maze 枚举排列+dfs搜迷宫
题意:给一个迷宫,求是否可从S走到G,迷宫中有门课钥匙,要获得相应种类的所有钥匙才能打开该种门(比如获得所有a才能打开A)。分析:枚举打开门的顺序,搜路径即可。代码://poj 2157//sep9#include#includeusing namespace std;const int maxN=32;char maze[maxN][maxN];char g[m原创 2015-09-11 17:27:48 · 1132 阅读 · 0 评论 -
poj 2038 Team Rankings 枚举排列
//poj 2038//sep9#include #include using namespace std;char s[128][8];int count(char s1[],char s2[]){ int cnt=0; for(int i=0;i<5;++i) for(int j=i+1;j<5;++j){ int k; for(k=0;k<5;++k)原创 2015-07-06 10:21:48 · 1005 阅读 · 0 评论 -
poj 3039 Close Encounter “追赶法”搜索
//poj 3039//sep9//similar with poj 1650#include #include using namespace std;const int MAXX=32767;int gcd(int x,int y){ return x%y==0?y:gcd(y,x%y);}int main(){ int n,d,ansn,ansd,x,y; dou原创 2015-12-15 01:14:47 · 628 阅读 · 0 评论 -
poj 1465 Multiple 静态节点+余数判重+非STL队列的广搜
//poj 1465//sep9#include #include using namespace std;int n,m,p;int num[16],vis[8192];struct NODE{ int dig,r,pre; }Q[8192];void print(int x){ if(x!=-1){ print(Q[x].pre); printf("%d",原创 2015-12-16 04:07:27 · 487 阅读 · 0 评论 -
poj 2138 Travel Games 搜索+自动机水题
//poj 2138//sep9#include using namespace std;const int MAXN=1024;const int MAXL=80;int n,ecnt;struct edge{ char s[MAXL+4]; bool exist; int nxt;}first,e[MAXN+4];int head[MAXL+4];bool judg原创 2017-07-28 00:29:49 · 422 阅读 · 0 评论 -
poj 1022 Packing Unit 4D Cubes 搜索
//poj 1022//sep9#include using namespace std;const int MAXN=128;const int MAX_INDEX=1024;int g[MAXN][MAXN][10];int vis[MAXN],map[MAX_INDEX],minx[4],maxx[4],x[4];int cases,n,cnt;void dfs(int原创 2017-08-02 19:36:10 · 478 阅读 · 0 评论 -
poj 1040 Transportation 深搜剪枝
//poj 1040//sep9#include using namespace std;int n,m,order_num,ans;struct Order{ int s,t,num,val; }orders[32];int passengers[16],most_earn[32];void dfs(int cur,int sum_value){ if(cur>=ord原创 2017-07-19 22:35:15 · 540 阅读 · 0 评论 -
poj 1128 Frame Stacking 字典序输出拓扑序
//poj 1128//sep9 #include using namespace std; const int MAXLEN=36; const int MAXANS=72; char s[MAXLEN][MAXLEN]; int l[MAXLEN],r[MAXLEN],u[MAXLEN],d[MAXLEN],g[MAXLEN][MAXLEN]; int in_degre原创 2016-10-12 10:04:26 · 490 阅读 · 0 评论 -
poj 1376 Robot 广搜
//poj 1376//sep9#include #include using namespace std;const int maxN=128;int n,m,sx,sy,sdir,ex,ey;int g[maxN][maxN],vis[maxN][maxN][4];int dx[]={-1,0,1,0};int dy[]={0,1,0,-1};struct NODE{原创 2016-07-15 13:26:08 · 510 阅读 · 0 评论 -
poj 1101 The Game 广搜
//poj 1101//sep9#include using namespace std;int w,h;char g[128][128];int d[128][128];struct Node{ int x,y,v;}q[128*128];int ex,ey,tx,ty;int dx[4]={-1,1,0,0};int dy[4]={0,0,-1,1};void原创 2016-04-24 13:26:20 · 715 阅读 · 0 评论 -
poj 1054 The Troublesome Frog 枚举
//poj 1054//sep9#include #include using namespace std;struct NODE{ int x,y;}p[5012];bool vis[5012][5012];int n,r,c;int cmp(NODE a,NODE b){ return a.y!=b.y?a.y<b.y:a.x<b.x; }int main(){原创 2016-05-05 17:41:47 · 813 阅读 · 0 评论 -
poj 1190 dfs+剪枝
题意:维数为m,维度需枚举的深搜,剪枝很重要,手痒了随便找题写写没想到被卡了2个小时。。代码://poj 1190//sep9#include #include using namespace std;int N,M,ans,rm,min_s[20],min_v[20];void dfs(int cur,int res,int sum,int pre_r,int pre_原创 2016-03-18 17:47:44 · 550 阅读 · 1 评论 -
poj 3258 River Hopscotch 二分搜索极值
//poj 3258//sep9#include #include using namespace std;int L,N,M;int a[50012],b[50012];bool judge(int mid){ int i=0,sum=0; while(i<=N){ if(b[i]>=mid){ ++i; continue; } int tmp=b[原创 2016-01-06 12:53:47 · 494 阅读 · 0 评论 -
poj 1178 Camelot 枚举
//poj 1178//sep9#include using namespace std;char s[132];int da[8][2]={ {-2,-1},{-2,1},{-1,-2},{-1,2}, {2,1},{2,-1},{1,2},{1,-2} };int db[8][2]={ {-1,-1},{-1,0},{-1,-1},{0,-1}, {0,1},{1,-1}原创 2015-12-04 09:08:27 · 558 阅读 · 0 评论 -
poj 1186 方程的解数 折半枚举+hash
//poj 1186//sep9#include using namespace std;const int HASHLEN=1000023;const int MAXNODENUM=4000024;int n,m,ans,e,tag;int k[32],p[32];int head[HASHLEN+10];struct Node{ int val,cnt,nxt;}edg原创 2015-12-02 22:44:40 · 732 阅读 · 0 评论 -
poj 1905 Expanding Rods 二分解方程
题意: 中已知L,S解h。分析:两个方程两个未知数,理论是可解的。解起来有困难,可用二分的方法。代码:#include #include using namespace std;int main(){ double l,n,c,s,r; while(scanf("%lf%lf%lf",&l,&n,&c)==3){ if(l<0) break;原创 2015-06-15 12:23:57 · 745 阅读 · 0 评论 -
poj 1166 The Clocks 记录路径的广搜
题意:给9个时钟的初始状态,和一些对某几个钟的操作,求最少经过几步能到目标状态(全指向12点)。分析:明显的广搜,但实现起来的细节要注意:1.因为要记录路径,所以要在整个程序执行过程中扩展出的节点在输出路径前不能销毁, 故采用静态内存分配的方法(开node[600000],用get_node()创建节点。2.queue比queue要多花1别的时间。//poj 1166//sep9原创 2015-06-05 17:26:55 · 755 阅读 · 0 评论 -
poj 3221 Diamond Puzzle 反向bfs
分析:简单的bfs,但要注意因为输入cases很多,对每个初始状态都搜一遍会超时,其实可以从终止状态反向搜一遍,对搜到的每个状态打表存下即可。代码://poj 3221//sep9#include #include using namespace std;int n;int fac[]={1,1,2,6,24,120,720,5040,40320}; int vis[1原创 2015-06-25 19:33:50 · 870 阅读 · 0 评论 -
poj 2585 Window Pains 暴力枚举排列
题意:在4*4的格子中有9个窗口,窗口会覆盖它之下的窗口,问是否存在一个窗口放置的顺序使得最后的结果与输入相同。分析:在数据规模较小且不需要剪枝的情况下可以暴力(思路清晰代码简单),暴力一般分为枚举子集(for(s=0;s代码://poj 2585//sep9#include #include using namespace std;int order[10];in原创 2015-03-18 13:10:39 · 819 阅读 · 0 评论 -
poj 3977 Subset 折半枚举
题意:给n(n分析:折半枚举。代码://poj 3977//sep9#include #include using namespace std;typedef long long LL;const int maxN=40;LL a[maxN];LL myabs(LL x){ return x>=0?x:-x;}int main(){ int n;原创 2015-03-22 10:57:04 · 1087 阅读 · 0 评论 -
poj 2918 Tudoku 数独dfs
题意:解数独游戏。分析:这道数独的数独直接dfs就能过。代码://poj 2918//sep9#includeusing namespace std;char s[12][12];int board[12][12];int CheckSquare[12][12];int CheckRow[12][12];int CheckColumn[12][12];int原创 2015-03-09 20:34:46 · 935 阅读 · 0 评论 -
poj 2507 Crossed ladders 二分解方程
题意:给两把梯子的长度x,y和他们交点的高度c,求两梯子底部之间的距离。分析:化简后得方程c/sqrt(x^2-w^2)+c/sqrt(y^2-w^2)=1,f(w)=c/sqrt(x^2-w^2)+c/sqrt(y^2-w^2)单调增,可以二分解,注意精度。代码://poj 2507//sep9#include #include using namespace std原创 2015-01-23 11:31:16 · 1318 阅读 · 0 评论 -
poj 2837 Silver Matrix 不使用栈的深搜
深搜可以不用栈或递归?!!原创 2015-01-05 09:32:14 · 987 阅读 · 0 评论 -
poj 1416 Shredding Company 模拟+枚举
题意:给一个目标数和一个待分割数,可以对待分割数进行任意划分,比如将带分割数12305分为12,30,5,问将分好的数加起来最接近且不超过目标数的分割方案。分析:关键是在对带分割数的任意划分,直接for循环枚举状态,比如状态10101(二进制)表示将12305分为1,23,05.代码:#include #include using namespace std;int t,原创 2015-01-22 16:04:26 · 873 阅读 · 0 评论 -
poj 1691 Painting A Board 拓扑序+dfs
题意:一个木板上被分成了很多区域,每个区域要涂上一种特定的颜色,当涂一个区域的时候,它上方与它有重合部分的区域必须之前要涂好。求最少需要拿几次画笔(拿一次画笔可以涂颜色相同的多个区域)。分析:上方与它有重合部分的区域必须之前要涂好这个限制可以用拓扑序来描述,每次涂有很多种可以涂的颜色,dfs就可以了。dfs的状态空间维数定为拿笔的次数,每次枚举可以涂的颜色。要注意的是dfs过程中要慎用原创 2015-01-11 08:46:09 · 1216 阅读 · 0 评论 -
poj 1950 Dessert 深搜
题意;给n,问有多少和由1,2...n,和'+','-',’.‘(表示连接,4.5表示45)组成,值为0的表达式。分析:深搜,因为要枚举到“连接”的情况,所以传送pre表示上个表达式中最后一个数字。代码://poj 1950//sep9#include using namespace std;__int64 n,tot,printNum;__int64 ans[32]原创 2014-12-01 18:53:07 · 696 阅读 · 0 评论 -
poj 3411 Paid Roads dfs+状态压缩
题意:求点1到点n的最短路,每条路可能有两种权值,如果之前访问过某个点c则权值为p,否则为r。思路:将之前访问过的点序列状态压缩+深搜。其实也可以不用状态压缩,当判断是否访问过c时判断cnt[c]>0就可以了。代码://poj 3411//sepNINE#include using namespace std;const int maxN=16;int head[ma原创 2014-11-26 14:50:04 · 660 阅读 · 0 评论 -
poj 3279 Fliptile 枚举子集
题意:原创 2014-11-21 12:03:24 · 575 阅读 · 0 评论 -
poj 1759 Garland 二分搜索
题意:有n个dongbao原创 2014-11-21 00:39:38 · 642 阅读 · 0 评论 -
poj 4022 ASCII Area dfs求二维面积
题意:给一个有'/','\','.'组成的二维字符数组,求图中‘/’和‘\’组成的面积有多大。分析:每个‘/’和‘\’的格每个贡献1/2的面积,每个多边形内部的'.'贡献1的面积,关键是求多边形内部的’.‘有多少个。一开始往上下左右4个方向搜wa了,原来内部的点可以斜着扩展,比如/./这种情况。但斜着搜要注意避免从多边形内部跑到外部的情况,比如题目中给的sample。 代码:/原创 2015-04-07 23:20:10 · 612 阅读 · 0 评论