-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
60 lines (57 loc) · 1.27 KB
/
main.cpp
File metadata and controls
60 lines (57 loc) · 1.27 KB
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
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> res(n, vector<int>(n,0));
int u = 0, d = n - 1, l = 0, r = n - 1;
int k = 1;
while(1)
{
// l -> r
for(int col = l; col <= r; ++col)
{
res[u][col] = k++;
}
if(++u > d) break;
// u -> d
for(int row = u; row <= d; ++row)
{
res[row][r] = k++;
}
if(--r < l) break;
// r -> l
for(int col = r; col >= l; --col)
{
res[d][col] = k++;
}
if(--d < u) break;
// d -> u
for(int row = d; row >= u; --row)
{
res[row][l] = k++;
}
if(++l > r) break;
}
return res;
}
};
int main()
{
int n;
cin >> n;
Solution s;
vector<vector<int>> res;
res = s.generateMatrix(n);
for(int i = 0; i < res.size(); ++i)
{
for(int j = 0; j < res[i].size(); ++j)
{
cout << res[i][j] << " ";
}
cout << endl;
}
return 0;
}