Cod sursa(job #1518995)

Utilizator NicholasFlamelFasie Vlad George NicholasFlamel Data 6 noiembrie 2015 17:54:40
Problema Jocul Flip Scor 0
Compilator java Status done
Runda Arhiva de probleme Marime 2.9 kb
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

class Main {
 
    private static Integer n, m;
    private static Integer[][] map;
     
    public static void main(String [] args) throws FileNotFoundException, UnsupportedEncodingException {
        read();
        for (int i=0; i<n; i++)
            if (checkRow(i) > 0)
                switchRow(i);
        for (int i=0; i<m; i++)
            if (checkColumn(i) > 0)
                switchColumn(i);
        PrintWriter writer = new PrintWriter("flip.out", "UTF-8");
        writer.println(getSum());
        writer.close();
    }
     
    public static void read() {
        String path = System.getProperty("user.dir");
        String fileName = path + "\\flip.in";
 
        try {
            FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line = null;
             
            line = bufferedReader.readLine();
            String[] p = line.split(" ");
            n = Integer.parseInt(p[0]);
            m = Integer.parseInt(p[1]);
            map = new Integer[n][m];
             
            for (int i=0; i<n; i++) {
                line = bufferedReader.readLine();
                p = line.split(" ");
                for (int j=0; j<m; j++)
                    map[i][j] = Integer.parseInt(p[j]);
            }
            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" +
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '"
                + fileName + "'");            
        }
    }
     
    public static void switchRow(int r) {
        for (int i=0; i<m; i++)
            map[r][i] = -map[r][i];
    }
    public static void switchColumn(int c) {
        for (int i=0; i<n; i++)
            map[i][c] = -map[i][c];
    }
     
    public static int getSum() {
        int result = 0;
        for (int i=0; i<n; i++)
            for (int j=0; j<m; j++)
                result += map[i][j];
        return result;
    }
     
    public static int getSumRow(int r) {
        int result = 0;
        for (int i=0; i<m; i++)
            result += map[r][i];
        return result;
    }
    public static int getSumColumn(int c) {
        int result = 0;
        for (int i=0; i<n; i++)
            result += map[i][c];
        return result;
    }
     
    public static int checkRow(int r) {
        return -2 * getSumRow(r);
    }
    public static int checkColumn(int c) {
        return -2 * getSumColumn(c);
    }
     
}