| 12
 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
 
 | #include<bits/stdc++.h>
 #define LL long long
 #define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
 #define pii pair<int,int>
 using namespace std;
 int n,st,arr[50][50];
 int row=1,col=1;
 int last=0;
 string ans;
 void solve(int x,int y,int face){
 if(col+row == 2*n+1)
 return;
 if(st%2==0 && col+row==2*n)
 last=1;
 else if(st%2==1 && col+row==2*n)
 last=1;
 if(face==0){
 
 for(int i=1 ; i<=col-last ; i++){
 ans+=(char)('0'+arr[x][y-i]);
 }
 col++;
 solve(x,y-(col-1),1);
 }
 else if(face==1){
 
 for(int i=1 ; i<=row-last ; i++){
 ans+=(char)('0'+arr[x-i][y]);
 }
 row++;
 solve(x-(row-1),y,2);
 }
 else if(face==2){
 
 for(int i=1 ; i<=col-last ; i++){
 ans+=(char)('0'+arr[x][y+i]);
 }
 col++;
 solve(x,y+(col-1),3);
 }
 else if(face==3){
 
 for(int i=1 ; i<=row-last ; i++){
 ans+=(char)('0'+arr[x+i][y]);
 }
 row++;
 solve(x+(row-1),y,0);
 }
 }
 int main(){
 IOS
 cin>>n>>st;
 for(int i=0 ; i<n ; i++)
 for(int j=0 ; j<n ; j++)
 cin>>arr[i][j];
 ans+=(char)('0'+arr[n/2][n/2]);
 solve(n/2,n/2,st);
 cout<<ans<<'\n';
 return 0;
 }
 
 |