Untitled

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

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 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 możliwe ruchy: lewo, prawo, góra, dół, lewy górny skos, prawy dolny skos
        int dx[] = { 0, 0, -1, 1, -1, 1 };
        int dy[] = { -1, 1, 0, 0, -1, 1 };
        for (int i = 0; i < 6; ++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
        // Sprawdzamy możliwe ruchy: lewo, prawo, góra, dół, lewy górny skos, prawy dolny skos
        int dx[] = { 0, 0, -1, 1, -1, 1 };
        int dy[] = { -1, 1, 0, 0, -1, 1 };
        for (int i = 0; i < 6; ++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 Answer(const string& question)
{
    if (question == "OARD_SIZE")
    {
        return 1;
    }
    else if (question == "AWNS_NUMBER")
    {
        return 2;
    }
    else if (question == "S_BOARD_CORRECT")
    {
        return 3;
    }
    else if (question == "S_GAME_OVER")
    {
        return 4;
    }
    else
    {
        return 5;
    }
}

int main()
{
    vector< char > board;
    string question;
    char cell;

    while( true )
    {
        while( cin.get( cell ) && cell != 'I' && cell != 'B' && cell != 'P' )
        {
            board.push_back( cell );
        }

        // Sprawdź, czy wczytywanie zakończyło się z powodu końca pliku (EOF)
        if (cin.eof())
        {
            // Zakończ pętlę, jeśli osiągnięto koniec pliku
            break;
        }
        else if (cin.fail())
        {
            // Obsługa niepowodzenia wczytywania danych
            cerr << "Błąd wczytywania danych." << endl;
            break; // Przerwij pętlę w przypadku błędu wczytywania
        }
        
        getline(cin, question);

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

        if( Answer( question ) == 1 )
        {
            cout << calculated_size << endl;
        }
        else if( Answer( question ) == 2 )
        {
            cout << calculated_pawns << endl;
        }
        else if( Answer( question ) == 3 )
        {
            if( is_board_correct( board ) == 1 )
            {
                cout << "NO" << endl;
            }
            else
            {
                cout << "YES" << endl;
            }
        }
        else if( Answer( question ) == 4 )
        {
            if( game_result == 1 )
            {
                cout << "YES RED" << endl;
            }
            else if( game_result == 2 )
            {
                cout << "YES BLUE" << endl;
            }
            else
            {
                cout << "NO" << endl;
            }
        }
//        else if (Answer(question) == 5)
//        {
//            int game_result = is_game_over(board);
//            if (game_result == 3 && is_board_correct(board) == 1)
//            {
//                cout << "NO" << endl;
//            }
//            else if (game_result == 3 && is_board_correct(board) == 2)
//            {
//                cout << "YES" << endl;
//            }
//            else if (game_result == 1)
//            {
//                int red_number = 0;
//                int blue_number = 0;
//                for (char b : board) {
//                    if (b == 'r') {
//                        red_number++;
//                    }
//                    else if (b == 'b') {
//                        blue_number++;
//                    }
//                }
//                if (red_number == blue_number + 1)
//                {
//                    cout << "YES" << endl;
//                }
//                else
//                {
//                    cout << "NO" << endl;
//                }
//            }
//            else if (game_result == 2)
//            {
//                int red_number = 0;
//                int blue_number = 0;
//                for (char b : board) {
//                    if (b == 'r') {
//                        red_number++;
//                    }
//                    else if (b == 'b') {
//                        blue_number++;
//                    }
//                }
//                if (red_number == blue_number)
//                {
//                    cout << "YES" << endl;
//                }
//                else
//                {
//                    cout << "NO" << endl;
//                }
//            }
//        }

        question.clear();
        board.clear();
    }

    return 0;
}
Leave a Comment