100道题中,99道都可以通过搜索大法得分。只练好搜索,随随便便拿个二等奖,再学点其他的,一等奖马上到手
推荐几道难度适合的搜索题目,并附上当时我的代码。题目都在codevs中,下面是其编号
- codevs1136 mayan游戏
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> #include<cstdio> #include<string.h> #include<ctime> #define L_L "%I64d" #define N 28 using namespace std; int n; struct mayan{ int a[6][9]; int c[11]; int fa,x,y,choose; void read(){ int i,j; for(i=0;i<=10;i++) c[i]=0; for(i=1;i<=5;i++){ for(j=1;;j++){ scanf("%d",&a[i][j]); if(!a[i][j])break; c[a[i][j]]++; } } } void print(){ printf("map%d %d %d\n",x,y,choose); int i,j; for(i=1;i<=5;i++){ for(j=1;j<=7;j++) printf("%d ",a[i][j]); printf("\n"); } } void down(){ int i,j,k; for(i=1;i<=5;i++){ k=0; for(j=1;j<=7;j++) if(a[i][j]) a[i][++k]=a[i][j]; for(k++;k<=7;k++) a[i][k]=0; } } void cut(){ while(1){ int d[6][9]={}; bool flag=false; int i,j,k; for(i=1;i<=5;i++) for(j=1;j<=7;j++) if(a[i][j]){ for(k=0;a[i][j+k]&&a[i][j+k]==a[i][j]&&j+k<=7;k++); if(k>2) { for(k--;k>=0;k--)d[i][j+k]=1; flag=true; } for(k=0;a[i+k][j]&&a[i+k][j]==a[i][j]&&i+k<=5;k++); if(k>2){ for(k--;k>=0;k--)d[i+k][j]=1; flag=true; } } for(i=1;i<=5;i++){ k=0; for(j=1;j<=7;j++) if(d[i][j]&&a[i][j]) {c[a[i][j]]--;a[i][j]=0;} down(); } if(!flag) return; } } }stack[10]; bool flag=false; inline void DFS(int dep){ int i,x,y; for(i=1;i<=10;i++) if(stack[dep].c[i]>0&&stack[dep].c[i]<3) return; if(dep==n){ for(i=1;i<=10;i++) if(stack[dep].c[i])return; flag=true; return; } for(x=1;x<=5;x++) for(y=1;y<=7;y++)if(stack[dep].a[x][y]){ if(x!=5){ stack[dep+1]=stack[dep]; stack[dep+1].x=x;stack[dep+1].y=y; stack[dep+1].choose=1; swap(stack[dep+1].a[x][y],stack[dep+1].a[x+1][y]); stack[dep+1].down(); stack[dep+1].cut(); DFS(dep+1); if(flag)return; } if(x!=1&&!stack[dep].a[x-1][y]){ stack[dep+1]=stack[dep]; stack[dep+1].x=x;stack[dep+1].y=y; stack[dep+1].choose=-1; swap(stack[dep+1].a[x][y],stack[dep+1].a[x-1][y]); stack[dep+1].down(); stack[dep+1].cut(); DFS(dep+1); if(flag)return; } } } void work(){ scanf("%d",&n); stack[0].read(); DFS(0); if(!flag){ printf("-1\n");return; } int i; for(i=1;i<=n;i++) printf("%d %d %d\n",stack[i].x-1,stack[i].y-1,stack[i].choose); } int main(){ freopen("codevs1136.in","r",stdin); freopen("codevs1136.out","w",stdout); work(); fclose(stdin); fclose(stdout); return 0; }
- codevs1064 虫食算
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> #include<cstdio> #include<string.h> #include<ctime> #define L_L "%I64d" #define N 28 using namespace std; int use[N],val[N]; int a[N][3]; int n; int chs[N]; bool flag=false; inline bool check(int i){ int u,v; for(i--;i;i--){ if(chs[a[i][0]]&&chs[a[i][1]]&&chs[a[i][2]]){ v=(val[a[i][0]]+val[a[i][1]])%n; if(v!=val[a[i][2]]&&(v+1)%n!=val[a[i][2]]){ //printf("false %d %d %d %d %d\n",i,val[a[i][0]],val[a[i][1]],val[a[i][2]],(val[a[i][0]]+val[a[i][1]]+1)%n); return false; } } } return true; } inline void DFS(int now,int jin){ int u,v,w,i,j; if(now==0){ if(jin)return; flag=true; return; } if(chs[a[now][0]]&&chs[a[now][1]]&&chs[a[now][2]]){ if((jin+val[a[now][0]]+val[a[now][1]])%n==val[a[now][2]]){ jin=(jin+val[a[now][0]]+val[a[now][1]])/n; DFS(now-1,jin); } return; }//¶¼ÓÐ if(chs[a[now][0]]&&chs[a[now][1]]){ v=(jin+val[a[now][0]]+val[a[now][1]])%n; // printf("caseV %d %d %d %d %d[%d %d %d]%d\n",now,v,jin,val[a[now][0]],val[a[now][1]],a[now][0],a[now][1],a[now][2],use[v]); if(use[v])return;//false // printf("yes %d\n",v); jin=(jin+val[a[now][0]]+val[a[now][1]])/n; chs[a[now][2]]=1;val[a[now][2]]=v;use[v]=1; // printf("beginer%d\n",v); if(!check(now)){//printf("false %d\n",v); chs[a[now][2]]=0;val[a[now][2]]=0;use[v]=0; return; } DFS(now-1,jin); if(!flag){ chs[a[now][2]]=0;val[a[now][2]]=0;use[v]=0; } return; } if(chs[a[now][2]]&&(chs[a[now][1]]||chs[a[now][0]])){ u=chs[a[now][0]]?1:0; v=(val[a[now][2]]-val[a[now][u^1]]-jin)%n; if(v<0)v+=n; if(use[v])return; chs[a[now][u]]=1;val[a[now][u]]=v;use[v]=1; jin=(jin+val[a[now][0]]+val[a[now][1]])/n; // printf("caseF %d %d %d %d %d[%d %d %d]%d %d\n",now,u,jin,val[a[now][0]],val[a[now][1]],a[now][0],a[now][1],a[now][2]); if(!check(now)) { chs[a[now][u]]=0;val[a[now][u]]=0;use[v]=0; return; } DFS(now-1,jin); if(!flag){ chs[a[now][u]]=0;val[a[now][u]]=0;use[v]=0; } return; } //ÓÐÁ½¸ö if(chs[a[now][0]]||chs[a[now][1]]){ u=chs[a[now][0]]?1:0; // if(val[a[12][0]]==8&&val[a[12][1]]==5&&val[a[12][2]]==1) //printf("DFS %d\n",now); for(i=0;i<n;i++) if(!use[i]){ chs[a[now][u]]=1;val[a[now][u]]=i;use[i]=1; if(!chs[a[now][2]]){ v=(jin+val[a[now][0]]+val[a[now][1]])%n; if(!use[v]){ chs[a[now][2]]=1;val[a[now][2]]=v;use[v]=1; w=(jin+val[a[now][0]]+val[a[now][1]])/n; if(check(now)){ DFS(now-1,w); if(flag)return; } chs[a[now][2]]=0;val[a[now][2]]=0;use[v]=0; } } else { v=(jin+val[a[now][0]]+val[a[now][1]])%n; if(v==val[a[now][2]]){ w=(jin+val[a[now][0]]+val[a[now][1]])/n; if(check(now)){ DFS(now-1,w); if(flag)return; } } } chs[a[now][u]]=0;val[a[now][u]]=0;use[i]=0; } return; } if(chs[a[now][2]]){ for(i=0;i<n;i++) if(!use[i]){ chs[a[now][0]]=1;val[a[now][0]]=i;use[i]=1; if(!chs[a[now][1]]){ v=(val[a[now][2]]-val[a[now][0]]-jin)%n; if(v<0)v+=n; if(!use[v]){ chs[a[now][1]]=1;val[a[now][1]]=v;use[v]=1; w=(jin+val[a[now][0]]+val[a[now][1]])/n; if(check(now)){ DFS(now-1,w); if(flag)return; } chs[a[now][1]]=0;val[a[now][1]]=0;use[v]=0; } } else { v=(val[a[now][2]]-val[a[now][0]]-jin)%n; if(v<0)v+=n; if(val[a[now][1]]==v){ w=(jin+val[a[now][0]]+val[a[now][1]])/n; if(check(now)){ DFS(now-1,w); if(flag)return; } } } chs[a[now][0]]=0;val[a[now][0]]=0;use[i]=0; } return; } //ö¾ÙÁ½¸ö¶¨Ò»¸ö for(i=0;i<n;i++) if(!use[i]){ chs[a[now][0]]=1;val[a[now][0]]=i;use[i]=1; for(j=0;j<n;j++) if(!chs[a[now][1]]||val[a[now][1]]==j){ if(!chs[a[now][1]]){ if(use[j])continue; chs[a[now][1]]=1;val[a[now][1]]=j;use[j]=1; } v=(jin+val[a[now][0]]+val[a[now][1]])%n; w=(jin+val[a[now][0]]+val[a[now][1]])/n; //printf("tryer %d %d %d [%d %d %d %d %d](%d)\n",i,j,v,use[0],use[1],use[2],use[3],use[4],chs[a[now][2]]); if((!chs[a[now][2]]&&!use[v])||val[a[now][2]]==v){ // printf("try %d %d %d %d %d %d %d %d\n",i,j,v,use[0],use[1],use[2],use[3],use[4]); chs[a[now][2]]=1;val[a[now][2]]=v;use[v]=1; if(check(now)){ DFS(now-1,w); if(flag)return; } if(a[now][2]!=a[now][1]&&a[now][2]!=a[now][0]){ chs[a[now][2]]=0;val[a[now][2]]=0;use[v]=0; } } if(a[now][1]!=a[now][0]){ chs[a[now][1]]=0;val[a[now][1]]=0;use[j]=0; } } chs[a[now][0]]=0;val[a[now][0]]=0;use[i]=0; } } void work(){ scanf("%d",&n); register int i,j; char ch; for(i=0;i<3;i++){ for(j=1;j<=n;j++) { ch=getchar(); while(ch<'A'||ch>'Z') ch=getchar(); a[j][i]=ch-'A'+1; } } DFS(n,0); for(i=1;i<=n;i++) printf("%d ",val[i]); } int main(){ freopen("codevs1064.in","r",stdin); freopen("codevs1064.out","w",stdout); work(); fclose(stdin); fclose(stdout); return 0; }
- codevs4610 斗地主
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> #include<cstdio> #include<string.h> #include<ctime> #define L_L "%I64d" #define N 20 using namespace std; int t,n; int a[N]; int ans; int b[5]; inline void dfs2(int b1,int b2,int b3,int b4,int nt,int jie,int r1,int r2,int yes){ //yes±íʾÊÇ·ñÓÐ3¸ö´ø1¸ö£¬Õâ¿ÉÒÔ½â¾ö1¸öÂäµ¥ÊÇ·ñ¿ÉÒÔ±»´øµÄÇé¿ö£¨Ëĸö²»ÄÜ´øµ¥ÅÆ£© if(nt>=ans)return; //×îÖØÒªµÄÊǼÆËã if(b3==0&&b4==0){ //printf("calc %d %d %d %d %d\n",b1,b2,b3,b4,nt); if((b1&1)&&!yes&&r1){ //ÖÁÉÙÒª¶à´òÒ»Õŵ¥ÅÆ if(b2<=r2){ b2=0;r2=0; if(r1>b1) nt++; else { nt+=(b1-r1); } if(ans>nt)ans=nt; return; } else if(r1<=b1){ { nt=nt+(b1-r1)+(b2-r2); if(ans>nt)ans=nt; return; } }else { //b1<=r1,b2>=r2 r1-=(b1-1);nt++; b2-=(r1>>1); b2-=r2; if(b2<0)b2=0; nt+=b2; if(ans>nt)ans=nt; return; } } else {//printf("case2\n"); if(b2<=r2){ b2=0;r2=0; if(r1>=b1); else { nt+=(b1-r1); } if(ans>nt)ans=nt; return; } else if(r1<=b1)//r2<=b2 {//printf("oh %d\n",nt+b1-r1+b2-r2); nt=nt+(b1-r1)+(b2-r2); if(ans>nt)ans=nt; return; } else {//b1<=r1,b2>=r2 r1-=b1; b2-=(r1>>1); b2-=r2; if(b2<0)b2=0; nt+=b2; if(ans>nt)ans=nt; return; } } } //¼ÆËãÍêÖ®ºó¾Í¼òµ¥Ã¶¾ÙÁË if(b4){ if(jie<=0) dfs2(b1,b2,b3,b4-1,nt+1,0,r1,r2+2,0); if(jie<=1) dfs2(b1,b2,b3,b4-1,nt+1,1,r1+2,r2,0); if(jie<=2) dfs2(b1,b2,b3,b4-1,nt+1,2,r1,r2,0); if(jie<=3) dfs2(b1,b2+2,b3,b4-1,nt,3,r1,r2,0); if(jie<=4) dfs2(b1+b4,b2,b3+b4,0,nt,4,r1,r2,0); } else if(b3){ if(jie<=4) dfs2(b1,b2,b3-1,0,nt+1,4,r1,r2+1,0); if(jie<=5) dfs2(b1,b2,b3-1,0,nt+1,5,r1+1,r2,0); dfs2(b1+1,b2+1,b3-1,0,nt,5,r1,r2,0); } } void solve(int nt){ //¹íÖ±½Óö¾Ù£¬ËùÓÐÅƼÆÊý¼´¿É int i; b[1]=b[2]=b[3]=b[4]=0; for(i=0;i<=15;i++) if(a[i]) b[a[i]]++; //printf("solve %d %d %d %d ;%d %d; %d\n",b[1],b[2],b[3],b[4],a[16],a[17],nt); if(a[16]&&a[17]){ dfs2(b[1],b[2],b[3],b[4],nt+1,0,0,0,0);//Ë«¹í } dfs2(b[1]+a[16]+a[17],b[2],b[3],b[4],nt,0,0,0,0);//µ¥´ò } inline void dfs(int now,int nt){ //¾¡Á¿Ì°ÐijöÅÆ£¬±ãÓÚ¿ìËٱƽü½ÏÓŽâ if(nt>=ans)return; if(now==15){ solve(nt);return; } int i; for(i=0;i+now<=14;i++) if(a[i+now]<3) { break; } else a[i+now]-=3; for(i--;i>=1;i--){ dfs(now,nt+1); a[i+now]+=3; } for(;i>=0;i--) a[i+now]+=3; for(i=0;i+now<=14;i++) if(a[i+now]<2){ break; } else a[i+now]-=2; for(i--;i>=2;i--){ dfs(now,nt+1); a[i+now]+=2; } for(;i>=0;i--) a[i+now]+=2; for(i=0;i+now<=14;i++) if(!a[i+now]){ break; }else a[i+now]--; for(i--;i>=4;i--) { dfs(now,nt+1); a[i+now]++; } for(;i>=0;i--) a[i+now]++; dfs(now+1,nt); } void work(){ scanf("%d%d",&t,&n); while(t--){ memset(a,0,sizeof(a)); int u,v,i; for(i=1;i<=n;i++){ scanf("%d%d",&u,&v); if(u==2||u==1) u+=13; else if(u==0&&v==1) u=16; else if(u==0&&v==2) u=17; // printf("u=%d v=%d\n",u,v); a[u]++; } ans=n; // for(i=3;i<=17;i++) // printf("%d ",a[i]); // printf("\n"); dfs(3,0); printf("%d\n",ans); } } int main(){ freopen("codevs4610.in","r",stdin); freopen("codevs4610.out","w",stdout); work(); fclose(stdin); fclose(stdout); return 0; }