UVA_167_The Sultan's Successors

本文详细阐述了八皇后问题的解决方法,并通过特定的算法实现了问题的求解。进一步,文章提供了求解结果的计算过程及最终得分的展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<cmath>
#include<algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::stringstream;
using std::swap;
using std::sort;
using std::max;
bool check(const vector<int>&board, const int &row, const int &column)
{
    for (int i = 1; i < board.size(); i++)
    {
        //两个棋子在同一列
        if (board[i] == column)
        {
            return false;
        }
        //k=-1
        if ((row - i == column - board[i]) || (row - i == board[i] - column))
        {
            return false;
        }
    }
    return true;
}
void get(vector<vector<int>>&solution)
{
    for (int one = 1; one <= 8; one++)
    {
        vector<int>board(1);
        board.push_back(one);
        for (int two = 1; two <= 8; two++)
        {
            board.resize(2);
            if (check(board, 2, two))
            {
                board.push_back(two);
            }
            else
            {
                continue;
            }
            for (int three = 1; three <= 8; three++)
            {
                board.resize(3);
                if (check(board, 3, three))
                {
                    board.push_back(three);
                }
                else
                {
                    continue;
                }
                for (int four = 1; four <= 8; four++)
                {
                    board.resize(4);
                    if (check(board, 4, four))
                    {
                        board.push_back(four);
                    }
                    else
                    {
                        continue;
                    }
                    for (int five = 1; five <= 8; five++)
                    {
                        board.resize(5);
                        if (check(board, 5, five))
                        {
                            board.push_back(five);
                        }
                        else
                        {
                            continue;
                        }
                        for (int six = 1; six <= 8; six++)
                        {
                            board.resize(6);
                            if (check(board, 6, six))
                            {
                                board.push_back(six);
                            }
                            else
                            {
                                continue;
                            }
                            for (int seven = 1; seven <= 8; seven++)
                            {
                                board.resize(7);
                                if (check(board, 7, seven))
                                {
                                    board.push_back(seven);
                                }
                                else
                                {
                                    continue;
                                }
                                for (int eight = 1; eight <= 8; eight++)
                                {
                                    board.resize(8);
                                    if (check(board, 8, eight))
                                    {
                                        board.push_back(eight);
                                        solution.push_back(board);
                                    }
                                    else
                                    {
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
int calculate(const vector<vector<int>>&value,const vector<int>&board)
{
    int sum=0;
    for (int i = 1; i <= 8; i++)
    {
        sum += value[i][board[i]];
    }
    return sum;
}
int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt","w",stdout);
    vector<vector<int>>solution;
    get(solution);
    int T;
    while (cin >> T)
    {
        while (T--)
        {
            vector<vector<int>>value(9, (vector<int>)9);
            for (size_t i = 1; i <= 8; i++)
            {
                for (size_t j = 1; j <= 8; j++)
                {
                    int num; cin >> num;
                    value[i][j] = num;
                }
            }
            int maximum = 0;
            for (size_t i = 0; i < solution.size(); i++)
            {
                maximum = max(maximum, calculate(value, solution[i]));
            }
            stringstream stream; stream << maximum;
            string str; stream >> str;
            for (int n = 5; n > str.size(); n--)
            {
                cout << ' ';
            }
            cout << maximum << endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值