Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
4.5 kB
1
Indexable
Never
#include <iostream>
#include <vector>

using namespace std;

int calculate_board_size(const vector<char>& board) {
    int max_width = 0;
    int current_width = 0;
    for (char cell : board) {
        if (cell == '<') {
            current_width++;
        }
        else if (cell == '\n') {
            if (current_width > max_width) {
                max_width = current_width;
            }
            current_width = 0;
        }
    }
    return max_width;
}

int calculate_pawns_number(const vector<char>& board) {
    int red_number = 0;
    int blue_number = 0;
    for (char cell : board) {
        if (cell == 'r') {
            red_number++;
        }
        else if (cell == 'b') {
            blue_number++;
        }
    }
    return red_number + blue_number;
}

int is_board_correct(const vector<char>& board) {
    int red_number = 0;
    int blue_number = 0;
    for (char cell : board) {
        if (cell == 'r') {
            red_number++;
        }
        else if (cell == 'b') {
            blue_number++;
        }
    }
    if (blue_number > red_number) {
        return 1;
    }
    else if (red_number > blue_number + 1) {
        return 1;
    }
    else {
        return 2;
    }
}

vector< int > get_neighbors( const vector< char >& board, int current, int boardSize )
{
    vector< int > neighbors;

    int up = current - boardSize - 1;
    int down = current + boardSize + 1;
    int left = current - 1;
    int right = current + 1;
    int up_left = current - boardSize;
    int down_right = current + boardSize;

    if (up >= 0 && up % (boardSize + 1) != boardSize - 1) {
        neighbors.push_back(up);
    }
    if (down < board.size() && down % (boardSize + 1) != 0) {
        neighbors.push_back(down);
    }
    if (left >= 0 && left % (boardSize + 1) != boardSize - 1) {
        neighbors.push_back(left);
    }
    if (right < board.size() && right % (boardSize + 1) != 0) {
        neighbors.push_back(right);
    }
    if (up_left >= 0 && up_left % (boardSize + 1) != boardSize - 1) {
        neighbors.push_back(up_left);
    }
    if (down_right < board.size() && down_right % (boardSize + 1) != 0) {
        neighbors.push_back(down_right);
    }

    return neighbors;
}

bool dfs( const vector< char >& board, int current, vector< bool >& visited, int boardSize, char color)
{
    visited[ current ] = true;

    if( ( color == 'r' && current % (boardSize + 1) == boardSize - 1 ) ||
        ( color == 'b' && current / (boardSize + 1) == boardSize - 1 ) )
    {
        return true;
    }

    vector<int> neighbors = get_neighbors(board, current, boardSize);
    for (int neighbor : neighbors) {
        if (!visited[neighbor] && board[neighbor] == color) {
            if (dfs(board, neighbor, visited, boardSize, color)) {
                return true;
            }
        }
    }

    return false;
}

int is_game_over(const vector<char>& board)
{
    int boardSize = calculate_board_size(board);

        /* 1-czerwone 2-niebieskie 3-żaden */

    vector<bool> visited(board.size(), false);
    for (int i = 0; i < boardSize; ++i) {
        if (board[i] == 'r' && !visited[i]) {
            if (dfs(board, i, visited, boardSize, 'r')) {
                return 1;
            }
        }
    }

    visited.assign(board.size(), false);
    for (int i = 0; i < board.size(); ++i) {
        if (board[i] == 'b' && !visited[i]) {
            if (dfs(board, i, visited, boardSize, 'b')) {
                return 2;
            }
        }
    }

    return 3;
}

int main() {
    vector<char> board;
    char cell;
    while (cin.get(cell) && cell != 'I') {
        board.push_back(cell);
    }


    int calculated_size = calculate_board_size(board);
    int calculated_pawns = calculate_pawns_number(board);

    cout << calculated_size << endl << " " << endl;
    cout << calculated_pawns << endl << " " << endl;

    if (is_board_correct(board) == 1) {
        cout << "NO" << endl << " " << endl;
    }
    else {
        cout << "YES" << endl << " " << endl;
    }

    int game_result = is_game_over(board);
    if (game_result == 1) {
        cout << "YES RED" << endl << " " << endl;
    }
    else if (game_result == 2) {
        cout << "YES BLUE" << endl << " " << endl;
    }
    else {
        cout << "NO" << endl << " " << endl;
    }

    return 0;
}
Leave a Comment