Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main()
  5. {
  6.     int n;
  7.     while(cin>>n)
  8.     {
  9.         int a[n][n]={ },i=n*n-1,v=n-1,h=n-1,dir=1;
  10.         while(i>=0)
  11.         {
  12.             a[v][h]=i;
  13.             i--;
  14.             //cout << v << " " << h << " " << a[v][h] << endl;
  15.             if(dir==1)
  16.             {
  17.                 if(h-1<0||a[v][h-1]!=0)
  18.                 {
  19.                     dir=2;
  20.                     v--;
  21.                 }
  22.                 else h--;
  23.             }
  24.             else if(dir==2)
  25.             {
  26.                 if(v-1<0||a[v-1][h]!=0)
  27.                 {
  28.                     dir=3;
  29.                     h++;
  30.                 }
  31.                 else v--;
  32.             }
  33.             else if(dir==3)
  34.             {
  35.                 if(h+1>n-1||a[v][h+1]!=0)
  36.                 {
  37.                     dir=4;
  38.                     v++;
  39.                 }
  40.                 else h++;
  41.             }
  42.             else //if(dir==4)
  43.             {
  44.                 if(v+1>n-1||a[v+1][h]!=0)
  45.                 {
  46.                     dir=1;
  47.                     h--;
  48.                 }
  49.                 else v++;
  50.             }
  51.         }
  52.  
  53.         for(int j=0;j<n;j++)
  54.         {
  55.             for(int k=0;k<n;k++)
  56.                 cout << setw(4) << a[j][k] ;
  57.             cout<<endl;
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement