Advertisement
AshTurner67

Number Spiral

Jun 19th, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8.     long long t;
  9.     cin >> t;
  10.     for(long long i = 1; i <= t; i++){
  11.         long long ycord = 1, xcord = 1, layer = 1, num = 1;
  12.         char move = 'r';
  13.         long long x, y;
  14.         cin >> y >> x;
  15.         while(1){
  16.             cout << "present position : " << ycord << ' ' << xcord << " num : " << num << " layer : " << layer << ' ' << move <<'\n';
  17.             if(xcord == x && ycord == y) break;
  18.             if(move == 'r') xcord++;
  19.             else if(move == 'l') xcord--;
  20.             else if(move == 'u') ycord--;
  21.             else if (move == 'd') ycord++;
  22.             num++;
  23.             if(ycord == 1){
  24.                 layer=layer+1;
  25.                 if(layer%2==0) move = 'd';
  26.                 else move ='r';
  27.             }
  28.             if(xcord == 1){
  29.                 layer=layer+1;
  30.                 if(layer%2==0) move = 'd';
  31.                 else move = 'r';
  32.             }
  33.             if(xcord == ycord){
  34.                 if(layer%2==0) move = 'l';
  35.                 else move = 'u';
  36.             }
  37.         }
  38.         // cout << num << '\n';
  39.     }
  40.  
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement