Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. bool hit(int x, int n) {
  2.     return x == n ? true : false;
  3. }
  4.  
  5.  
  6. std::vector<std::vector<int>> spiralNumbers(int n) {
  7.     int count = 0;
  8.     int direction = 0;
  9.     int x, y = 0;
  10.     std::vector<std::vector<int>> matrix;
  11.    
  12.     while(count < (n*n)+1) {
  13.         std::cout << count << std::endl;
  14.         bool flag = 0;
  15.        
  16.         switch(direction) {
  17.             case 0 :                
  18.                 std::cout << "right" << std::endl;
  19.                 x++;                    // right
  20.                 if(hit(x, n)) {
  21.                     x--;
  22.                     direction++;
  23.                     flag = 1;
  24.                 }      
  25.                 break;
  26.                
  27.             case 1 :                
  28.                 std::cout << "down" << std::endl;
  29.                 y++;                      // down
  30.                     if(hit(y, n)) {
  31.                     y--;
  32.                     direction++;
  33.                     flag = 1;
  34.                 }      
  35.                 break;
  36.                
  37.             case 2 :                
  38.                 std::cout << "left" << std::endl;
  39.                 x--;                  // left
  40.                 if(hit(x, n)) {
  41.                     x++;
  42.                     direction++;
  43.                     flag = 1;
  44.                 }
  45.                 break;
  46.                
  47.             case 3 :                
  48.                 std::cout << "up" << std::endl;
  49.                 y--;                    // up
  50.                 if(hit(y, n)) {
  51.                     y++;
  52.                     direction++;
  53.                     flag = 1;
  54.                 }
  55.                 break;
  56.         }
  57.        
  58.         if(!flag) {
  59.             matrix[x][y] = count;
  60.             count++;
  61.         }
  62.        
  63.         std::cout << "direction: " << direction << " x: " << x << " y: " << y << std::endl;
  64.         if(direction == 4) direction = 0;
  65.  
  66.     }
  67.     return matrix;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement