您的位置:首页 > 其它

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

2017-03-29 21:23 204 查看
如【图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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs 算法