蓝桥杯试题(二)----------方格填数

如图,如下的10个格子,填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)一共有多少种可能的填数方案?
请填写表示方案数目的整数。
1014426-20170313201722151-1872543254.png
我用的是暴力破解法虽然写起来复杂些 但是便于理解。代码如下:

public class A
{
private static int count = 0;
public static void main(String args[]){
for (int a=0;a<10;a++){
for (int b = 0;b<10;b++){
if (Math.abs(a-b)==1||b==a)continue;
for (int c = 0;c<10;c++){
if (Math.abs(c-b)==1||c==b||c==a)continue;
for (int d = 0;d<10;d++){
if (Math.abs(d-a)==1||d==a||d==b||d==c)continue;
for (int e=0;e<10;e++){
if (Math.abs(e-d)==1||Math.abs(e-a)==1||Math.abs(e-b)==1||e==a||e==b||e==c||e==d)continue;
for (int f=0;f<10;f++){
if (Math.abs(f-e)==1||Math.abs(f-a)==1||Math.abs(f-b)==1||Math.abs(f-c)==1||
f==a||f==b||f==c||f==d||f==e)continue;
for (int g=0;g<10;g++){
if (Math.abs(g-b)==1||Math.abs(g-c)==1||Math.abs(g-f)==1||
g==a||g==b||g==c||g==d||g==e||g==f)continue;
for (int h=0;h<10;h++){
if (Math.abs(h-d)==1||Math.abs(h-e)==1||
h==a||h==b||h==c||h==e||h==d||h==f||h==g)continue;
for (int i=0;i<10;i++){
if (Math.abs(i-d)==1||Math.abs(i-e)==1||Math.abs(i-f)==1||Math.abs(i-h)==1||
i==a||i==b||i==c||i==d||i==e||i==f||i==g||i==h)continue;
for (int j=0;j<10;j++){
if (Math.abs(j-e)==1||Math.abs(j-f)==1||Math.abs(j-g)==1||Math.abs(j-i)==1||
j==a||j==b||j==c||j==d||j==e||j==f||j==g||j==h||j==i)continue;
count++;
}
}
}
}
}
}
}
}
}
}
System.out.print(count);
}
}

转载于:https://www.cnblogs.com/nbdedu/p/6544831.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值