[Baekjoon / JAVA] Baekjoon Algorithm #1007 Vectors
[Baekjoon / JAVA] Baekjoon Algorithm #1007 Vectors
| Rank | Language Used |
|---|---|
🖼️ JAVA |
| Time Limit | Memory Limit |
|---|---|
| 2 sec | 512MB |
There are points marked on a plane, and let's call this set of points . A vector matching of set is a set of vectors, where every vector starts at one point in set and ends at another point. Also, every point belonging to must be used exactly once.
The number of vectors in is half the number of points in .
Given the points on the plane, write a program that outputs the minimum length of the sum of the vectors in the vector matching of set .
The first line contains the number of test cases . Each test case is structured as follows.
The first line of each test case gives the number of points . is even. From the second line, lines follow, each giving the coordinates of a point. is a natural number less than or equal to 20, and the absolute value of each coordinate is an integer less than or equal to 100,000. All points are distinct.
Print the answer for each test case. An absolute/relative error of up to is allowed.
- Input
TC
2 4 5 5 5 -5 -5 5 -5 -5 2 -100000 -100000 100000 100000
- Output
TC
0.000000000000 282842.712474619038
You can form a single vector from two points. Since , the maximum number of given points is 20. Assuming , the number of vectors we can form is half that, or 10. Depending on how we connect the 20 points, there are many different combinations for creating 10 vectors. Among these possible combinations, calculating the smallest total sum of vectors is the result of this algorithm. (Note this is not calculating the shortest of the 10 vectors.)
The core of this algorithm is calculating, among all the possible ways to form vectors from elements, the minimum value. Since the maximum value of is a very small 20, it's feasible to compare each possibility individually. This is also why the algorithm itself is classified as Brute Force in the first place.
Suppose we have coordinates , and from these coordinates we form two vectors and (with the same conditions as in the algorithm problem). Suppose is formed from , and is formed from . Expressing each vector via its coordinates gives us the following.
The sum of vectors is simply the sum of the vector coordinates.
In other words, the total sum can be expressed as follows.
The minimum value computed with the formula above is the answer to the algorithm. In other words, we need to combine according to the given condition. We calculate for each combination and return the minimum among them.
Blindly forming all 10 vectors by looping through them won't work. Let's think of a more efficient way to calculate the vectors.
Looking closely at the formula for , you can find a useful characteristic: for each coordinate , , half the coordinates are added and half are subtracted. With 4 coordinates, 2 are added and the other 2 subtracted. What if there were 10? 5 would be added, 5 subtracted.
Using this, what if we computed all combinations of coordinates out of the total coordinates? We could divide them into coordinates to be added and coordinates to be subtracted. Then, by adding and subtracting each accordingly, we could easily compute .
Therefore, the core of this algorithm is splitting the points in half and finding all the ways to choose points to be used in positive operations versus points to be used in negative operations. Using (Combination) makes this easy to compute. Calculate , add the chosen coordinates, and subtract the unchosen coordinates.
For example 1, the possibilities are as follows.
| Positive coords | Negative coords | ||
|---|---|---|---|
| (5, 5), (5, -5) | (-5, 5), (-5, -5) | (20, 0) | 20 |
| (5, 5), (-5, 5) | (5, -5), (-5, -5) | (0, 20) | 20 |
| (5, 5), (-5, -5) | (5, -5), (-5, 5) | (0, 0) | 0 |
| (5, -5), (-5, 5) | (5, 5), (-5, -5) | (0, 0) | 0 |
| (5, -5), (-5, -5) | (5, 5), (-5, 5) | (0, -20) | 20 |
| (-5, 5), (-5, -5) | (5, 5), (5, -5) | (-20, 0) | 20 |
For this reason, the minimum total sum of vectors in example 1 is 0.
JAVA
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Baekjoon problem #1007 algorithm class * * @author RWB * @see <a href="https://blog.itcode.dev/posts/2021/06/09/a1007">1007 solution</a> * @since 2021.06.09 Tue 00:50:26 */ public class Main { // Result private static double result; // Whether each element is selected in the current combination private static boolean[] isChecked; // Array of points private static int[][] P; /** * Main function * * @param args: [String[]] arguments * * @throws IOException data input/output exception */ public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); // Number of cases int T = Integer.parseInt(reader.readLine()); for (int i = 0; i < T; i++) { // Number of points int N = Integer.parseInt(reader.readLine()); result = Double.MAX_VALUE; isChecked = new boolean[N]; P = new int[N][2]; for (int j = 0; j < N; j++) { String[] temp = reader.readLine().split(" "); P[j][0] = Integer.parseInt(temp[0]); P[j][1] = Integer.parseInt(temp[1]); } combination(0, N / 2); System.out.println(result); } reader.close(); } /** * Combination function * * @param index: [int] index * @param count: [int] number of elements left to combine */ private static void combination(int index, int count) { // If there are no more elements left to combine if (count == 0) { result = Math.min(result, getVector()); } // If there are still elements left to combine else { for (int i = index; i < P.length; i++) { isChecked[i] = true; combination(i + 1, count - 1); isChecked[i] = false; } } } /** * Function that computes the vector magnitude * * @return [double] vector magnitude */ private static double getVector() { int x = 0; int y = 0; for (int i = 0; i < P.length; i++) { // If the point is selected as positive if (isChecked[i]) { x += P[i][0]; y += P[i][1]; } // If the point is selected as negative else { x -= P[i][0]; y -= P[i][1]; } } return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); } }
- Math
- Brute Force Algorithm

![[Jekyll] Building My Own Blog with GitHub Pages - 4. Shopping for Jekyll Themes](https://user-images.githubusercontent.com/50317129/90983201-582f1080-e5a7-11ea-970b-8d7d82cb2084.png)