方格填数
如图,如下的10个格子,填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)一共有多少种可能的填数方案?
请填写表示方案数目的整数。
思路:这题方法很简单,暴力求解,依次向每个格子填数与已经填入的进行比较是否连续,即:是否两者之差的绝对值为1
注意:暴力解答时编程一定要仔细,因为代码量很大,太容易出错了,避免低级错误!!!!!!!!!!!!!!
#include <iostream>
#include <string>
#include <stdio.h>
#include <cmath>
using namespace std;
int main()
{
int a,b,c,d,e,f,g,h,i,j;
int sum=0;
for(a=0; a<10; a++)
{
for(b=0; b<10; b++)
{
if(b==a||abs(b-a)==1)
continue;
for(c=0; c<10; c++)
{
if(c==a||c==b || abs(c-b)==1 )
continue;
for(d=0; d<10; d++)
{
if(d==a||d==b||d==c || abs(d-a)==1)
continue;
for(e=0; e<10; e++)
{
if(e==a||e==b||e==c||e==d || abs(e-a)==1||abs(e-b)==1||abs(e-d)==1 )
continue;
for(f=0; f<10; f++)
{
if(f==a||f==b||f==c||f==d||f==e || abs(f-a)==1||abs(f-b)==1||abs(f-c)==1||abs(f-e)==1 )
continue;
for(g=0; g<10; g++)
{
if(g==a||g==b||g==c||g==d||g==e||g==f || abs(g-b)==1||abs(g-c)==1||abs(g-f)==1)
continue;
for(h=0; h<10; h++)
{
if(h==a||h==b||h==c||h==d||h==e||h==f||h==g|| abs(h-d)==1||abs(h-e)==1 )
continue;
for(i=0; i<10; i++)
{
if(i==a||i==b||i==c||i==d||i==e||i==f||i==g||i==h || abs(i-d)==1||abs(i-e)==1||abs(i-f)==1||abs(i-h)==1 )
continue;
for(j=0; j<10; j++)
{
if(j==a||j==b||j==c||j==d||j==e||j==f||j==g||j==h||j==i ||abs(j-e)==1||abs(j-f)==1||abs(j-g)==1||abs(j-i)==1 )
{
continue;
}
sum++;
}
}
}
}
}
}
}
}
}
}
cout<<sum;
}
/*
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int sum=0;
for(int a=0; a<=9; a++)
for(int b=0; b<=9; b++)
{
if(abs(b-a)==1||b==a) continue;
for(int c=0; c<=9; c++)
{
if(abs(c-b)==1||c==a||c==b) continue;
for(int d=0; d<=9; d++)
{
if(abs(d-a)==1||d==a||d==b||d==c) continue;
for(int e=0; e<=9; e++)
{
if(abs(e-a)==1||abs(e-b)==1||abs(e-d)==1||e==a||e==b||e==c||e==d) continue;
for(int f=0; f<=9; f++)
{
if(abs(f-a)==1||abs(f-b)==1||abs(f-c)==1||abs(f-e)==1||f==a||f==b||f==c||f==d||f==e) continue;
for(int g=0; g<=9; g++)
{
if(abs(g-b)==1||abs(g-c)==1||abs(g-f)==1||g==a||g==b||g==c||g==d||g==e||g==f) continue;
for(int h=0; h<=9; h++)
{
if(abs(h-d)==1||abs(h-e)==1||h==a||h==b||h==c||h==d||h==e||h==f||h==g) continue;
for(int i=0; i<=9; i++)
{
if(abs(i-d)==1||abs(i-e)==1||abs(i-f)==1||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<=9; j++)
{
if(abs(j-e)==1||abs(j-f)==1||abs(j-g)==1||abs(j-i)==1||j==a||j==b||j==c||j==d||j==e||j==f||j==g||j==h||j==i) continue;
sum++;
}
}
}
}
}
}
}
}
}
cout<<sum<<endl;
return 0;
}
*/
/*
#include <cstdio>
#include <queue>
#include <map>
#include <cmath>
#include <vector>
#include <cstring>
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
const int r = 3, c = 4;
int mapp[10][10];
int numv[15];
int cou;
int dir[4][2] = { 0, -1, -1, -1, -1, 0, -1, 1 };
bool check(int x, int y, int n)
{
for (int i = 0; i < 4; i++)
{
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (nx >= 0 && nx < r && ny >= 0 && ny < c)
{
//相邻的点有相邻的数字
//还没有填过数字的格子-10 肯定不会等于当前格子0-9 +1或-1
if (mapp[nx][ny] == n - 1 || mapp[nx][ny] == n + 1)
{
return false;
}
}
}
return true;
}
void dfs(int dep, int pos)
{
//(2,3)为缺口,结束
if (dep == 2 && pos == 3)
{
cou++;
#if 0
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
printf("%4d", mapp[i][j]);
}
puts("");
}
puts("");
system("pause");
#endif
return;
}
if (pos >= c)
{
dfs(dep + 1, 0);
}
else
{
for (int i = 0; i <= 9; i++)
{
//这个数i没用过,并且没有越出格子
if (!numv[i] && check(dep, pos, i))
{
numv[i] = true;
mapp[dep][pos] = i;
dfs(dep, pos + 1);
mapp[dep][pos] = -10;
numv[i] = false;
}
}
}
}
int main()
{
//初始化为一个比较大的负数,那么在
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j <= 5; j++)
{
mapp[i][j] = -10;
}
}
memset(numv, false, sizeof(numv));
cou = 0;
dfs(0, 1); //(0,0)为缺口,所以从(0,1)开始
cout << cou << endl; //1580
return 0;
}
*/