#P14114. [IAMOI R4] 皇后

    ID: 13551 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>洛谷原创O2优化洛谷月赛分类讨论

[IAMOI R4] 皇后

Description

Little T likes to play chess, so Little Y decides to give her a challenge.

Given an n×mn \times m chessboard, Little Y places a queen^\dag at position (x1,y1)(x_1, y_1). Little T needs to move the queen to (x2,y2)(x_2, y_2) in as few moves as possible.

To increase the difficulty, Little Y will place an obstacle on a single square, other than the starting and ending squares. The queen cannot pass through this obstacle. Little Y wants to place this obstacle to maximize the number of moves Little T needs.

Assuming both players adopt their optimal strategies, what is the number of moves Little T will take?

^\dag: The queen is a piece in international chess. In a single move, it can move any number of squares in one of the eight directions (horizontally, vertically, and diagonally).

Input Format

This problem contains multiple test cases.

The first line of the input contains an integer TT, representing the number of test cases.

This is followed by TT test cases. For each test case, a single line contains six positive integers: n,m,x1,y1,x2,y2n, m, x_1, y_1, x_2, y_2.

Output Format

For each test case, output a single line containing an integer, which is the answer.

3
2 2 1 1 1 1
3 3 1 1 3 3
2 5 1 1 1 5
0
2
3

Hint

【Sample Explanation】

For the first test case, the queen is already at the target position, so 0 moves are needed.

For the second test case, Little Y might place the obstacle at (2,2)(2,2). Little T can then move the queen first to (3,1)(3,1) and then to (3,3)(3,3), taking 2 moves.

For the third test case, Little Y might place the obstacle at (1,2)(1,2). Little T can then move the queen first to (2,1)(2,1), then to (2,5)(2,5), and finally to (1,5)(1,5), taking 3 moves.

【Data Constraints】

Test Case # nn \le mm \le Special Properties
11 22 None
22 33 ^
33 44
44 55
55 22 100100
66 100100 ^
77 22 10910^9 x1=x2x_1=x_2
88 10910^9 ^ ^
99 22 None
1010 10910^9 ^

For all test cases, it is guaranteed that: 1T201 \le T \le 20, 2n,m1092 \le n, m \le 10^9.