剪邮票
如【图1.jpg】, 有12张连在一起的12生肖的邮票。
现在你要从中剪下5张来,要求必须是连着的。
(仅仅连接一个角不算相连)
比如,【图2.jpg】,【图3.jpg】中,粉红色所示部分就是合格的剪取。
请你计算,一共有多少种不同的剪取方法。
请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
#include <iostream>
#include<cstdio>#include<cstring>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int A[10];
int B[3][4];
int flag,ans=0,over;
int turns[4][2]={0,1,1,0,-1,0,0,-1};
int book[5][5];
void dfs(int m,int n){
book[m][n]=1; //注意这里,如果book写在成book【tx】【ty】则不能保存第一次编历过的信息
if(over){
return;
}
if(flag==5){
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
printf("%d ",B[i][j]);
}
printf("\n");
}
printf("\n");
ans++;
over=1;
return;
}
int tx,ty;
for(int i=0;i<=3;i++){
tx=turns[i][0]+m;
ty=turns[i][1]+n;
if(tx>2||tx<0||ty<0||ty>3){
continue;
}
if(book[tx][ty]==1){
continue;
}
if(B[tx][ty]==1){
flag++;
dfs(tx,ty);
}
}
return;
}
void tond(int i){ //将i对应的值在B中的位置设为一(将数字转为二维数组3*4)
int x,y;
x=(i-1)/4;
y=(i-1)%4;
B[x][y]=1;
}
int main() {
for( A[0]=1;A[0]<=8;A[0]++){
for( A[1]=A[0]+1;A[1]<=9;A[1]++){
for(A[2]=A[1]+1;A[2]<=10;A[2]++){
for(A[3]=A[2]+1;A[3]<=11;A[3]++){
for( A[4]=A[3]+1;A[4]<=12;A[4]++){
memset(B,0,sizeof(B));
over=0;
memset(book,0,sizeof(book));
flag=1;
for(int o=0;o<5;o++){
tond(A[o]);
}
int sx=(A[0]-1)/4;
int sy=(A[0]-1)%4;
dfs(sx,sy);
}
}
}
}
}
printf("%d",ans);
return 0;
}