Guest User

Untitled

a guest
Feb 18th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. static int[] Razmotat(int[,] a)
  2.         {
  3.             var n = a.GetLength(0); // Matrix is n*n
  4.  
  5.             var result = new int[a.Length]; // 1d array of length n*n
  6.  
  7.             var p = 0; //processed elements
  8.  
  9.             //start coords (right bottom)
  10.             int i = n - 1;
  11.             int j = 0;
  12.  
  13.             //bounds to know where to stop on each circle
  14.             int leftBound = 0;
  15.             int rightBound = n - 1;
  16.             int topBound = 0;
  17.             int bottomBound = n - 1;
  18.  
  19.             //moving direction
  20.             var dir = Direction.Up;
  21.  
  22.             //while current element is not center
  23.             for(int k = 0; k < result.Length; k++)
  24.             {
  25.                 switch (dir)
  26.                 {
  27.                     //UP
  28.                     case Direction.Up:
  29.                         result[p++] = a[i, j]; //save current element to result
  30.                         if (i == topBound)  //if it's bound then change direction, move index to next and change bound
  31.                         {
  32.                             dir = Direction.Right;
  33.                             j++;
  34.                             leftBound++;
  35.                         }
  36.                         else  //else go to next element in same direction
  37.                             i--;
  38.                         break;
  39.                     //RIGHT
  40.                     case Direction.Right:
  41.                         result[p++] = a[i, j];
  42.                         if (j == rightBound)
  43.                         {
  44.                             dir = Direction.Down;
  45.                             i++;
  46.                             topBound++;
  47.                         }
  48.                         else
  49.                             j++;
  50.                         break;
  51.  
  52.  
  53.                     case Direction.Down:
  54.                         result[p++] = a[i, j];
  55.                         if (i == bottomBound)
  56.                         {
  57.                             dir = Direction.Left;
  58.                             j--;
  59.                             rightBound--;
  60.                         }
  61.                         else
  62.                             i++;
  63.                         break;
  64.  
  65.                     case Direction.Left:
  66.                         result[p++] = a[i, j];
  67.                         if (j == leftBound)
  68.                         {
  69.                             dir = Direction.Up;
  70.                             i--;
  71.                             bottomBound--;
  72.                         }
  73.                         else
  74.                             j--;
  75.                         break;
  76.                 }
  77.             }
  78.  
  79.             //reverse
  80.             for (int k = 0; k < result.Length / 2; k++)
  81.             {
  82.                 int tmp = result[k];
  83.                 result[k] = result[result.Length - k - 1];
  84.                 result[result.Length - k - 1] = tmp;
  85.             }
  86.             return result;
  87.         }
  88.  
  89.         enum Direction
  90.         {
  91.             Up,
  92.             Down,
  93.             Left,
  94.             Right
  95.         }
Add Comment
Please, Sign In to add comment