Cod sursa(job #1469885)

Utilizator sabin.antoheSabin Antohe sabin.antohe Data 9 august 2015 20:05:58
Problema Jocul Flip Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>
#include <algorithm>
using namespace std;
int lin[20], col[20], m[20][20], suma_max = 0, N, M;

void suma () {
    int max1 = 0;

    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= M; j++)
            if (lin[i] == col[j])
                max1 += m[i][j];
            else
                max1 -= m[i][j];

    if (max1 > suma_max)
        suma_max = max1;
}

int main(void) {
    ifstream input ("flip.in");
    ofstream output ("flip.out");
    input >> N >> M;

    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= M; j++)
            input >> m[i][j];
    
    for (int i = 0; i <= N; i++) {
        lin[i] = 1;
        do {
            for (int j = 0; j <= M; j++) {
                col[j] = 1;
                do {
                    suma ();
                }while (next_permutation(col+1,col+M+1));
            }
            for(int j = 0; j <= M; j++)
                col[j] = 0;        
        }while (next_permutation(lin+1,lin+N+1));
    }
    output << suma_max;
}