搜索练习题

100道题中,99道都可以通过搜索大法得分。只练好搜索,随随便便拿个二等奖,再学点其他的,一等奖马上到手

推荐几道难度适合的搜索题目,并附上当时我的代码。题目都在codevs中,下面是其编号

  1. codevs1136 mayan游戏
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #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;
    }
    

     

  2. codevs1064 虫食算
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #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'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;
    }
    

     

  3. codevs4610 斗地主
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #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;
    }
    

     

 

你可能感兴趣的:(高中NOIP,C++)