Cod sursa(job #2771769)

Utilizator Gabryel9898Bizdoc Vasile Gabriel Gabryel9898 Data 29 august 2021 04:51:08
Problema Jocul Flip Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.78 kb
#include <iostream>
#include <fstream>
#include <vector>


class Board {
private:
    std::vector<std::vector<int16_t>> board;
public:
    explicit Board(std::vector<std::vector<int16_t>> matrix) {
        board = std::move(matrix);
    }

    void flip_line(int16_t number) {
        for (auto &col: board[number]) {
            col *= -1;
        }
    }

    void flip_column(int16_t number) {
        for (auto &row: board) {
            row[number] *= -1;
        }
    }

    void print_matrix() {
        std::cout << "\n<------------------->\n";

        for (const auto &row: board) {
            for (const auto &col: row) {
                std::cout << col << " ";
            }
            std::cout << '\n';
        }
    }

    int32_t get_sum() {
        int32_t sum = 0;

        for (const auto &row: board) {
            for (const auto &col: row) {
                sum += col;
            }
        }

        return sum;
    }

    int32_t sum_line(int16_t number) {
        int32_t sum = 0;

        for (const auto &col: board[number]) {
            sum += col;
        }

        return sum;
    }

    int32_t sum_col(int16_t number) {
        int32_t sum = 0;

        for (const auto &row: board) {
            sum += row[number];
        }

        return sum;
    }

    void print_sum() {
        std::cout << '\n' << get_sum() << '\n';
    }
};

int flip_algo() {
    std::string input_file_name("../flip.in");
    std::string output_file_name("../flip.out");

    std::ifstream input_file;
    input_file.open(input_file_name);

    if (!input_file.is_open()) {
        std::cerr << "Could not open the file: '" << input_file_name << "'" << std::endl;
        return EXIT_FAILURE;
    }

    int16_t M, N;
    input_file >> M >> N;

    auto matrix = std::vector<std::vector<int16_t>>(M);

    for (auto &row: matrix) {
        row = std::vector<int16_t>(N);

        for (auto &col: row) {
            input_file >> col;
        }
    }

    input_file.close();

    auto game = new Board(matrix);

    bool update = true;
    do {
        update = false;

        for (int16_t i = 0; i < M; i++) {
            auto line_sum = game->sum_line(i);
            if (line_sum < 0) {
                game->flip_line(i);
                update = true;
            }
        }

        for (int16_t j = 0; j < N; j++) {
            auto line_sum = game->sum_col(j);
            if (line_sum < 0) {
                game->flip_column(j);
                update = true;
            }
        }
    } while (update);

    auto max = game->get_sum();

    std::ofstream out;
    out.open(output_file_name);

    if (!out.is_open()) {
        std::cerr << "Could not open the file: '" << output_file_name << "'" << std::endl;
        return EXIT_FAILURE;
    }

    out << max;

    return 0;
}

int main() {
    flip_algo();
}