Advertisement
ColdWater0

Untitled

Sep 24th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.     int M, N, n, t;
  6.     int timee;
  7.     int x, y, w, z;
  8.     int is_valid = 0;
  9.     cin >> n;
  10.     for(t=0; t<n; t++){
  11.         cin >> M >> N >> w >> z ;
  12.         is_valid = 0;
  13.         timee = 1;
  14.         for(x=1, y=1; !((x == M) && (y == N));){
  15.             if(x<M){
  16.                 x++;
  17.             }else{
  18.                 x = 1;
  19.             }
  20.             if(y<N){
  21.                 y++;
  22.             }else{
  23.                 y = 1;
  24.             }
  25.             timee++;
  26.            
  27.             if((x == w) && (y == z)){
  28.                 is_valid = 1;
  29.                 cout << timee << "\n";
  30.                 continue;
  31.             }
  32.         }
  33.         if(is_valid == 0){
  34.             cout << -1 << "\n";
  35.         }
  36.     }
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement