#include "iostream"
#include "vector"
using namespace std;
vector<vector<int>> generateMatrix_T(int n) {
vector<vector<int>> v(n, vector<int>(n, 0));
int count = 1;
int x = 0, y = 0;
int startX = 0, startY = 0;
int num = n * (n + 1) / 2;
while (count <= num) {
x = startX, y = startY;
while (y < n) {
v[x][y] = count;
count++;
y++;
}
y -= 2;
x++;
while (x < n) {
v[x][y] = count;
count++;
x++;
y--;
}
x -= 2;
y++;
while (x > startX) {
v[x][y] = count;
count++;
x--;
}
n -= 2;
startX = startX + 1;
startY = startY + 1;
}
return v;
}
int main(){
int n = 0;
cin >> n;
auto ret = generateMatrix_T(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << ret[i][j] << " ";
}
cout << endl;
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51