-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP2615.cpp
51 lines (51 loc) · 984 Bytes
/
P2615.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
#include<bits/stdc++.h>
using namespace std;
int N;
int a[40][40];
int main()
{
cin>>N;
int x1,y1;
a[0][N/2]=1;
x1=0;y1=N/2;
for(int i=2;i<=N*N;i++){
if(x1==0&&y1!=N-1){
a[N-1][y1+1]=i;
x1=N-1;
y1=y1+1;
}
else if(x1!=0&&y1==N-1)
{
a[x1-1][0]=i;
x1=x1-1;
y1=0;
}
else if (x1==0&&y1==N-1)
{
a[x1+1][y1]=i;
x1=x1+1;
}
else if (x1!=0&&y1!=N-1)
{
if(a[x1-1][y1+1]==0){
a[x1-1][y1+1]=i;
x1=x1-1;
y1=y1+1;
}
else {
a[x1+1][y1]=i;
x1=x1+1;
}
}
//cout<<i<<' '<<x1<<' '<<y1<<endl;
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cout<<a[i][j]<<' ';
}
cout<<endl;
}
return 0;
}