蓝桥杯 : 方格填数

版权声明:欢迎大佬批评指正!O(∩_∩)O https://blog.csdn.net/wyh1618/article/details/88376241


方格填数

如下的10个格子
   +--+--+--+
   |  |  |  |
+--+--+--+--+
|  |  |  |  |
+--+--+--+--+
|  |  |  |
+--+--+--+

(如果显示有问题,也可以参看【图1.jpg】)

填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)

一共有多少种可能的填数方案?

请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。


答案: 1580

暴力美学。

万无一失的做法。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
	int n = 0;
	for(int a=0;a<10;a++){
		for(int b=0;b<10;b++){
			for(int c=0;c<10;c++){
				for(int d=0;d<10;d++){
					for(int e=0;e<10;e++){
						for(int f=0;f<10;f++){
							for(int g=0;g<10;g++){
								for(int h=0;h<10;h++){
									for(int i=0;i<10;i++){
										for(int j=0;j<10;j++){
											if( (abs(a-b) > 1) && (abs(a-d) > 1) && (abs(a-e) > 1) && (abs(a-f) > 1)
											&& (abs(b-c) > 1) && (abs(b-e) > 1) && (abs(b-f) > 1) && (abs(b-g) > 1)
											&& (abs(c-f) > 1) && (abs(c-g) > 1)
											&& (abs(d-e) > 1) && (abs(d-h) > 1) && (abs(d-i) > 1)
											&& (abs(e-f) > 1) && (abs(e-h) > 1) && (abs(e-i) > 1) && (abs(e-j) > 1)
											&& (abs(f-g) > 1) && (abs(f-i) > 1) && (abs(f-j) > 1) 
											&& (abs(g-j) > 1) && (abs(h-i) > 1) && (abs(i-j) > 1)){
												if((a!=b)&&(a!=c)&&(a!=d)&&(a!=e)&&(a!=f)&&(a!=g)&&(a!=h)&&(a!=i)&&(a!=j)
												&&(b!=c)&&(b!=d)&&(b!=e)&&(b!=f)&&(b!=g)&&(b!=h)&&(b!=i)&&(b!=j)
												&&(c!=d)&&(c!=e)&&(c!=f)&&(c!=g)&&(c!=h)&&(c!=i)&&(c!=j)
												&&(d!=e)&&(d!=f)&&(d!=g)&&(d!=h)&&(d!=i)&&(d!=j)
												&&(e!=f)&&(e!=g)&&(e!=h)&&(e!=i)&&(e!=j)
												&&(f!=g)&&(f!=h)&&(f!=i)&&(f!=j)
												&&(g!=h)&&(g!=i)&&(g!=j)
												&&(h!=i)&&(h!=j)
												&&(i!=j)){
													n++;
													cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f<<" "<<g<<" "<<h<<" "<<i<<" "<<j<<" "<<endl; 
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	cout<<n<<endl;
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/wyh1618/article/details/88376241