Pagini recente » Cod sursa (job #946322) | Cod sursa (job #747110) | Cod sursa (job #621692) | Cod sursa (job #2977113) | Cod sursa (job #2972218)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("flip.in"));
String[] line = br.readLine().split(" ");
int n = Integer.parseInt(line[0]);
int m = Integer.parseInt(line[1]);
int[][] board = new int[n][m];
for (int i = 0; i < n; i++) {
line = br.readLine().split(" ");
for (int j = 0; j < m; j++) {
board[i][j] = Integer.parseInt(line[j]);
}
}
br.close();
int maxSum = flip(n, m, board);
PrintWriter pw = new PrintWriter("flip.out");
pw.println(maxSum);
pw.close();
}
public static int flip(int n, int m, int[][] board) {
int maxSum = 0;
for (int i = 0; i < (1 << n); i++) {
for (int j = 0; j < (1 << m); j++) {
int currSum = 0;
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
if ((i & (1 << x)) != 0) {
board[x][y] *= -1;
}
if ((j & (1 << y)) != 0) {
board[x][y] *= -1;
}
currSum += board[x][y];
}
}
maxSum = Math.max(maxSum, currSum);
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
if ((i & (1 << x)) != 0) {
board[x][y] *= -1;
}
if ((j & (1 << y)) != 0) {
board[x][y] *= -1;
}
}
}
}
}
return maxSum;
}
}