五星填数。

这篇博客探讨了如何使用深度优先搜索(DFS)和next_permutation()函数来解决一个数学问题:在五星图案中填入1到12的数字,排除7和11,使得每条直线上的数字和相等。博主提供了两种C++实现方法,并展示了代码细节。最终,博客给出了计算可能填法总数的结果。
摘要由CSDN通过智能技术生成

 在五星图案节点填上数字:1~12, 不包括7和11。

要求每条直线上数字和相等

 请搜索所有可能的填法有多少种。

 注意:旋转或镜像后相同的算同一种填法

如下图就是一个恰当的填法。


一、使用dfs算法

#include<iostream>
using namespace std;
const int N=10;
int star[N], num[N]={1, 2, 3, 4, 5, 6, 8, 9, 10, 12};
bool st[N];
int cnt=0, starcnt=0;

#define A star[0]+star[2]+star[5]+star[8]
#define B star[1]+star[2]+star[3]+star[4]
#define C star[0]+star[3]+star[7]+star[9]
#define D star[1]+star[5]+star[6]+star[9]
#define E star[4]+star[6]+star[7]+star[8]

void printstar()
{
    cout << "     " << star[0] << "\n";
    cout << star[1] << "  " << star[2] << "   "<< star[3] << "  " << star[4] << "\n";
    cout << "  " << star[5] << "     " << star[7] << "\n";
    cout << "     " << star[6] << "\n";
    cout << " " << star[8] << "     " << star[9] << "\n\n\n";
    return;
}

void dfs(int u)
{
    if(u==N)
    {
        cnt++;
        if((A==B) && (A==C) && (A==D) && (A==E))
        {
            printstar();
            starcnt++;
        }
        return;
    }

    for(int i=0; i<N; i++)
    {
        if(!st[i])
        {
            st[i]=1;
            star[u]=num[i];
            dfs(u+1);
            st[i]=0;
        }
    }
}

int main()
{
    dfs(0);
    cout << "total arrange =" << cnt << "\n";
    cout << " star num=" << starcnt/10 << "\n";
    return 0;
}

二、使用next_permutation()函数 

#include<iostream>
#include<algorithm>
using namespace std;
const int N=10;
int star[N]={1, 2, 3, 4, 5, 6, 8, 9, 10, 12};
int cnt=0, starcnt=0;

#define A star[0]+star[2]+star[5]+star[8]
#define B star[1]+star[2]+star[3]+star[4]
#define C star[0]+star[3]+star[7]+star[9]
#define D star[1]+star[5]+star[6]+star[9]
#define E star[4]+star[6]+star[7]+star[8]

void printstar()
{
    cout << "     " << star[0] << "\n";
    cout << star[1] << "  " << star[2] << "   "<< star[3] << "  " << star[4] << "\n";
    cout << "  " << star[5] << "     " << star[7] << "\n";
    cout << "     " << star[6] << "\n";
    cout << " " << star[8] << "     " << star[9] << "\n\n\n";
    return;
}

int main()
{
    do
    {
        cnt++;
        if(A==B && A==C && A==D && A==E)
        {
            printstar();
            starcnt++;
        }
    }while(next_permutation(star, star+ N));

    cout << "total arrange =" << cnt << "\n";
    cout << " star num=" << starcnt/10 << "\n";
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,第一个引用是关于如何使用Python实现五星红旗的源码,第二个引用是关于五星填数的问题。请问您需要了解哪一个问题的Python实现呢?如果您需要了解五星填数的Python实现,可以参考以下代码: ```python from itertools import permutations # 定义五星图案的节点 nodes = [ [0, 0], [1, 2], [2, 1], [2, 3], [1, 4], [3, 4], [4, 3], [4, 1], [3, 2], [4, 0], [2, 0], [1, 1] ] # 定义五条直线 lines = [ [0, 1, 2, 3, 4], [4, 5, 6, 7, 8], [8, 9, 10, 11, 0], [1, 7, 9, 2, 10], [3, 6, 11, 5, 8] ] # 定义每条直线上数字和相等的函数 def check_sum(line, nums): return sum([nums[i] for i in line]) == sum([nums[i] for i in lines[0]]) # 定义五星填数的函数 def five_star_fill(): # 生成1~12的数字排列 nums = list(permutations(range(1,13), 12)) # 去掉包含7和11的数字排列 nums = [n for n in nums if 7 not in n and 11 not in n] # 遍历所有数字排列 for n in nums: # 判断每条直线上数字和是否相等 if all([check_sum(line, n) for line in lines]): # 输出符合条件的数字排列 print(n) break # 调用五星填数函数 five_star_fill() ``` 运行以上代码,即可输出符合条件的数字排列。该代码使用了Python的itertools模块中的permutations函数,生成了1~12的数字排列,并使用了列表推导式去掉了包含7和11的数字排列。然后遍历所有数字排列,判断每条直线上数字和是否相等,输出符合条件的数字排列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值