poj 3600 Subimage Recognition (枚举+dfs)
摘要:http://poj.org/problem?id=3600 枚举subimage第一行在image中的位置,然后在image中选取c列,若这c列中有r行和subimage相同,那么即为有解。code:#include<cstdio>#include<cstring>#include<iostream>usingnamespacestd;intvis[21];charsmap[21][21],imap[21][21];intr,c,R,C,flag;intcheck(intcr){//判断imap中是否有r行与smap相同inti,j,count=0,x=0
阅读全文
posted @
2012-04-14 08:30
追逐.
阅读(367)
推荐(1)
poj 1222 & zoj 1354 EXTENDED LIGHTS OUT (枚举)
摘要:http://poj.org/problem?id=1222 和poj 1753类似,1753是求全0或全1的步数,这题是求全0的解决方案。 当时是拿1753的代码改的,枚举步数,最多30步,这样的话状态总量就是2^30。。。 枚举第一行状态,共2^6,第一行确定了便可确定其余行,最后看末行是否全为0即可。code:#include<cstdio>#include<cstring>intmap[5][6],ans[5][6];inttur[5][2]={0,0,0,1,0,-1,1,0,-1,0};intflag;voidTur(intx,inty){for(inti=
阅读全文
posted @
2012-04-13 11:46
追逐.
阅读(311)
推荐(1)
hdu 4198 Quick out of the Harbour (bfs)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=4198 今天杭电热身赛的B题,读完题就上手写dfs,结果爆栈了,500*500的规模对暴力递归来说有点多。 然后就是bfs,遇到@时先放入另一队列,当当前step>que[head].step时que[head]入队(这里可以证明que[head]的step是que总的最小值)。第一个出口便为最终解。 思路就这样,结果写出来各种bug各种蛋疼。最后直接调晕了...code:#include<cstdio>#include<cstring>#defineMin(a,b)a>b
阅读全文
posted @
2012-04-03 19:57
追逐.
阅读(409)
推荐(1)
zoj 3332 Strange Country II (dfs)
摘要:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3332 看XSY博客有这题,就做一下,结果够让人郁闷的,太粗心了... 一开始用的宏比较多,出了个没见过的错Segmentation Fault ,这个好像类似与POJ的RE,内存错误。搞不懂,直接去掉了宏。 然后就开始WA了,impossible写成了impossbile,改正,依旧WA,发现i是大写的... 继续改正,还是WA,换了好多种dfs的方式,没用。没办法,瞎改吧。把dfs中定义的全局变量j改成局部变量,AC了... 这么简单的一道暴搜搞成了这个样子,引以为戒
阅读全文
posted @
2012-04-02 15:22
追逐.
阅读(473)
推荐(0)
poj 2157 Maze (bfs)
摘要:http://poj.org/problem?id=2157 算是细节比较多的搜索题了吧,考虑的时间比较长,最终代码写的也是那么的纠结。。。去真的不知道为什么RE啊!重新敲了一遍,完全一样的思路,然后就A掉了!搞毛啊?!白白浪费一下午找bug啊...#include<cstdio>#include<cstring>intkey[6],temkey[6];inttur[4][2]={0,1,0,-1,1,0,-1,0};boolvis[21][21];charmap[21][21];intn,m,sx,sy;structnode{intx,y;}q[500],door[6
阅读全文
posted @
2012-03-23 19:04
追逐.
阅读(355)
推荐(0)
poj 1606 Jugs (bfs)
摘要:http://poj.org/problem?id=1606 猛一看没思路,仔细想想,记住当前a,b水量及操作就可以bfs了,纯粹的暴力啊。。 输出需要路径,要在node中用指针记录当前状态的前一状态,根据oper输出即可。code:#include<cstdio>#include<cstring>inta,b,n,h,r;boolvis[1001][1001];intans[100001];structnode{intva,vb,oper,pre;}queue[1000001];voidoutput(){inti=1;ans[0]=queue[h].oper;h=qu
阅读全文
posted @
2012-03-17 22:05
追逐.
阅读(354)
推荐(0)
poj 3009 Curling 2.0 (dfs)
摘要:http://poj.org/problem?id=3009 其实一开始就想把dir当作参数,根据map条件搜索,但是却是在递归中用while处理的,处理不好map由0变1的回溯. 蛋疼的代码,看着就纠结。。code:#include<cstdio>#include<cstring>#defineMin(a,b)a>b?b:a#defineMAX1e+6inttur[4][2]={0,1,0,-1,1,0,-1,0};intmap[21][21];intw,h,ans,sx,sy,ex,ey;boolcheck(intx,inty){if(x<0||x>
阅读全文
posted @
2012-03-16 19:48
追逐.
阅读(255)
推荐(1)
poj 3256 Cow Picnic (dfs)
摘要:http://poj.org/problem?id=3256 对N个牧场用邻接表存储路径,记录下每个牧场初始牛的数目,沿路径dfs求连通牧场的牛数和。code:#include<cstdio>#include<cstring>usingnamespacestd;intnum[1001];intsum[1001];inthead[1001];boolvis[1001];intx;structpast{intv,nex;}edge[10001];voidaddedge(intu,intv){edge[x].v=v;edge[x].nex=head[u];head[u]=x;
阅读全文
posted @
2012-03-14 20:53
追逐.
阅读(261)
推荐(0)
zoj 1004 Anagrams by Stack (dfs+stack)
摘要:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 用到的主要是回溯,递归时先将所有字符放入栈中,在回溯时判断字符是否出栈并记录路径。栈是用字符数组模拟的。 另外这题的输出有点扯,每一行的最后一个i或o后面是有空格的,不需要处理,PE一次。code:#include<cstdio>#include<cstring>usingnamespacestd;charstr[50];charbstr[50],estr[50],ans[100];intblen,temp,h,r;voiddfs(intb
阅读全文
posted @
2012-03-13 21:17
追逐.
阅读(285)
推荐(0)
poj 1564 && zoj 1711 Sum It Up (dfs)
摘要:http://poj.org/problem?id=1564http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1711 简单深搜,主要是这一句 if(!vis[i]&&data[i]<=sum&&(data[i]!=data[i-1]||i==pos))。 确保两个连续的相同的数只有一次机会进入ans[],避免了结果的重复。code:#include<cstdio>#include<cstring>intdata[15];intans[15];boolvis[1
阅读全文
posted @
2012-03-11 21:13
追逐.
阅读(208)
推荐(0)
poj 2965 The Pilots Brothers' refrigerator (bfs+位运算)
摘要:http://poj.org/problem?id=2965 16个位置分别有两个状态,用一个16位的二进制表示,对beg按位或运算得到初始状态,bfs中,用只包含0,1的16进制数实现翻转操作。 剩下的就是单纯的bfs了。code:#include<iostream>#include<cstdio>#include<cstring>usingnamespacestd;intdir[16]={0x111f,0x222f,0x444f,0x888f,0x11f1,0x22f2,0x44f4,0x88f8,0x1f11,0x2f22,0x4f44,0x8f88,
阅读全文
posted @
2012-03-11 19:57
追逐.
阅读(219)
推荐(0)
poj 1562 Oil Deposits (dfs)
摘要:http://poj.org/problem?id=1562 水题。code:#include<cstdio>#include<cstring>inttur[8][2]={-1,-1,-1,0,-1,1,1,-1,1,0,1,1,0,1,0,-1};charmap[101][101];intn,m,ans,num;boolvis[101][101];structnode{intx,y;}coor[10005];boolinmap(nodep){if(p.x>=0&&p.x<n&&p.y>=0&&p.y&l
阅读全文
posted @
2012-03-01 19:17
追逐.
阅读(246)
推荐(0)
poj 2907 Collecting Beepers (dfs)
摘要:http://poj.org/problem?id=2907 挺简单的一题,但是上来就给想错了。应该是按各个可用点搜索,累加各点间距离取最小,我想的是按坐标全搜,标记过程值。code:#include<cstdio>#include<cstring>usingnamespacestd;constintMAX=1e8;intn,m,sx,sy,num,ans;structnode{intx,y;boolvis;}bee[11];intdis(intp,intq){intdx=bee[p].x-bee[q].x<0?bee[q].x-bee[p].x:bee[p].x
阅读全文
posted @
2012-03-01 19:14
追逐.
阅读(194)
推荐(0)
poj 3411 Paid Roads (dfs)
摘要:http://poj.org/problem?id=3411 这题RE了N多次,到最后也不知道是什么原因。看到网上说vis[x]不会超过3,就试着加上了<=3的限制,我了个去,马上AC! 问题应该还是递归过程爆栈了吧。code:#include<cstdio>#include<cstring>constintMAX=99999999;intvis[15];intans,n,m;boolflag;structnode{inta;intb;intc;intp;intr;}q[15];voiddfs(inti,intv){if(v>ans)return;if(i=
阅读全文
posted @
2012-02-29 19:00
追逐.
阅读(226)
推荐(0)
poj 1655 Balancing Act (树形dfs)
摘要:http://poj.org/problem?id=1655 同poj3107,只要求输出一个数值最小的点。 刚开始的答案值修改的num[]记录的,罪过罪过。。。code:#include<cstdio>#include<cstring>#defineMax(a,b)a>b?a:busingnamespacestd;constintMAX=20001;intk,n,Min,anspoint,ansnum;intvis[MAX],head[MAX],num[MAX];structEdge{intv,next;}edge[2*MAX];voidaddedge(inta
阅读全文
posted @
2012-02-29 15:11
追逐.
阅读(232)
推荐(0)
poj 3107 Godfather (树形dfs)
摘要:http://poj.org/problem?id=3107 给定一棵树,取去掉某一节点后形成子树的最大节点数,求使这个数最小的节点。 去掉某一节点后,形成的树包括子树和原树去掉以这个点为根的树所形成的树,在这几个树中求最大值即可。 节点数的计算可用回溯,过程中选取最大值。 貌似是我第一个用邻接表存边的题,贴下模板。邻接表存边code:#include<iostream>#include<cstring>#include<cstdio>usingnamespacestd;constintnMax=1000;classedge{public:intv,nex;
阅读全文
posted @
2012-02-29 15:03
追逐.
阅读(812)
推荐(0)
poj 2251 Dungeon Master (bfs)
摘要:http://poj.org/problem?id=2251 刷水题好爽!但是别真跟xcy说的一样,把rp都给用完了... 三维空间的bfs,只是搜索的时候多了两个方向而已。code:#include<cstdio>#include<cstring>boolvis[31][31][31];intlve[6]={1,0,0,0,0,-1};introw[6]={0,-1,0,0,1,0};intcol[6]={0,0,-1,1,0,0};boolflag;inta,b,c;structnode{intx;inty;intz;intstep;}q[1000000];node
阅读全文
posted @
2012-02-16 03:18
追逐.
阅读(199)
推荐(0)
poj 3126 Prime Path (bfs)
摘要:http://poj.org/problem?id=3126 给两个素数,求第一个素数转变成第二个素数所需最小步骤数。每次转换只能改变一位,且转换的中间数都为素数。 最短路径问题,打出一个素数表,对4位数的各个位置0..9暴搜。 比较郁闷的是sqrt()和pow()中必须要用强制转换才行,不记得以前要这样用啊??CE了几次。code:#include<cstdio>#include<cmath>#include<cstring>usingnamespacestd;boolprim[10000];boolvis[10000];intq[1000];voidge
阅读全文
posted @
2012-02-16 01:48
追逐.
阅读(250)
推荐(0)
poj 2676 Suduku (dfs)
摘要:http://poj.org/problem?id=2676 单纯的dfs,用三个数组记录行、列以及3×3方格的情况。 只是一开始不知道为什么没办法结束程序的运行,提交两次TLE,感觉应该是getchar()的问题。code:#include<cstdio>#include<iostream>#include<cstring>usingnamespacestd;boolc[10][10];boolr[10][10];bools[4][4][10];boolvis[10][10];charstr[10];intdata[10][10];boolfla
阅读全文
posted @
2012-02-16 00:22
追逐.
阅读(241)
推荐(0)
hdu 2899 Strange Fuction(二分)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2899 简单二分,但这个不满足单调性。 题目要求极值点,以函数导数的正负作为条件二分即可。code:#include<cstdio>#include<cmath>doubley=0;doublecal(doublex){return42*pow(x,6.0)+48*pow(x,5.0)+21*pow(x,2.0)+10*x-y;}doublesolve(doublex){return6*pow(x,7.0)+8*pow(x,6.0)+7*pow(x,3.0)+5*pow(x,2.0)-
阅读全文
posted @
2012-02-14 23:21
追逐.
阅读(192)
推荐(0)