zoukankan      html  css  js  c++  java
  • 第七届蓝桥杯省赛6:方格填数


    方格填数

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

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

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

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

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

    图1.jpg

    next_permutation很好用

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int a[12]={-2,0,1,2,3,4,5,6,7,8,9,-2};
    int dx[8]={1,1,1,0,-1,-1,-1,0};
    int dy[8]={-1,0,1,1,1,0,-1,-1};
    int res;
    bool judge()
    {
        for(int i=1;i<=10;i++)
        {
            int y=i/4;
            int x=i%4;
            for(int k=0;k<8;k++)
            {
                int ny=y+dy[k];
                int nx=x+dx[k];
                int j=ny*4+nx;
                if(0<=ny&&ny<3&&0<=nx&&nx<4)
                {    
                    if(abs(a[j]-a[i])==1)
                        return false;
                }
            }
        }
        return true;
    }
    int main()
    {
        do{
            if(judge())
                res++;
        }while(next_permutation(a+1,a+11));
        
        printf("%d
    ",res);
        
        return 0;
    }
    /*
    res:1580
    */
  • 相关阅读:
    HDU_2191_多重背包
    HDU_1494_dp
    POJ_1088_dfs
    所有的畅通工程[HDU1232][HDU1874][HDU1875][HDU1879]
    畅通工程[HDU1863]
    还是畅通工程[HDU1233]
    最小生成树
    Who's in the Middle[HDU1157]
    Bungee Jumping[HDU1155]
    Is It A Tree?[HDU1325][PKU1308]
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5321306.html
Copyright © 2011-2022 走看看