Advertisement
vencinachev

Spiral

Dec 16th, 2020
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void left(int& i, int& j)
  6. {
  7.     j--;
  8. }
  9.  
  10. void right(int& i, int& j)
  11. {
  12.     j++;
  13. }
  14.  
  15. void up(int& i, int& j)
  16. {
  17.     i--;
  18. }
  19.  
  20. void down(int& i, int& j)
  21. {
  22.     i++;
  23. }
  24.  
  25. int b[5][5] = {{0, 0, 0, 0, 0},
  26.                {0, 0, 0, 0, 0},
  27.                {0, 0, 0, 0, 0},
  28.                {0, 0, 0, 0, 0},
  29.                {0, 0, 0, 0, 0}};
  30.  
  31. bool isFree(int i, int j, int n)
  32. {
  33.     if (b[i][j] != 0 || i < 0 || i >= n || j < 0 || j >= n)
  34.     {
  35.         return false;
  36.     }
  37.     return true;
  38. }
  39.  
  40. int main()
  41. {
  42.     int n = 5;
  43.     int a[5][5] = {{1, 2, 3, 4, 5},
  44.                   {6, 7, 8, 9, 10},
  45.                   {11, 12, 13, 14, 15},
  46.                   {16, 17, 18, 19, 20},
  47.                   {21, 22, 23, 24, 25}};
  48.  
  49.                   /*
  50.     int si = 0, sj = 0;
  51.  
  52.     for (int k = 0; k <= n / 2; k++)
  53.     {
  54.         while (isFree(si, sj, n))
  55.         {
  56.             b[si][sj] = 1;
  57.             cout << a[si][sj] << " ";
  58.             right(si, sj);
  59.         }
  60.         sj--;
  61.         si++;
  62.         while (isFree(si, sj, n))
  63.         {
  64.             b[si][sj] = 1;
  65.             cout << a[si][sj] << " ";
  66.             down(si, sj);
  67.         }
  68.         sj--;
  69.         si--;
  70.         while (isFree(si, sj, n))
  71.         {
  72.             b[si][sj] = 1;
  73.             cout << a[si][sj] << " ";
  74.             left(si, sj);
  75.         }
  76.         si--;
  77.         sj++;
  78.         while (isFree(si, sj, n))
  79.         {
  80.             b[si][sj] = 1;
  81.             cout << a[si][sj] << " ";
  82.             up(si, sj);
  83.         }
  84.         si++;
  85.         sj++;
  86.     }
  87.     */
  88.  
  89.     // dlur
  90.     int si = 0, sj = n - 1;
  91.  
  92.     for (int k = 0; k <= n / 2; k++)
  93.     {
  94.         while (isFree(si, sj, n))
  95.         {
  96.             b[si][sj] = 1;
  97.             cout << a[si][sj] << " ";
  98.             down(si, sj);
  99.         }
  100.         sj--;
  101.         si--;
  102.          while (isFree(si, sj, n))
  103.         {
  104.             b[si][sj] = 1;
  105.             cout << a[si][sj] << " ";
  106.             left(si, sj);
  107.         }
  108.         si--;
  109.         sj++;
  110.         while (isFree(si, sj, n))
  111.         {
  112.             b[si][sj] = 1;
  113.             cout << a[si][sj] << " ";
  114.             up(si, sj);
  115.         }
  116.         si++;
  117.         sj++;
  118.         while (isFree(si, sj, n))
  119.         {
  120.             b[si][sj] = 1;
  121.             cout << a[si][sj] << " ";
  122.             right(si, sj);
  123.         }
  124.         sj--;
  125.         si++;
  126.     }
  127.     return 0;
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement