16蓝桥试题之方格填数

方格填数

如图,如下的10个格子,填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)一共有多少种可能的填数方案?
请填写表示方案数目的整数。


思路:我们把这样的一个区域的每一块进行命名,如下图

    a   b  c

d  e   f   g

h   i   j

我们只要保证相邻的区域的差的绝对值不等于1即可 并且数不能重复使用,代码如下

package 省赛试题2016;

public class 方格填数_填空4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a,b,c,d,e,f,g,h,i,j;
		int count = 0;
		for(a=0;a<=9;a++){
			for(b=0;b<=9;b++){
				if(Math.abs(a-b)==1||a==b) continue;
				for(c=0;c<=9;c++){
					if(Math.abs(c-b)==1||a==c||b==c) continue;
					for(d=0;d<=9;d++){
						if(Math.abs(d-a)==1||a==d||b==d||c==d) continue;
						for(e=0;e<=9;e++){
							if(Math.abs(e-d)==1||Math.abs(e-a)==1||Math.abs(e-b)==1||a==e||b==e||c==e||d==e) continue;
							for(f=0;f<=9;f++){
								if(Math.abs(f-e)==1||Math.abs(f-a)==1||Math.abs(f-b)==1||Math.abs(f-c)==1||a==f||b==f||c==f||d==f||e==f) continue;
								for(g=0;g<=9;g++){
									if(Math.abs(g-c)==1||Math.abs(g-f)==1||Math.abs(g-b)==1||a==g||b==g||c==g||d==g||e==g||f==g) continue;
									for(h=0;h<=9;h++){
										if(Math.abs(h-d)==1||Math.abs(h-e)==1||a==h||b==h||c==h||d==h||e==h||f==h||g==h) continue;
										for(i=0;i<=9;i++){
											if(Math.abs(i-h)==1||Math.abs(i-d)==1||Math.abs(i-e)==1||Math.abs(i-f)==1||a==i||b==i||c==i||d==i||e==i||f==i||g==i||h==i) continue;
											for(j=0;j<=9;j++){
												if(Math.abs(j-i)==1||Math.abs(j-f)==1||Math.abs(j-e)==1||Math.abs(j-g)==1||a==j||b==j||c==j||d==j||e==j||f==j||g==j||h==j||i==j) continue;
												count++;
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		System.out.println(count);
	}

}

就是这么暴力,最后的答案是1580。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码敌敌畏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值