蓝桥杯第七届初赛试题 剪邮票

如【图1.jpg】, 有12张连在一起的12生肖的邮票。


现在你要从中剪下5张来,要求必须是连着的。
(仅仅连接一个角不算相连)
比如,【图2.jpg】,【图3.jpg】中,粉红色所示部分就是合格的剪取。


请你计算,一共有多少种不同的剪取方法。
请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。

题目答案:

116


#include<stdio.h> 
#include<string.h>
bool b[5];
int a[12]={1,2,3,4,5,6,7,8,9,10,11,12};
int flag=1;
int count=0;
int position=0;
void arrangement(int position,int k);//列出所有可能的组合数,用DFS的方法 
bool check(int s,int t);//判断两点是否相连 
void dfs(int a[],int w);//判断五个点是否连通 
int main(){
	arrangement(position,0);
	printf("%d",count);
    return 0;
}

void arrangement(int position ,int k) {
	int i;
	if(position==5)
	
	    {   memset(b,false,sizeof(b));
	       dfs(a,0);
	    if(flag==5){
		for(i=0;i<5;i++) 
		    printf("%d ",a[i]);
		    printf("\n");
			flag=1;
			count++ ;  }
		else
		flag=1;	}
	
else{
	
	for(i=k+1;i<=12;i++){
	   	   a[position]=i;
	        arrangement(position+1,i);
	              
	  
	  }
	 }
}

void dfs(int a[],int w){
	int i;
	b[w]=true;
	for(i=0;i<5;i++)
	  if(check(a[w],a[i])&&!b[i]){
	       flag++;
	      dfs(a,i);
	          }

}

bool check(int s,int t){// 这个方法比较蠢,有更聪明的办法 
	  
	if(s==1)
	   {if(t==2||t==5)
	       return true;
		else 
		   return false;  
	   }
else if(s==2)
	{if(t==1||t==6||t==3)
	       return true;
		else 
		   return false;  
	   }
else if(s==3)
	{if(t==2||t==7||t==4)
	       return true;
		else 
		   return false;  
	   }
else if(s==4)
	{if(t==3||t==8)
	       return true;
		else 
		   return false;  
	   }
else if(s==5)
	{if(t==1||t==6||t==9)
	       return true;
		else 
		   return false;  
	   }
else if(s==6)
	{if(t==2||t==5||t==7||t==10)
	       return true;
		else 
		   return false;  
	   }
else if(s==7)
	  {if(t==3||t==6||t==8||t==11)
	       return true;
		else 
		   return false;  
	   }
else	if(s==8)
	  {if(t==4||t==7||t==12)
	       return true;
		else 
		   return false;  
	   }
else	if(s==9)
	{if(t==5||t==10)
	       return true;
		else 
		   return false;  
	   }
else	if(s==10)
	{if(t==6||t==9||t==11)
	       return true;
		else 
		   return false;  
	   }
else	if(s==11)
	{if(t==10||t==7||t==12)
	       return true;
		else 
		   return false;  
	   }
else	if(s==12)
	  {if(t==11||t==8)
	       return true;
		else 
		   return false;  
	   }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值