Cod sursa(job #2180039)

Utilizator xsorinJohn Smith xsorin Data 20 martie 2018 16:33:12
Problema Jocul Flip Scor 0
Compilator java Status done
Runda Arhiva de probleme Marime 2.97 kb
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {

    public static int make_sum(int[] arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws FileNotFoundException {

        //creating File instance to reference text file in Java
        File text = new File("flip.in");
        BufferedWriter writer;
        int suma = 0;
        int[] rows = new int[16];
        int[] cols = new int[16];
        int[] temp = new int[16];
        //Creating Scanner instnace to read File in Java
        Scanner sc = new Scanner(text);
        String line = sc.nextLine();
        String[] params = line.split("\\s+");
        int randuri = Integer.valueOf(params[0]);
        int coloane = Integer.valueOf(params[1]);
        int[][] matrix = new int[randuri][coloane];
        int t = 0;
        while (sc.hasNextLine()) {
            line = sc.nextLine();
            String[] vals = line.split("\\s+");
            for (int j = 0; j < coloane; j++) {
                matrix[t][j] = Integer.valueOf(vals[j]);
            }
            t++;

        }
        for (int i = 0; i < randuri; i++) {
            for (int j = 0; j < coloane; j++) {
                rows[j] = matrix[i][j];
                temp[j] = matrix[i][j] * -1;
            }

            if (make_sum(rows) < make_sum(temp)) {
                for (int p = 0; p < coloane; p++) {
                    matrix[i][p] = temp[p];
                }
            } else {
                for (int p = 0; p < coloane; p++) {
                    matrix[i][p] = rows[p];
                }
            }
        }
        for (int i = 0; i < coloane; i++) {
            for (int j = 0; j < randuri; j++) {
                cols[j] = matrix[j][i];
                temp[j] = matrix[j][i] * -1;
            }

            if (make_sum(cols) < make_sum(temp)) {
                for (int p = 0; p < randuri; p++) {
                    matrix[p][i] = temp[p];
                }
            } else {
                for (int p = 0; p < randuri; p++) {
                    matrix[p][i] = cols[p];
                }
            }

        }

        for (int i = 0; i < randuri; i++) {
            for (int j = 0; j < coloane; j++) {
                suma += matrix[i][j];
            }
        }

        try {
            writer = new BufferedWriter(new FileWriter("flip.out"));
            writer.write(String.valueOf(suma));
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        

    }

}