-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_59.cpp
More file actions
33 lines (33 loc) · 755 Bytes
/
Problem_59.cpp
File metadata and controls
33 lines (33 loc) · 755 Bytes
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
class Solution {
public:
int dr[4] = {0 , 1, 0 , -1};
int dc[4] = {1 , 0 , -1 , 0};
vector<vector<int>> ans;
void solve( int sz, int n , int i, int j ){
if(sz <= 0){
return ;
}
ans[i][j] = n++;
sz--;
if(sz <= 0){
return ;
}
for(int k = 0; k<4 ; k++){
int temp = sz;
if(k==3){
temp--;
}
while(temp--){
i += dr[k];
j += dc[k];
ans[i][j] = n++;
}
}
solve(sz-1 , n , i, j+1);
}
vector<vector<int>> generateMatrix(int n) {
ans.resize(n , vector<int> (n , 0));
solve( n , 1 , 0 , 0 );
return ans;
}
};