方格填空蓝桥杯

如下的10个格子
在这里插入图片描述
填入0〜9的数字要求:连续的两个数字不能相邻。(左右,上下,对角都算相邻)一共有多少种可能的填数方案?
Input

Output

暴力就完事了

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int p(int x,int y)
{
    if(x<y)
        return y-x;
    return x-y;
}
int pan(int a,int b,int c,int d,int e,int f,int g,int h,int i,int j)
{
    if(p(a,b)==1||p(a,e)==1||p(a,d)==1||p(a,f)==1||p(b,c)==1||p(b,f)==1||p(b,e)==1||p(b,g)==1||p(c,f)==1||p(c,g)==1||p(d,e)==1||p(d,h)==1||p(d,i)==1||p(e,f)==1||p(e,h)==1||p(e,i)==1||p(e,j)==1||p(f,g)==1||p(f,i)==1||p(f,j)==1||p(h,i)==1||p(i,j)==1||p(g,j)==1)
        return 0;
    return 1;
}
int main()
{
    int a,b,c,d,e,f,g,h,i,j,s=0;
    for(a=0; a<10; a++)
        for(b=0; b<10; b++)
        {
            if(b==a)
                continue;
            for(c=0; c<10; c++)
            {
                if(c==a||c==b)
                    continue;
                for(d=0; d<10; d++)
                {
                    if(d==a||d==b||d==c)
                        continue;
                    for(e=0; e<10; e++)
                    {
                        if(e==a||e==b||e==c||e==d)
                            continue;
                        for(f=0; f<10; f++)
                        {
                            if(f==a||f==b||f==c||f==d||f==e)
                                continue;
                            for(g=0; g<10; g++)
                            {
                                if(g==a||g==b||g==c||g==d||g==e||g==f)
                                    continue;
                                for(h=0; h<10; h++)
                                {
                                    if(h==a||h==b||h==c||h==d||h==e||h==f||h==g)
                                        continue;
                                    for(i=0; i<10; i++)
                                    {
                                        if(i==a||i==b||i==c||i==d||i==e||i==f||i==g||i==h)
                                            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)
                                                continue;
                                            if(pan(a,b,c,d,e,f,g,h,i,j)==1)
                                                s++;//cout<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<e<<' '<<f<<' '<<g<<' '<<h<<' '<<i<<' '<<j<<endl;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    cout<<s;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43589396/article/details/88741168
今日推荐