[CF2152B]B. Catching the Krug题解

B. Catching the Krug

time limit per test

1 second

memory limit per test

1024 megabytes

Doran and the Krug are playing a game on a grid consisting of (n+1)×(n+1) cells whose coordinates are pairs of integers from 0 to n, inclusive. The Krug's goal is not to be caught by Doran for as long as possible, while Doran's goal is to catch the Krug as early as possible. We say Doran caught the Krug if they stand on the same grid cell.

To play the game, the Krug and Doran take turns alternately, starting from the Krug:

  • The Krug can either stay in the same cell or move to a cell vertically or horizontally (but not diagonally) adjacent. Formally, if the Krug is currently at the cell (a,b), she can stay at (a,b) or move to either (a−1,b),(a,b−1),(a,b+1),(a+1,b).
  • Doran can either stay in the same cell or move to a cell vertically, horizontally, or diagonally adjacent. Formally, if Doran is currently at the cell (c,d), he can stay at (c,d) or move to either (c−1,d−1),(c−1,d),(c−1,d+1),(c,d−1),(c,d+1),(c+1,d−1),(c+1,d),(c+1,d+1).
  • Both players cannot go outside of the grid.

Figures showing possible movements of the Krug and Doran. Letters 'K' and 'D' represent the current position of the Krug and Doran, and the colored cells represent possible positions each player can move to in their respective turn.

The Krug's survival time is defined as the number of Doran's turns until Doran catches the Krug for the given starting cells of the players. Assuming that both players play optimally, find the Krug's survival time or report that the Krug can survive for infinite turns.

讯飞听见 翻译

多兰和克鲁格在一个由 (n+1)×(n+1) 个单元格组成的网格上玩游戏,这些单元格的坐标是从 0 到 n 的整数对。包容。克鲁格的目标是尽可能长时间地不被多兰抓住,而多兰的目标是尽可能早地抓住克鲁格。如果多兰和克鲁格站在同一个网格单元格上,我们就说他们抓住了克鲁格。为了玩这个游戏,克鲁格和多兰轮流交替。

从克鲁格开始:

-Krug可以停留在同一个单元中,也可以垂直或水平移动到一个单元中(但不是对角)

相邻的。形式上,如果克鲁格目前在牢房 (a,b) ,她可以留在 (a,b) 或搬到 (a−1,b),(a,b−1),(a,b+1),(a+1,b) 。-多兰可以停留在同一单元格中,也可以移动到垂直、水平或对角相邻的单元格中。

形式上,如果多兰目前在牢房 (c,d) ,他可以留在 (c,d) 或搬到 (c−1,d−1),(c−1,d),(c−1,d+1),(c,d−1),(c,d+1),(c+1,d−1),(c+1,d),(c+1,d+1) 。-两名球员都不能离开发车区。

显示克鲁格和多兰可能移动的数字。字母“K”和“D”代表克鲁格和多兰的当前位置。并且彩色单元表示每个玩家在他们各自的回合中可以移动到的可能位置。克鲁格的生存时间被定义为多兰的回合数,直到多兰为给定的玩家的起始细胞抓住克鲁格。

假设两个玩家都玩得最好,找出克鲁格的生存时间或报告克鲁格可以生存无限回合。

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.

Each test case consists of a single line containing five integers n, rK, cK, rD, and cD (1≤n≤109, 0≤rK,cK,rD,cD≤n, (rK,cK)≠(rD,cD)) — n is the size of the grid, (rK,cK) represents the Krug's starting cell, and (rD,cD) represents Doran's starting cell.

讯飞听见 翻译

输入

每个测试包含多个测试用例。第一行包含测试用例的数量 t ( 1≤t≤104 )。测试用例的描述如下。

每个测试用例由包含五个整数 n 、 rK 、 cK 、 rD 和 cD ( 1≤n≤109 , 0≤rK,cK,rD,cD≤n , (rK,cK)≠(rD,cD) )的单行组成— n 是网格的大小, (rK,cK) 表示Krug的起始单元格,而 (rD,cD) 表示Doran的起始单元格。

Output

For each test case, output the Krug's survival time when both players play optimally. If the Krug can survive for infinite turns, print −1.

讯飞听见 翻译

输出

对于每个测试用例,当两个玩家都处于最佳状态时,输出克鲁格的生存时间。如果克鲁格可以存活无限回合,则打印 −1 。

Example

Input

Copy



7

2 0 0 1 1

3 1 1 0 1

1 1 0 0 1

6 1 3 3 2

9 4 1 4 2

82 64 2 63 2

1000000000 500000000 500000000 1000000000 0

Output

Copy



1

3

1

4

2

19

1000000000

Note

Explanation of the first test case:

The Krug starts at (0,0) and Doran starts at (1,1). On the Krug's turn, she can only move to (0,0), (0,1), or (1,0).

Doran, starting at (1,1), can reach any cell on the 3×3 grid in a single move. Therefore, no matter where the Krug moves, Doran can always move to her cell on his first turn. The Krug's survival time is 1.

Explanation of the second test case:

The Krug starts at (1,1) and Doran at (0,1). To survive Doran's first turn, the Krug must move to a cell that Doran cannot reach. The only such cell is (2,1).

After the Krug moves to (2,1), Doran moves to (1,1) to get closer.

Now, for her second move, the Krug must again move to a cell Doran can't reach, which is (3,1). Doran then moves to (2,1).

At this point, no matter where the Krug moves for her third turn, Doran can always reach her cell. It can be shown that this is an optimal strategy for both, and therefore the Krug's survival time is 3.

讯飞听见 翻译

注意

第一个测试用例说明:

克鲁格从 (0,0) 开始,多兰从 (1,1) 开始。

轮到克鲁格时,她只能移动到 (0,0) 、 (0,1) 或 (1,0) 。从 (1,1) 开始,多兰可以在一次移动中到达 3×3 网格上的任何单元格。

因此,无论克鲁格移动到哪里,多兰总是可以在他的第一轮移动到她的牢房。克鲁格的存活时间是 1 。

!

第二个测试用例说明:

克鲁格从 (1,1) 开始,多兰从 (0,1) 开始。

为了在多兰的第一轮中生存下来,克鲁格必须移动到多兰无法到达的牢房中。唯一这样的单元格是 (2,1) 。在克鲁格移动到 (2,1) 后,多兰移动到 (1,1) 以更接近。

现在,在她的第二次行动中,克鲁格必须再次移动到多兰无法到达的牢房,即 (3,1) 。然后多兰移动到 (2,1) 。

在这一点上,无论克鲁格在第三个回合中移动到哪里,多兰总能到达她的牢房。

可以证明这是两者的最优策略,因此Krug的生存时间是 3 。

com/884d3355d039dc35594ce536d991cb8093ce1bc0.PNG)

思路

分类讨论+贪心。

代码见下

#include<bits/stdc++.h>
using namespace std;
long long t,n,rk,ck,rd,cd;
int main(){
	cin>>t;
	while(t--){
		cin>>n>>rk>>ck>>rd>>cd;
		if(rd<=rk&&cd<=ck){
			if(rk==rd){
				cout<<n-cd<<endl;
			}
			else if(ck==cd){
				cout<<n-rd<<endl;
			}
			else{
				cout<<max(n-cd,n-rd)<<endl;
			}
		}
		else if(rd>=rk&&cd>=ck){
			if(rk==rd){
				cout<<cd<<endl;
			}
			else if(ck==cd){
				cout<<rd<<endl;
			}
			else{
				cout<<max(cd,rd)<<endl;
			}
		}
		else if(rd<=rk&&cd>=ck){
			if(rk==rd){
				cout<<cd<<endl;
			}
			else if(ck==cd){
				cout<<n-rd<<endl;
			}
			else{
				cout<<max(n-rd,cd)<<endl;
			}			
		}
		else{
			if(rk==rd){
				cout<<n-cd<<endl;
			}
			else if(ck==cd){
				cout<<rd<<endl;
			}
			else{
				cout<<max(rd,n-cd)<<endl;
			}				
		}
	}
	return 0;
}

posted @ 2025-10-09 11:52  bz02_2023f2  阅读(3)  评论(0)    收藏  举报  来源