diff --git a/59_spiral_matrix_2/main.cpp b/59_spiral_matrix_2/main.cpp new file mode 100644 index 0000000..932eac7 --- /dev/null +++ b/59_spiral_matrix_2/main.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; + +class Solution { + private: + // {row, col} + const vector> directions{ + {0, 1}, + {1, 0}, + {0, -1}, + {-1, 0}, + }; + + inline pair addPair(const pair& x, const pair& y) { + return {x.first + y.first, x.second + y.second}; + } + + void nextDirection(int& index, pair& direction) { + if (++index >= 4) index = 0; + direction = directions[index]; + } + + public: + vector> generateMatrix(int n) { + vector> matrix(n + 2, vector(n + 2, 0)); + for (int i = 0; i < n + 2; i++) + matrix[i][0] = INT32_MAX, + matrix[i][n + 1] = INT32_MAX, + matrix[0][i] = INT32_MAX, + matrix[n + 1][i] = INT32_MAX; + + int directionIndex = 0; + pair direction = directions[directionIndex]; + pair currentPos{1, 1}; + + for (int i = 1; i <= n * n; i++) { + auto nextPos = addPair(currentPos, direction); + if (matrix[nextPos.first][nextPos.second] > 0) { + nextDirection(directionIndex, direction); + nextPos = addPair(currentPos, direction); + } + matrix[currentPos.first][currentPos.second] = i; + currentPos = nextPos; + } + + vector> result(n, vector(n, 0)); + for (int i = 1; i < n + 1; i++) + for (int j = 1; j < n + 1; j++) + result[i - 1][j - 1] = matrix[i][j]; + + return result; + } +}; + +int main() { + Solution().generateMatrix(3); + return 0; +}