Cod sursa(job #2445940)

Utilizator Sebastian_RapaRapa Sebastian Sebastian_Rapa Data 6 august 2019 12:26:54
Problema Jocul Flip Scor 0
Compilator java Status done
Runda Arhiva de probleme Marime 2.87 kb
package infoPb_3;

import java.io.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(new File("/home/sebastian.rapa/Projects/InfoArenaProblems/src/infoPb_3/infoPb_3_resource"));
            String[] line = scanner.nextLine().split("\\s");

            //Matrix boundries
            int n = Integer.parseInt(line[0]);
            int m = Integer.parseInt(line[1]);
            int[][] matrix = new int[n][m];
            int i = 0;
            int j = 0;

            //Putting the matrix in memory
            while (scanner.hasNext()) {
                line = scanner.nextLine().split("\\s");
                for (String s : line) {
                    matrix[i][j] = Integer.parseInt(s);
                    j++;
                }
                j = 0;
                i++;
            }

            //Calculating the sum of the elements on each line
            int sum = 0;
            int switchedSum = 0;
            for (i = 0; i < n; i++) {
                for (j = 0; j < m; j++) {
                    sum += matrix[i][j];
                    switchedSum += (matrix[i][j] * (-1));
                }
                if (sum < switchedSum) {
                    switchTheLine(matrix, i, m);
                }
                sum = 0;
                switchedSum = 0;
            }

            //Calculating the sum of the elements on each column
            for (j = 0; j < m; j++) {
                for (i = 0; i < n; i++) {
                    sum += matrix[i][j];
                    switchedSum += (matrix[i][j] * (-1));
                }
                if (sum < switchedSum) {
                    switchTheColumn(matrix, j, n);
                }
                sum = 0;
                switchedSum = 0;
            }

            BufferedWriter writer = new BufferedWriter(new FileWriter("/home/sebastian.rapa/Projects/InfoArenaProblems/src/infoPb_3/infoPb_3_answer"));
            try {
                printTheMatrix(matrix, n, m, writer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            writer.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void printTheMatrix(int[][] matrix, int n, int m, BufferedWriter writer) throws IOException {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                sum += matrix[i][j];
            }
        }
        writer.write(Integer.toString(sum));
    }

    private static void switchTheLine(int[][] matrix, int i, int m) {
        for (int j = 0; j < m; j++) {
            matrix[i][j] = matrix[i][j] * (-1);
        }
    }

    private static void switchTheColumn(int[][] matrix, int j, int n) {
        for (int i = 0; i < n; i++) {
            matrix[i][j] = matrix[i][j] * (-1);
        }
    }
}