310257: CF1806A. Walking Master

Memory Limit:1024 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

A. Walking Mastertime limit per test1 secondmemory limit per test1024 megabytesinputstandard inputoutputstandard output

YunQian is standing on an infinite plane with the Cartesian coordinate system on it. In one move, she can move to the diagonally adjacent point on the top right or the adjacent point on the left.

That is, if she is standing on point $(x,y)$, she can either move to point $(x+1,y+1)$ or point $(x-1,y)$.

YunQian initially stands at point $(a,b)$ and wants to move to point $(c,d)$. Find the minimum number of moves she needs to make or declare that it is impossible.

Input

The first line contains a single integer $t$ ($1\le t\le 10^4$) — the number of test cases. The description of test cases follows.

The first line and only line of each test case contain four integers $a$, $b$, $c$, $d$ ($-10^8\le a,b,c,d\le 10^8$).

Output

For each test case, if it is possible to move from point $(a,b)$ to point $(c,d)$, output the minimum number of moves. Otherwise, output $-1$.

ExampleInput
6
-1 0 -1 2
0 0 4 5
-2 -1 1 1
-3 2 -3 2
2 -1 -1 -1
1 1 0 2
Output
4
6
-1
0
3
3
Note

In the first test case, one possible way using $4$ moves is $(-1,0)\to (0,1)\to (-1,1)\to (0,2)\to (-1,2)$. It can be proven that it is impossible to move from point $(-1,0)$ to point $(-1,2)$ in less than $4$ moves.

Input

题意翻译

云倩现在站在 $ (x,y) $ 上,她每次只能想左或向右上移动一步,即移动到 $ (x-1,y) $ 或 $(x+1,y+1) $。 她现在想从 $ (a,b) $ 移动到 $ (c,d) $,请问她最少需要移动多少次才能到达 $ (c,d) $,如无法到达请输出 $ -1 $。 输入第一行一个整数 $ t $ ( $ 1\le t\le 10^4 $ ),后面 $ t $ 行,每行四个整数 $ a $ , $ b $ , $ c $ , $ d $ ( $ -10^8\le a,b,c,d\le 10^8 $ ),意义与描述中对应。 对于每组数据输出一个整数,表示最少的移动次数。

Output

题目大意:
YunQian站在一个带有笛卡尔坐标系的无限平面上。每次移动,她可以移动到右上角的对角线相邻点或者左边的相邻点。也就是说,如果她站在点 $(x,y)$ 上,她可以移动到点 $(x+1,y+1)$ 或者点 $(x-1,y)$。YunQian最初站在点 $(a,b)$ 上,想要移动到点 $(c,d)$。求她需要做出的最小移动次数,或者声明这是不可能的。

输入数据格式:
第一行包含一个整数 $t$ ($1 \le t \le 10^4$) —— 测试用例的数量。接下来是每个测试用例的描述。
每个测试用例只有一行,包含四个整数 $a, b, c, d$ ($-10^8 \le a,b,c,d \le 10^8$)。

输出数据格式:
对于每个测试用例,如果能从点 $(a,b)$ 移动到点 $(c,d)$,输出所需的最小移动次数。否则,输出 $-1$。

示例:
输入:
```
6
-1 0 -1 2
0 0 4 5
-2 -1 1 1
-3 2 -3 2
2 -1 -1 -1
1 1 0 2
```
输出:
```
4
6
-1
0
3
3
```

注意:
在第一个测试用例中,一种可能的移动方式是 $(-1,0) \to (0,1) \to (-1,1) \to (0,2) \to (-1,2)$。可以证明,从点 $(-1,0)$ 到点 $(-1,2)$ 的移动不可能少于 4 次。题目大意: YunQian站在一个带有笛卡尔坐标系的无限平面上。每次移动,她可以移动到右上角的对角线相邻点或者左边的相邻点。也就是说,如果她站在点 $(x,y)$ 上,她可以移动到点 $(x+1,y+1)$ 或者点 $(x-1,y)$。YunQian最初站在点 $(a,b)$ 上,想要移动到点 $(c,d)$。求她需要做出的最小移动次数,或者声明这是不可能的。 输入数据格式: 第一行包含一个整数 $t$ ($1 \le t \le 10^4$) —— 测试用例的数量。接下来是每个测试用例的描述。 每个测试用例只有一行,包含四个整数 $a, b, c, d$ ($-10^8 \le a,b,c,d \le 10^8$)。 输出数据格式: 对于每个测试用例,如果能从点 $(a,b)$ 移动到点 $(c,d)$,输出所需的最小移动次数。否则,输出 $-1$。 示例: 输入: ``` 6 -1 0 -1 2 0 0 4 5 -2 -1 1 1 -3 2 -3 2 2 -1 -1 -1 1 1 0 2 ``` 输出: ``` 4 6 -1 0 3 3 ``` 注意: 在第一个测试用例中,一种可能的移动方式是 $(-1,0) \to (0,1) \to (-1,1) \to (0,2) \to (-1,2)$。可以证明,从点 $(-1,0)$ 到点 $(-1,2)$ 的移动不可能少于 4 次。

加入题单

算法标签: