蓝桥杯2015国A——方格填数

官网

如下的10个格子

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

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

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

#include <iostream>
#include <cstring>
using namespace std;
int thearray[10];
int record[10] = {0};
int conflict[10][10] = {
  0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 
  1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 
  0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 
  0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 
  1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 
  1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 
  1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 
  0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 
  0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 
  0, 0, 0, 1, 1, 1, 0, 0, 1, 0};
int sum = 0;
bool func(int i)
{
  if(i == 10)
  {
    sum++;
    return 1;
  }
  for(int j = 0; j < 10; j++)
  {
    if(!record[j])
    {
      int tage = 0;
      for(int n = 0; n < i; n++)
      {
        if(conflict[n][i] && ((j == thearray[n] - 1) || (j == thearray[n] + 1)))
          tage++;
      }
      if(tage)
        continue;
      if(j == 10)
        return 0;
      thearray[i] = j;
      record[j] = 1;
      func(i + 1);
      record[j] = 0;
    }
  }
  return 0;
}
int main()
{
  memset(thearray, -1, sizeof(thearray));
  func(0);
  cout << sum <<endl;
  return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值