-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprialMatrix.cpp
112 lines (104 loc) · 1.95 KB
/
sprialMatrix.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a, int b){
return a>b;
}
int main(){
int row=1,col=1;
int n;
cin>>n;
if(n==1){
row=1;col=1;
}
else
{
for(int i=1;i<n;i++){
if(n%i==0){
if(i>n/i){
break;
}
row = n/i;
col = i;
}
}
}
vector<int> datav;
for(int i=0;i<n;i++){
int d;
cin>>d;
datav.push_back(d);
}
sort(datav.begin(), datav.end(), cmp);
vector<vector<int>> mv;
mv.resize(row);
for(int i=0;i<mv.size();i++){
mv[i].resize(col,-1);
}
//0r 1d 2l 3u
int i=0,j=0;
int cnt=0;
int stat=0;
while(cnt<n){
mv[i][j]=datav[cnt];
cnt++;
if(stat==0){
if(j!=col-1 && mv[i][j+1]==-1)
{
j=j+1;
continue;
}
else
{
stat=1;
}
}
if(stat==1){
if(i!=row-1 && mv[i+1][j]==-1)
{
i++;
continue;
}
else
{
stat=2;
}
}
if(stat==2){
if(j!=0 && mv[i][j-1]==-1)
{
j--;
continue;
}
else
{
stat=3;
}
}
if(stat==3){
if(i!=0 && mv[i-1][j]==-1)
{
i--;
continue;
}
else
{
stat=0;
j=j+1;
}
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(j==0){
cout<<mv[i][j];
}
else
{
cout<<" "<<mv[i][j];
}
}
cout<<endl;
}
}