Pagini recente » Cod sursa (job #2957766) | Cod sursa (job #307898) | Cod sursa (job #2018186) | Cod sursa (job #2350331) | Cod sursa (job #514040)
Cod sursa(job #514040)
#include <fstream>
#include <string>
using namespace std;
long int g_lnMatrix[16][16];
int g_nVerticalSize = 1, g_nHorizontalSize = 1, g_nLargestSum = 0;
string Line = "Line", Column = "Column";
void Flip (const int &rnIndex, string strWhat)
{
for (int iii = 0; iii < ((strWhat == "Line") ? g_nVerticalSize : g_nHorizontalSize); iii++)
((strWhat == "Line") ? g_lnMatrix[iii][rnIndex] : g_lnMatrix[rnIndex][iii]) *= -1;
}
long int CalculateSum ()
{
long int lnSum = 0;
for (int iii = 0; iii < g_nVerticalSize; iii++)
for (int jjj = 0; jjj < g_nHorizontalSize; jjj++)
lnSum += g_lnMatrix[iii][jjj];
return lnSum;
}
void Permutations (int nVertIndex, int nHorizIndex)
{
if (nHorizIndex == g_nHorizontalSize)
{
int nSum = CalculateSum ();
if (nSum > g_nLargestSum)
g_nLargestSum = nSum;
}
else if (nVertIndex == g_nVerticalSize)
{
Permutations (nVertIndex, nHorizIndex + 1);
Flip (nHorizIndex, Line);
Permutations (nVertIndex, nHorizIndex + 1);
}
else
{
Permutations (nVertIndex + 1, nHorizIndex);
Flip (nVertIndex, Column);
Permutations (nVertIndex + 1, nHorizIndex);
}
}
int main ()
{
ifstream in ("flip.in");
in >> g_nVerticalSize >> g_nHorizontalSize;
for (int iii = 0; iii < g_nVerticalSize; iii++)
for (int jjj = 0; jjj < g_nHorizontalSize; jjj++)
in >> g_lnMatrix[iii][jjj];
in.close();
Permutations (0, 0);
ofstream out ("flip.out");
out << g_nLargestSum;
out.close();
return 0;
}