Pagini recente » Cod sursa (job #1631658) | Cod sursa (job #1302688) | Cod sursa (job #1676477) | Cod sursa (job #2533219) | Cod sursa (job #1422870)
#include <iostream>
#include <fstream>
using namespace std;
int maxsum = 0;
int lines, cols;
int **data = new int*[16];
int getSum(int** mat){
int sum = 0;
for (int i = 0; i < lines; i ++)
for (int j = 0; j < cols; j++)
sum += mat[i][j];
return sum;
}
int ** flip (int** mat, int no, bool line) {
if (line)
for (int i = 0; i < cols; i++)
mat[no][i] *= -1;
else
for (int i = 0; i < lines; i++)
mat[i][no] *= -1;
return mat;
}
void readFile()
{
ifstream in ("flip.in");
if (in.is_open()){
in >> lines;
in >> cols;
//cout << "lines is: " << lines << " cols is: " << cols;
for (int i = 0; i < lines; i ++)
{
data[i] = new int[16];
for (int j = 0; j < cols; j ++)
in >> data[i][j];
}
in.close();
}
}
void writeFile()
{
ofstream out ("flip.out");
out << maxsum;
out.close();
}
void process(int** mat, int no)
{
if (no > lines + cols - 1) return;
int csum = getSum(mat);
if (csum > maxsum)
maxsum = csum;
if (no > lines - 1)
{
process(mat, no + 1);
process(flip(mat, no - lines, false), no + 1);
} else
{
process(mat, no + 1);
process(flip(mat, no, true), no+1);
}
}
int main()
{
readFile();
process(data, 0);
writeFile();
return 0;
}