Pagini recente » Cod sursa (job #706717) | Cod sursa (job #996727) | Cod sursa (job #800360) | Cod sursa (job #1815107) | Cod sursa (job #2255950)
#include <fstream>
using namespace std;
int n, m; //matrix dimension
int sum_max;
int matrix[17][17];
int sumMatrixElements();
int switchRowColElements(int val, bool elem); // elem true is row and elem false is column
int BKT(int k);
bool compareSumMax(int sumValue);
ifstream fin ("flip.in");
ofstream fout ("flip.out");
int main() {
fin>>n>>m; //read matrix dimensions
for(int i = 0; i < n; i++ ) {
for(int j = 0; j < m; j++ ){
fin>>matrix[i][j];
}
}
//switchRowColElements(2, true);
//switchRowColElements(1, false);
//fout<<sumMatrixElements();
//write in flip.out file
for(int i = 0; i < n; i++ ) {
for(int j = 0; j < m; j++ ){
fout<<matrix[i][j] << " ";
}
fout<<'\n';
}
int sum_matrix_partial = 0;
/*for(int i = 0; i < m; i++) {
switchRowColElements(i, false);
//BKT(0);
for( int j = 0; j < n; j++ ) {
switchRowColElements(j, true);
sum_matrix_partial = sumMatrixElements();
fout<<"Sum part is: "<<sum_matrix_partial<<'\n';
if(compareSumMax(sum_matrix_partial)) {
sum_max = sum_matrix_partial;
}
switchRowColElements(j, true);
}
switchRowColElements(i, false);
}*/
for(int i = 0; i < m; i++) {
switchRowColElements(i, false);
BKT(0);
switchRowColElements(i, false);
}
fout<<"Maximum value of the matrix is: "<<sum_max;
return 0;
}
bool compareSumMax(int sumValue) {
return sumValue > sum_max ? 1 : 0;
}
int switchRowColElements(int val, bool elem) {
int i;
if( elem ) {
for( i = 0; i < m; i++) {
matrix[val][i] = (-1) * matrix[val][i];
}
} else if( !elem ){
for( i = 0; i < n; i++) {
matrix[i][val] = (-1) * matrix[i][val];
}
}
}
int sumMatrixElements() {
int sum_Matrix = 0;
for(int i = 0; i < n; i++ ) {
for(int j = 0; j < m; j++ ){
sum_Matrix += matrix[i][j];
}
}
return sum_Matrix;
}
int BKT(int k) {
int sum_matrix_partial = 0;
if( k == n ) {
//fout<<sum_max<<" || ";
} else {
for(int i = 0; i < n; i++ ) {
switchRowColElements( i, true );
sum_matrix_partial = sumMatrixElements();
fout<<"Sum part is: "<<sum_matrix_partial<<'\n';
if(compareSumMax(sum_matrix_partial)) {
sum_max = sum_matrix_partial;
}
BKT(k+1);
switchRowColElements( i, true );
}
}
}