Guest User

Untitled

a guest
Jun 1st, 2016
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1. static int[,] TypeC(int r, int c)
  2.     {
  3.         int[,] m = new int[r, c];
  4.         int number = 1;
  5.  
  6.         for (int i = r-1; i >= 0; i--)
  7.         {
  8.             int startR = i;
  9.             for (int j = 0; j < r-startR; j++)
  10.             {
  11.                 if (j>c-1) { break; }
  12.                 m[startR + j, j] = number; number++;
  13.             }
  14.         }
  15.  
  16.         for (int i = 1; i < c; i++)
  17.         {
  18.             int startC = i;
  19.             for (int j = 0; j < c - startC; j++)
  20.             {
  21.                 if (j > r - 1) { break; }
  22.                 m[j, startC + j] = number; number++;
  23.             }
  24.         }
  25.  
  26.         return m;
  27.     }
  28.  
  29.     static int[,] TypeD(int r, int c)
  30.     {
  31.         int[,] m = new int[r, c];
  32.         int number = 1;
  33.         int indexR = 0;
  34.         int indexC = 0;
  35.  
  36.         while (number<=r*c)
  37.         {
  38.             m[indexR, indexC] = number;
  39.             number++;;
  40.  
  41.             bool canGoDown = (indexR + 1) < r && m[indexR + 1, indexC] == 0 && !((indexC - 1) >= 0 && m[indexR, indexC - 1] == 0);
  42.             if (canGoDown) { indexR++; continue; }
  43.             bool canGoRight = (indexC + 1) < c && m[indexR, indexC + 1] == 0;
  44.             if (canGoRight) { indexC++; continue; }
  45.             bool canGoUp = (indexR - 1) >= 0 && m[indexR - 1, indexC] == 0;
  46.             if (canGoUp) { indexR--; continue; }
  47.             bool canGoLeft = (indexC - 1) >= 0 && m[indexR, indexC - 1] == 0;
  48.             if (canGoLeft) { indexC--; continue; }
  49.         }
  50.  
  51.         return m;
  52.     }
Advertisement
Add Comment
Please, Sign In to add comment