上一页 1 ··· 13 14 15 16 17 18 19 20 21 ··· 30 下一页
  2012年9月2日
摘要: 大意:给定一个迷宫以及入口与出口,限制条件为最多的拐弯数为k,问是否在拐弯数limit之内要能够从迷宫出去。思路:用BFS求得每个顶点的最小拐弯数,然后判断出口拐弯数是否小于或等于k就行了,如果不能,则所有的都不能。那如何求得呢?(1)即一条路走到底,如果出队拓展路径或者一直往前延展且没有被标记那么则step++;(2)遇到已经走过的(已标记),说明这个节点的最小拐弯数不是它,经过它,但拐弯数不增加,继续往前走。(3)比较最先一个到达出口的拐弯数判断与limit的相对大小。关于特殊情况的讨论:数字代表可能的步数,x代表标记,s代表新的节点。1xx 3sxx 0像这种情况,0的可能拐弯数最小可能 阅读全文
posted @ 2012-09-02 16:29 有间博客 阅读(1757) 评论(0) 推荐(0) 编辑
摘要: BFS.CODE:#include<stdio.h>#include<stdlib.h>#include<string.h>#include<queue>usingnamespacestd;constintSIZE=100010;#definelow_bound0#definehigh_bound100010intv[SIZE];intn,k;structnode{intx,step;};intcheck(intx){if(x>=low_bound&&x<=high_bound)return1;return0;}int 阅读全文
posted @ 2012-09-02 12:04 有间博客 阅读(155) 评论(0) 推荐(0) 编辑
摘要: 大意:给定一个3D迷宫,已知出口和入口,求最小逃脱迷宫的时间。思路1:DFS需要求最小路需要回溯,果断TLE。下附DFS代码:voiddfs(intx,inty,intz){if(!check(x,y,z)||flag[x][y][z]||maze[x][y][z]=='#')return;elseif(x==ex&&y==ey&&z==ez){MIN<?=step;return;}for(inti=0;i<6;i++){intxx=x+dx[i];intyy=y+dy[i];intzz=z+dz[i];flag[xx][yy][zz 阅读全文
posted @ 2012-09-02 11:05 有间博客 阅读(169) 评论(0) 推荐(0) 编辑
  2012年9月1日
摘要: 题目大意:从一个素数变换到另一个素数最小需要多少步。思路:将所有1000~10000的素数通过打表表示出来,然后依次枚举与s相差一位的数并标记,通过BFS找寻最小步数。CODE:#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<queue>usingnamespacestd;intprime[10001];intvis[10001];intflag[10001];inttot;ints,e;structnode{intx,step 阅读全文
posted @ 2012-09-01 21:51 有间博客 阅读(153) 评论(0) 推荐(0) 编辑
摘要: 简单深搜。CODE:#include<iostream>#include<string.h>usingnamespacestd;constintlarge=200030;typedefclass{public:intx;intstep;}pos;intn,k;boolvist[large];posqueue[large];voidBFS(void){inthead,tail;queue[head=tail=0].x=n;queue[tail++].step=0;vist[n]=true;while(head<tail){posw=queue[head++];if( 阅读全文
posted @ 2012-09-01 19:54 有间博客 阅读(134) 评论(0) 推荐(0) 编辑
摘要: 简单搜索题。 注意:回溯时需要减1,否则会WA。CODE:#include<stdio.h>#include<stdlib.h>#include<string.h>usingnamespacestd;constintSIZE=30;intdx[]={1,0,-1,0};intdy[]={0,1,0,-1};charmaze[SIZE][SIZE];intflag[SIZE];intans,n,m,num;intcheck(intx,inty){if(x>=0&&y>=0&&x<n&&y< 阅读全文
posted @ 2012-09-01 17:43 有间博客 阅读(191) 评论(0) 推荐(0) 编辑
  2012年8月31日
摘要: 大意:给你一张棋盘,给你一定的棋子,空白区域不能放,问最多可能有几种方法。思路:典型的搜索题目。CODE:#include<stdio.h>#include<stdlib.h>#include<string.h>usingnamespacestd;constintSIZE=80;intv[SIZE];charmaze[SIZE][SIZE];intn,k;intcnt;voiddfs(intr,intnum){if(num==k){cnt++;return;}if(r>n)return;for(inti=0;i<n;i++){if(!v[i]&a 阅读全文
posted @ 2012-08-31 15:21 有间博客 阅读(112) 评论(0) 推荐(0) 编辑
摘要: 大意:通过货币的兑换去套利,若能套利则输出Yes,否则输出No。思路:通过Floyd算法算出的d[i][i]即每种货币能不能套利,只要存在一种则输出Yes。数据的处理通过STL中的map容器去处理。CODE:#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<map>usingnamespacestd;constintSIZE=34;doubled[SIZE][SIZE];map<string,int>hash; //ma 阅读全文
posted @ 2012-08-31 11:02 有间博客 阅读(257) 评论(0) 推荐(0) 编辑
  2012年8月30日
摘要: 思路:i * j + i + j------> (i + 1) * (j + 1) = N+1----> 2 <= i+1 <= sqrt(N+1); (0 < i <= j)CODE:#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>usingnamespacestd;intmain(){intT;scanf("%d",&T);while(T--){inti,j;__int64N;intcnt= 阅读全文
posted @ 2012-08-30 21:30 有间博客 阅读(111) 评论(0) 推荐(0) 编辑
摘要: 题意:求一个集合到另一个集合的最小路径。思路1:直接用循环T次Dijkstra算法,从而求得最小值,但是TLE了。后来加一个添加一个新的源点,把起点与源点路径的赋值为0就可以啦。条件:(1)有向图。(2)T次Dijkstra可能会超时。CODE:#include<stdio.h>#include<stdlib.h>#include<string.h>usingnamespacestd;constintSIZE=1010;constintINF=0x3f3f3f3f;intd[SIZE],v[SIZE];intw[SIZE][SIZE];intsave[SIZ 阅读全文
posted @ 2012-08-30 20:22 有间博客 阅读(280) 评论(0) 推荐(0) 编辑
上一页 1 ··· 13 14 15 16 17 18 19 20 21 ··· 30 下一页