六角填数



标题:六角填数


    如图所示六角形中,填入1~12的数字。


    使得每条直线上的数字之和都相同。


    图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少?


请通过浏览器提交答案,不要填写多余的内容。

#include <stdio.h>
#include <math.h>
#include <string.h>
#define N 15 
int a[N];
int  book[N];
 
void dfs(int x)
{
    if(x == 1 || x == 2 || x == 12)
	{
        dfs(x+1);
        return ;
    }
    if(x > 12)
	{
        int t[6];
        t[0] = a[1] + a[3] + a[6] + a[8];
        t[1] = a[1] + a[4] + a[7] + a[11];
        t[2] = a[2] + a[3] + a[4] + a[5];
        t[3] = a[2] + a[6] + a[9] + a[12];
        t[4] = a[8] + a[9] + a[10] + a[11];
        t[5] = a[12] + a[10] + a[7] + a[5];
        
        for(int i = 1; i < 6; ++i)
		{
            if(t[i] != t[i-1])
			{
				return ;
			}
        }
        printf("%d\n",a[6]);
        return ;
    }
 
    for(int i = 1;i < 13; ++i)
	{
        if(book[i]==0)
		{
            book[i] = 1;
            a[x] = i;
            dfs(x+1);
            book[i] = 0;
        }
    } 
}
 
int main()
{
    memset(book,0,sizeof(book));
    book[1] = 1;
    a[1] = 1;
    book[8] = 1;
    a[2] = 8;
    book[3] = 1;
    a[12] =3; 
    dfs(1); 
	return 0;
}

最后结果是:10

思想:dfs。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值