填入0~9的數字。要求:連續的兩個數字不能相鄰。
(左右、上下、對角都算相鄰)
一共有多少種可能的填數方案?
請填寫表示方案數目的整數。
注意:你提交的應該是一個整數,不要填寫任何多餘的內容或說明性文字。
1580
#include <cstdio>
#include <algorithm>
using namespace std;
int a[12]={-2,0,1,2,3,4,5,6,7,8,9,-2};
int dx[8]={1,1,1,0,-1,-1,-1,0};
int dy[8]={-1,0,1,1,1,0,-1,-1};
int s=0;
bool f() {
for (int i=1;i<=10;i++) {
int x=i/4;
int y=i%4;
for (int j=0;j<8;j++) {
int xx=x+dx[j];
int yy=y+dy[j];
int z=xx*4+yy;
if (xx>=0 && yy>=0 && xx<3 && yy<4) {
if (abs(a[z]-a[i])==1) return false;
}
}
}
return true;
}
int main()
{
do {
if (f()) s++;
} while (next_permutation(a+1,a+11));
printf ("%d\n",s);
return 0;
}