- 제목
Traveling Salesman Problem
- 조건
time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output
- 문제
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).
- 입력
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.
- 출력
For each test case output a single integer — the minimum number of moves required.
예제 입력1 | 예제 출력1 |
3 4 0 -2 1 0 -1 0 0 2 3 0 2 -3 0 0 -1 1 0 0 |
12 12 0 |
Note
In the first test case, a possible sequence of moves that uses the minimum number of moves required is shown below.
In the second test case, a possible sequence of moves that uses the minimum number of moves required is shown below.
In the third test case, we can collect all boxes without making any moves.
#include <iostream>
#include <algorithm>
using namespace std;
#define endl '\n'
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc, sz, x, y;
int px, nx, py, ny;
cin >> tc;
while(tc--){
cin >> sz;
px = -1; nx = 1;
py = -1, ny = 1;
for(int n = 0 ; n < sz ; n++){
cin >> x >> y;
if(x == 0){
if(y > 0) py = max(py, y);
else ny = min(ny, y);
}
if(y == 0){
if(x > 0) px = max(px, x);
else nx = min(nx, x);
}
}
int move = 0;
if(py != -1) move += py;
if(px != -1) move += px;
if(nx != 1) move += -nx;
if(ny != 1) move += -ny;
move *= 2;
cout << move << endl;
}
return 0;
}