UVA_167_The Sultan's Successors

来源:互联网 发布:游戏优化怎么做 编辑:程序博客网 时间:2024/06/06 12:36
#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;}

0 0