#include <iostream>
using namespace std;
const int MAX_SIZE = 50;
int main() {
int mtSize, windLine[MAX_SIZE + 1][MAX_SIZE + 1];
cin >> mtSize;
for (int line = 1; line <= mtSize; ++line) {
for (int col = 1; col <= mtSize; ++col) {
cin >> windLine[line][col];
}
}
int lineDir = 0, colDir = 1;
/*
pop.ga.flaviu
2024-02-11 10:36:11
Initialize 'lineDir' with the value of -1 and think how it could help you make your code shorter
*/
for (int linePos = 1, colPos = 1; linePos <= mtSize && colPos <= mtSize;) {
/*
pop.ga.flaviu
2024-02-11 10:35:24
Instead of 'mtSize * mtSize' use 2 variables, i and j, and multiple conditions.
Also, declare the 'linePos' and 'colPos' in the for loop's condition
*/
cout << windLine[linePos][colPos] << " ";
int lPlusC = linePos + colPos;
if (lPlusC % 2 && ((lineDir == 0 && colDir == 1 && lPlusC <= mtSize + 1)
/*
pop.ga.flaviu
2024-02-11 10:37:16
Let's use only 1 if statement here to print the result, then 1 if-else if.
*/
|| (lineDir == 1 && colDir == 0 && lPlusC >= mtSize + 1))) {
lineDir = 1;
colDir = -1;
}
if (lPlusC % 2 == 0 && ((lineDir == 0 && colDir == 1
&& lPlusC >= mtSize + 1) || (lineDir == 1 && colDir == 0
&& lPlusC <= mtSize + 1))) {
lineDir = -1;
colDir = 1;
}
if ((lineDir == 1 && colDir == -1 && lPlusC < mtSize + 1 && colPos == 1)
|| (lineDir == -1 && colDir == 1 && lPlusC >= mtSize + 1
&& colPos == mtSize)) {
lineDir = 1;
colDir = 0;
}
if ((lineDir == 1 && colDir == -1 && lPlusC >= mtSize + 1
&& linePos == mtSize) || (lineDir == -1 && colDir == 1 &&
lPlusC < mtSize + 1 && linePos == 1 )) {
lineDir = 0;
colDir = 1;
}
linePos += lineDir;
colPos += colDir;
lPlusC = linePos + colPos;
}
return 0;
}