zoukankan      html  css  js  c++  java
  • [蓝桥杯] 方格填数

    [蓝桥杯] 方格填数

    【题目描述 - Problem Description】

      如图,如下的10个格子,填入0~9的数字。要求:连续的两个数字不能相邻。


      (左右、上下、对角都算相邻)一共有多少种可能的填数方案?
      请填写表示方案数目的整数。

    【题解】

      不知道是不是题目没说清楚能不能重复,姑且当作不能重复吧(全排列大法好)
      暴力枚举……

    【最终结果】

    1580

    【代码 C++】

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 int data[10], map[10][10], fx[8][2] = { 0, 1, 1, 0, 0, -1, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1 };
     5 bool ts(int y, int x){
     6     int tmp1 = map[y][x] - 1, tmp2 = map[y][x] + 1, nxt, i;
     7     for (i = 0; i < 8; ++i){
     8         nxt = map[y + fx[i][0]][x + fx[i][1]];
     9         if (nxt == tmp1 || nxt == tmp2) return 1;
    10     }
    11     return 0;
    12 }
    13 int main() {
    14     int i, j, opt = 0;
    15     for (i = 0; i < 10; ++i) data[i] = i;
    16     memset(map, 0x7F, sizeof map);
    17     do{
    18         for (i = 0, j = 2; i < 3; ++i, ++j) map[1][j] = data[i];
    19         for (j = 1; j <= 4; ++i, ++j) map[2][j] = data[i];
    20         for (j = 1; j <= 4; ++i, ++j) map[3][j] = data[i];
    21 
    22         for (i = 2; i <= 4; ++i) if (ts(1, i)) goto ed;
    23         for (i = 1; i <= 4; ++i) if (ts(2, i)) goto ed;
    24         for (i = 1; i <= 3; ++i) if (ts(3, i)) goto ed;
    25         ++opt;
    26     ed:;
    27     } while (std::next_permutation(data, data + 10));
    28     printf("%d", opt);
    29     return 0;
    30 }
  • 相关阅读:
    CodeForces 1025G Company Acquisitions
    Luogu P4271 [USACO18FEB]New Barns P
    Luogu P1625 求和
    SP1772 Find The Determinant II
    CodeForces 1408G Clusterization Counting
    CodeForces 1420E Battle Lemmings
    设计模式——工厂模式
    设计模式——适配器模式
    rabbitMQ centos7 的安装
    dubbo spring 的使用
  • 原文地址:https://www.cnblogs.com/Simon-X/p/6516341.html
Copyright © 2011-2022 走看看