Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
7.1 kB
2
Indexable
Never
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <tuple>

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;
    }
}

map< int, string > getDiagonals( const vector<char>& board )
{
    map< int, string > diagonals;
    int key = 1;
    int underHalfBoard = 0;

    int boardSize = calculate_board_size( board );
    int row = 0;
    int middleRow = boardSize;

    for( unsigned int i = 0; i < board.size(); ++i )
    {
        if( board[ i ] == '-' )
        {
            continue;
        }
        else if( board[ i ] == '\n' )
        {
            ++row;
            if( row <= middleRow )
            {
                key = 1;
            }
            else
            {
                ++underHalfBoard;
                key = 1 + underHalfBoard;
            }
        }
        else if( board[ i ] == '<' )
        {
            if( board[ i + 2 ] == 'r' or board[ i + 2 ] == 'b' )
            {
                diagonals[ key ] += board[ i + 2 ];
            }
            else if( board[ i + 2 ] == ' ' )
            {
                diagonals[ key ] += 'x'; // zamiast spacji wstawiamy znak x
            }
            ++key;
            i += 4;
        }
    }

    // Kontrolne wypisanie
//    for( const auto& element : diagonals )
//    {
//        cout << element.first << " -> " << element.second << endl;
//    }

    return diagonals;
}

bool is_valid( int x, int y, int rows, int cols )
{
    return x >= 0 && x < rows && y >= 0 && y < cols;
}

bool hasBluePath(vector<vector<char>>& board) {
    size_t rows = board.size();
    size_t cols = board[ 0 ].size();

    // Tworzymy kolejkę do przechowywania współrzędnych
    queue< tuple< int, int > > q;

    // Rozpoczynamy od wszystkich komórek w pierwszej kolumnie, które mają wartość 'b'
    for( int i = 0; i < rows; ++i )
    {
        if( board[ i ][ 0 ] == 'b' )
        {
            q.emplace( i, 0 );
        }
    }

    // Tablica do śledzenia odwiedzonych komórek
    vector< vector< bool > > visited( rows, vector< bool >( cols, false ) );

    while( !q.empty() )
    {
        auto[x, y] = q.front();
        q.pop();

        // Jeśli dotarliśmy do prawej kolumny, zwracamy true
        if( y == cols - 1 )
        {
            return true;
        }

        // Sprawdzamy wszystkie możliwe ruchy w prawo, w dół i w górę
        int dx[] = {0, 1, -1};
        int dy[] = {1, 0, 0};
        for( int i = 0; i < 3; ++i )
        {
            int new_x = x + dx[ i ];
            int new_y = y + dy[ i ];
            if( new_x >= 0 && new_x < rows && new_y >= 0 && new_y < cols &&
                !visited [new_x ][ new_y ] && board[ new_x ][ new_y ] == 'b')
            {
                visited[new_x][new_y] = true;
                q.emplace(new_x, new_y);
            }
        }
    }

    return false;
}

bool hasRedPath(vector< vector< char > >& board )
{
    size_t rows = board.size();
    size_t cols = board[0].size();

    queue< tuple< int, int > > q;

    // Rozpoczynamy od pierwszej kolumny
    for( int j = 0; j < cols; ++j )
    {
        if( board[ 0 ][ j ] == 'r' )
        {
            q.emplace( 0, j );
        }
    }

    // Tablica do śledzenia odwiedzonych komórek
    vector< vector< bool > > visited( rows, vector< bool >( cols, false ) );

    while( !q.empty() )
    {
        auto[ x, y ] = q.front();
        q.pop();

        // Jeśli dotarliśmy do ostatniego wiersza, zwracamy true
        if( x == rows - 1 )
        {
            return true;
        }

        // Sprawdzamy wszystkie możliwe ruchy w dół i w bok
        int dx[] = { 1, 0 };
        int dy[] = { 0, 1 };
        for( int i = 0; i < 2; ++i )
        {
            int new_x = x + dx[ i ];
            int new_y = y + dy[ i ];
            if( new_x >= 0 && new_x < rows && new_y >= 0 && new_y < cols &&
                !visited[ new_x ][ new_y ] && board[ new_x ][ new_y ] == 'r' )
            {
                visited[ new_x ][ new_y ] = true;
                q.emplace( new_x, new_y );
            }
        }
    }

    return false;
}

int is_game_over(const vector<char>& board)
{
    map< int, string > diagonals = getDiagonals( board );

    vector< vector< char > > board2D;
    for( const auto& element : diagonals )
    {
        vector< char > pawns;
        for( const auto& s : element.second )
        {
            pawns.push_back( s );
        }
        board2D.push_back( pawns );
    }

    // Kontrolne wypisanie
    for( const auto& element : board2D )
    {
        for( const auto& e : element )
        {
            cout << e << " ";
        }
        cout << endl;
    }

    /* 1-czerwone 2-niebieskie 3-żaden */
    if( hasBluePath( board2D ) )
    {
        return 2;
    }

    if( hasRedPath( board2D ) )
    {
        return 1;
    }

    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