Pagini recente » Cod sursa (job #2527894) | Istoria paginii runda/aladin_si_lampa_fermecata_round15 | Cod sursa (job #2082833) | Cod sursa (job #406807) | Cod sursa (job #1500488)
import java.io.*;
class Flip {
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);
}
}