CF1804A.Lame King
入门
通过率:0%
时间限制:1.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a checkerboard of size 201×201, i. e. it has 201 rows and 201 columns. The rows of this checkerboard are numbered from −100 to 100 from bottom to top. The columns of this checkerboard are numbered from −100 to 100 from left to right. The notation (r,c) denotes the cell located in the r-th row and the c-th column.
There is a king piece at position (0,0) and it wants to get to position (a,b) as soon as possible. In this problem our king is lame. Each second, the king makes exactly one of the following five moves.
- Skip move. King's position remains unchanged.
- Go up. If the current position of the king is (r,c) he goes to position (r+1,c).
- Go down. Position changes from (r,c) to (r−1,c).
- Go right. Position changes from (r,c) to (r,c+1).
- Go left. Position changes from (r,c) to (r,c−1).
King is not allowed to make moves that put him outside of the board. The important consequence of the king being lame is that he is not allowed to make the same move during two consecutive seconds. For example, if the king goes right, the next second he can only skip, go up, down, or left.
What is the minimum number of seconds the lame king needs to reach position (a,b)?
你有一个 201×201 的棋盘,即它有 201 行和 201 列。该棋盘的行从下到上编号为 −100 至 100;列从左到右编号为 −100 至 100。记号 (r,c) 表示位于第 r 行、第 c 列的格子。
一个国王棋子初始位于位置 (0,0),目标是尽快到达位置 (a,b)。本题中,该国王是“跛足”的。每秒,国王恰好执行以下五种移动之一:
- 跳过(Skip)移动:国王位置保持不变;
- 向上移动:若国王当前位于 (r,c),则移动至 (r+1,c);
- 向下移动:位置由 (r,c) 变为 (r−1,c);
- 向右移动:位置由 (r,c) 变为 (r,c+1);
- 向左移动:位置由 (r,c) 变为 (r,c−1)。
国王不允许进行使其移出棋盘边界的移动。此外,“跛足”国王的重要限制是:不能在连续两秒内执行相同的移动。例如,若国王向右移动,则下一秒他只能选择跳过、向上、向下或向左移动。
问:该跛足国王到达位置 (a,b) 所需的最少秒数是多少?
输入格式
The first line of the input contains a single integer t (1≤t≤104) — the number of test cases. Then follow t lines containing one test case description each.
Each test case consists of two integers a and b (−100≤a,b≤100) — the position of the cell that the king wants to reach. It is guaranteed that either a=0 or b=0.
输入的第一行包含一个整数 t(1≤t≤104),表示测试用例的数量。随后是 t 行,每行描述一个测试用例。
每个测试用例包含两个整数 a 和 b(−100≤a,b≤100),表示国王希望到达的格子的位置。保证 a=0 或 b=0 中至少有一个成立。
输出格式
Print t integers. The i-th of these integers should be equal to the minimum number of seconds the lame king needs to get to the position he wants to reach in the i-th test case. The king always starts at position (0,0).
输出 t 个整数。其中第 i 个整数应等于瘸腿国王在第 i 个测试用例中到达目标位置所需的最少秒数。国王始终从位置 (0,0) 出发。
输入输出样例
输入#1
5 -4 1 4 4 0 -6 -5 -4 7 -8
输出#1
7 8 11 9 15
说明/提示
One of the possible solutions for the first example is: go down, go right, go down, go right, go down, go left, go down.
One of the possible solutions for the second example is to alternate "go right" and "go up" moves 4 times each.
One of the possible solutions for the third example is to alternate "go left" and "skip" moves starting with "go left". Thus, "go left" will be used 6 times, and "skip" will be used 5 times.
第一个样例的一种可能解法是:向下、向右、向下、向右、向下、向左、向下。
第二个样例的一种可能解法是交替执行“向右”和“向上”操作,各执行 4 次。
第三个样例的一种可能解法是交替执行“向左”和“跳过”操作,起始动作为“向左”。因此,“向左”将被执行 6 次,“跳过”将被执行 5 次。
输入解题思路,AI测评打分。不知道怎么写?