题目描述
如下的10个格子,填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)一共有多少种可能的填数方案?
输出
请填写表示方案数目的整数。
老暴力了,我称之为V暴算法
a = [0]*10
summ=0
for x1 in range(10):
a[x1]=1
for x2 in range(10):
if a[x2]==0 and abs(x2-x1)!=1:
a[x2]=1
for x3 in range(10):
if a[x3]==0 and abs(x2-x3)!=1:
a[x3]=1
for x4 in range(10):
if a[x4]==0 and abs(x4-x1)!=1:
a[x4]=1
for x5 in range(10):
if a[x5]==0 and abs(x5-x4)!=1 and abs(x5-x1)!=1 and abs(x2-x5)!=1:
a[x5]=1
for x6 in range(10):
if a[x6]==0 and abs(x6-x1)!=1 and abs(x2-x6)!=1 and abs(x6-x3)!=1 and abs(x6-x5)!=1:
a[x6]=1
for x7 in range(10):
if a[x7]==0 and abs(x2-x7)!=1 and abs(x7-x3)!=1 and abs(x7-x6)!=1:
a[x7]=1
for x8 in range(10):
if a[x8]==0 and abs(x8-x4)!=1 and abs(x8-x5)!=1:
a[x8]=1
for x9 in range(10):
if a[x9]==0 and abs(x9-x4)!=1 and abs(x9-x5)!=1 and abs(x9-x6)!=1 and abs(x9-x8)!=1:
a[x9]=1
for x10 in range(10):
if a[x10]==0 and abs(x10-x5)!=1 and abs(x10-x6)!=1 and abs(x10-x7)!=1 and abs(x10-x9)!=1:
a[x10]=1
summ+=1
a[x10]=0
a[x9]=0
a[x8]=0
a[x7]=0
a[x6]=0
a[x5]=0
a[x4]=0
a[x3]=0
a[x2]=0
a[x1]=0
print(summ)