Cod sursa(job #2639559)

Utilizator Gabryel9898Bizdoc Vasile Gabriel Gabryel9898 Data 2 august 2020 18:54:49
Problema Jocul Flip Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.73 kb

#include <iostream>
#include <vector>
#include <fstream>
std::ifstream infile("flip.in");
std::ofstream ofile("flip.out");

std::vector<std::vector<int>> rotateClockwise90(const std::vector<std::vector<int>>& x)
{
    std::vector<std::vector<int>> result;

    for (unsigned i = 0; i < x[0].size(); i++) {
        std::vector<int> row;
        for (int j = x.size() - 1; j >= 0; j--) {
            row.push_back(x[j][i]);
        }
        result.push_back(row);
    }

    return result;
}

std::vector<std::vector<int>> rotateCounterclockwise90(const std::vector<std::vector<int>>& x)
{
    std::vector<std::vector<int>> result;

    for (int i = x[0].size() - 1; i >= 0; i--) {
        std::vector<int> row;
        for (int j = 0; j < x.size(); j++) {
            row.push_back(x[j][i]);
        }
        result.push_back(row);
    }

    return result;
}

int print(const std::vector<std::vector<int>>& matrix) {
    for(auto& row: matrix) {
        for(auto& col: row) {
            std::cout << col;
            std::cout << ", ";
        }
        std::cout << std::endl;
    }

    return 0;
}

void negate(std::vector<int>& row){
     for(auto& col: row) {
        col *= -1;
        
     }
}

void calc(std::vector<std::vector<int>>& matrix) {
    bool retry = false; 
    for(auto& row: matrix) {
        int n = 0, p = 0;
        for(auto& col: row) {
            if(col < 0){
                n += col;
            } else {
                p += col;
            }
        }
        
        if(-1 * n > p) {
            negate(row);
            retry = true;
        }
    }
    
    matrix = rotateClockwise90(matrix);
    for(auto& row: matrix) {
        int n = 0, p = 0;
        for(auto& col: row) {
            if(col < 0){
                n += col;
            } else {
                p += col;
            }
        }
        
        if(-1 * n > p) {
            negate(row);
            retry = true;
        }
    }
    
    matrix = rotateCounterclockwise90(matrix);
    if(retry){
        calc(matrix);
    }
}

int sum(std::vector<std::vector<int>>& matrix) {
    int sum = 0;
    for(auto& row: matrix) {
        for(auto& col: row) {
            sum += col;
        }
    }
    return sum;
}

std::vector<std::vector<int>> read_matrix(const int& m, const int& n){
    std::vector<std::vector<int>> matrix( m , std::vector<int> (n, 0));  
    for(auto& row: matrix) {
        for(auto& col: row) {
            infile >> col;
        }
    }
    return matrix;
}

int main()
{
    int n, m;
    infile >> m >> n;
    std::vector<std::vector<int>> matrix = read_matrix(m, n);

    calc(matrix);

    ofile << sum(matrix);
    return 0;
}