算法提高 8皇后·改 蓝桥杯C语言(回溯法)

/*
  算法提高 8皇后·改  
问题描述
  规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大。
输入格式
  一个8*8的棋盘。
输出格式
  所能得到的最大数字和
样例输入
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
48 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64
样例输出
260
数据规模和约定
  棋盘上的数字范围0~99

98 72 40 88 27 70 90 30
4 94 36 12 60 93 43 33
69 73 84 27 6 67 28 46
92 7 60 16 63 21 1 46
99 94 62 45 27 53 71 77
8 19 82 14 40 81 24 52
53 12 83 80 77 57 65 95
44 86 99 75 84 12 59 0

603

99 92 53 74 69 76 87 98
 9 12 11 12 19 14 15 16
17 14 19 20 29 22 23 24
25 26 57 28 29 30 31 32
33 34 36 76 39 58 39 40
 1 42 43 44 85 46 47 48
58 60 71 82 53 34 55 56
57 58 39 90 61 32 23 44

429

*/
#include <stdio.h>

typedef
    struct
    {
        int sz;
        int bj;
    } QP_t ;

void input( QP_t [][8], int );
void output( QP_t [][8], int );

int main(void)
{
    QP_t a[8][8];
    input( a , 8 );
    output( a , 8 );
    return 0;
}

void output( QP_t a[][8], int n )
{
    int i;
    for( i = 0 ; i < n ; i ++ )
    {
        int j;
        for( j = 0 ; j < n ; j ++ )
        {
            printf("%d ", a[i][j] );
        }
        putchar('\n');
    }
}

void input( QP_t a[][8], int n)
{
    int i;
    for( i = 0 ; i < n ; i ++ )
    {
        int j;
        for( j = 0 ; j < n ; j ++ )
        {
            scanf("%d", &a[i][j].sz );
            a[i][j].bj = 1;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40990854/article/details/80078711