2016第七届蓝桥杯C/C++ B组省赛题解 F题

方格填数

如下的10个格子

   +--+--+--+
   |  |  |  |
+--+--+--+--+
|  |  |  |  |
+--+--+--+--+
|  |  |  |
+--+--+--+


填入0~9的数字。要求:连续的两个数字不能相邻。


(左右、上下、对角都算相邻)


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

使用全排列(stl)

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

int a[10];
int cot=0;
bool check(int i,int j){
   return abs(a[i]-a[j])!=1;
}
bool check(){
   return (check(0,1)&&check(0,3)&&check(0,4)&&check(0,5)&&check(1,2)
           &&check(1,4)&&check(1,5)&&check(1,6)&&check(2,5)&&check(2,6)&&check(3,4)&&check(3,7)&&
           check(3,8)&&check(4,5)&&check(4,7)&&check(4,8)&&check(4,9)
           &&check(5,6)&&check(5,8)&&check(5,9)&&check(6,9)&&check(7,8)&&check(8,9));
}
int main(){
    for(int i=0;i<10;i++)
        a[i]=i;
    do{
        cot=cot+check();
    }while(next_permutation(a, a + 10));

    cout << cot <<endl;
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值