CF1713A.Traveling Salesman Problem
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are living on an infinite plane with the Cartesian coordinate system on it. In one move you can go to any of the four adjacent points (left, right, up, down).
More formally, if you are standing at the point (x,y), you can:
- go left, and move to (x−1,y), or
- go right, and move to (x+1,y), or
- go up, and move to (x,y+1), or
- go down, and move to (x,y−1).
There are n boxes on this plane. The i-th box has coordinates (xi,yi). It is guaranteed that the boxes are either on the x-axis or the y-axis. That is, either xi=0 or yi=0.
You can collect a box if you and the box are at the same point. Find the minimum number of moves you have to perform to collect all of these boxes if you have to start and finish at the point (0,0).
你生活在一个带有笛卡尔坐标系的无限平面上。每一步,你可以移动到四个相邻点之一(左、右、上、下)。
更准确地说,若你当前位于点 (x,y),则你可以:
- 向左移动至 (x−1,y);
- 向右移动至 (x+1,y);
- 向上移动至 (x,y+1);
- 向下移动至 (x,y−1)。
平面上共有 n 个箱子。第 i 个箱子的坐标为 (xi,yi)。保证所有箱子均位于 x 轴或 y 轴上,即对每个 i,均有 xi=0 或 yi=0。
当你与某个箱子处于同一点时,即可收集该箱子。若你必须从点 (0,0) 出发,并最终返回点 (0,0),求收集全部箱子所需的最少移动步数。
输入格式
The first line contains a single integer t (1≤t≤100) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤100) — the number of boxes.
The i-th line of the following n lines contains two integers xi and yi (−100≤xi,yi≤100) — the coordinate of the i-th box. It is guaranteed that either xi=0 or yi=0.
Do note that the sum of n over all test cases is not bounded.
第一行包含一个整数 t(1≤t≤100)—— 表示测试用例的数量。
每个测试用例的第一行包含一个整数 n(1≤n≤100)—— 表示盒子的数量。
接下来的 n 行中,第 i 行包含两个整数 xi 和 yi(−100≤xi,yi≤100)—— 表示第 i 个盒子的坐标。保证对每个 i,均有 xi=0 或 yi=0。
请注意:所有测试用例的 n 之和没有上界。
输出格式
For each test case output a single integer — the minimum number of moves required.
对于每个测试用例,输出一个整数——所需的最少移动次数。
输入输出样例
输入#1
3 4 0 -2 1 0 -1 0 0 2 3 0 2 -3 0 0 -1 1 0 0
输出#1
12 12 0
说明/提示
In the first test case, a possible sequence of moves that uses the minimum number of moves required is shown below.
$$(0,0) \to (1,0) \to (1,1) \to (1, 2) \to (0,2) \to (-1,2) \to (-1,1) \to (-1,0) \to (-1,-1) \to (-1,-2) \to (0,-2) \to (0,-1) \to (0,0)$$
In the second test case, a possible sequence of moves that uses the minimum number of moves required is shown below.
$$(0,0) \to (0,1) \to (0,2) \to (-1, 2) \to (-2,2) \to (-3,2) \to (-3,1) \to (-3,0) \to (-3,-1) \to (-2,-1) \to (-1,-1) \to (0,-1) \to (0,0)$$
In the third test case, we can collect all boxes without making any moves.
在第一个测试用例中,以下是一种使用最少移动次数的可能移动序列:
$$(0,0) \to (1,0) \to (1,1) \to (1, 2) \to (0,2) \to (-1,2) \to (-1,1) \to (-1,0) \to (-1,-1) \to (-1,-2) \to (0,-2) \to (0,-1) \to (0,0)$$
在第二个测试用例中,以下是一种使用最少移动次数的可能移动序列:
$$(0,0) \to (0,1) \to (0,2) \to (-1, 2) \to (-2,2) \to (-3,2) \to (-3,1) \to (-3,0) \to (-3,-1) \to (-2,-1) \to (-1,-1) \to (0,-1) \to (0,0)$$
在第三个测试用例中,我们无需进行任何移动即可收集所有箱子。
输入解题思路,AI测评打分。不知道怎么写?