Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.0 kB
0
Indexable
Never
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int n_queens_max_sum(const vector<vector<int>>& board) {
    int n = 8;
    int r = n % 12;

    vector<int> even_numbers, odd_numbers;

    for (int i = 2; i <= n; i += 2) {
        even_numbers.push_back(i);
    }

    for (int i = 1; i <= n; i += 2) {
        odd_numbers.push_back(i);
    }

    if (r == 3 || r == 9) {
        even_numbers.erase(remove(even_numbers.begin(), even_numbers.end(), 2), even_numbers.end());
        even_numbers.push_back(2);
    }

    if (r == 8) {
        vector<int> temp;
        for (size_t i = 1; i < odd_numbers.size(); i += 2) {
            temp.push_back(odd_numbers[i]);
        }
        for (size_t i = 0; i < odd_numbers.size(); i += 2) {
            temp.push_back(odd_numbers[i]);
        }
        odd_numbers = temp;
    }

    if (r == 2) {
        swap(odd_numbers[0], odd_numbers[1]);
        odd_numbers.erase(remove(odd_numbers.begin(), odd_numbers.end(), 5), odd_numbers.end());
        odd_numbers.push_back(5);
    }

    if (r == 3 || r == 9) {
        odd_numbers.erase(remove(odd_numbers.begin(), odd_numbers.end(), 1), odd_numbers.end());
        odd_numbers.erase(remove(odd_numbers.begin(), odd_numbers.end(), 3), odd_numbers.end());
        odd_numbers.push_back(1);
        odd_numbers.push_back(3);
    }

    vector<int> column_indices = even_numbers;
    column_indices.insert(column_indices.end(), odd_numbers.begin(), odd_numbers.end());

    // Calculate the maximum sum
    int max_sum = 0;
    for (int row = 0; row < n; ++row) {
        int col = column_indices[row] - 1; 
        max_sum += board[row][col];
    }

    return max_sum;
}

int main() {
    vector<vector<int>> board(8, vector<int>(8));
    for (int i = 0; i < 8; ++i) {
        for (int j = 0; j < 8; ++j) {
            cin >> board[i][j];
        }
    }

    int max_sum = n_queens_max_sum(board);
    cout << max_sum << endl;

    return 0;
}
Leave a Comment