如【圖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;
}
}