blog.itcode.devblog.itcode.dev

[Baekjoon / JAVA] Baekjoon Algorithm 1002 Turret

Kyuhyun Cho and Seunghwan Baek are employees working at the Turret. But their presence is so faint that they don't even count toward the population. Below are photos of Kyuhyun Cho and Seunghyun Baek.

[Baekjoon / JAVA] Baekjoon Algorithm 1002 Turret

Kyuhyun Cho and Seunghwan Baek are employees working at the Turret. But their presence is so faint that they don't even count toward the population. Below are photos of Kyuhyun Cho and Seunghyun Baek.
RWB0104
@RWBwritten at 2021-05-21 12:56:10
Baekjoon Algorithm

시리즈 모아보기

Baekjoon Algorithm

3 / 22
RankLanguage Used

🖼️ JAVA

🔗 Full Problem 1002

Time LimitMemory Limit
2 sec128MB

Kyuhyun Cho and Seunghwan Baek are employees working at the Turret. But their presence is so faint that they don't even count toward the population. Below are photos of Kyuhyun Cho and Seunghyun Baek.

Seokwon Lee ordered Kyuhyun Cho and Seunghwan Baek to calculate the location of the enemy marine (Jaeyoung Ryu). Kyuhyun Cho and Seunghwan Baek each calculated the distance from their own turret's position to the current enemy.
Given Kyuhyun Cho's coordinates (x1,y1)(x_1, y_1) and Seunghwan Baek's coordinates (x2,y2)(x_2, y_2), along with the distance r1r_1 that Kyuhyun Cho calculated to Jaemyung Ryu and the distance r2r_2 that Seunghwan Baek calculated to Jaemyung Ryu, write a program that prints the number of possible coordinates where Jaemyung Ryu could be located.

The first line gives the number of test cases T. Each test case consists of the following.
One line gives x1x_1, y1y_1, r1r_1, x2x_2, y2y_2, r2r_2. x1x_1, y1y_1, x2x_2, y2y_2 are points greater than or equal to -10,000 and less than or equal to 10,000, and r1r_1, r2r_2 are natural numbers less than or equal to 10,000.

For each test case, print the number of possible positions where Jaemyung Ryu could be located. If the number of possible positions is infinite, print -1.

  • Input

TC

3
0 0 13 40 0 37
0 0 30 0 7 4
1 1 1 1 1 5
  • Output

TC

2
1
0

The example uses people's names for its elements, but since that gets in the way of understanding the problem, it can be simply restated as follows.
There are points AA, BB, CC at arbitrary positions, and we're given the distance between AA and CC, and the distance between BB and CC.
The task is to find the number of points where CC could actually be located. In other words, this can simply be defined as the problem of finding the intersection points of two circles.
There's a condition that if the circles overlap perfectly, since there are infinitely many such points, the answer should be expressed as -1.

This can be illustrated as follows.

The variables can be organized as follows.

nnxnx_nyny_nrnr_n
1A's x-coordinateA's y-coordinateA's radius
2B's x-coordinateB's y-coordinateB's radius

The cases can be broken down into 6 total categories.

  1. The two circles overlap exactly (-1)
  2. The two circles overlap without touching (0)
  3. The two circles don't overlap and don't touch (0)
  4. The two circles overlap and are tangent (1)
  5. The two circles don't overlap but are tangent (1)
  6. The two circles overlap without touching (2)

This solution proceeds using the distance (distancedistance) between x1x_1, y1y_1 and x2x_2, y2y_2, along with the sum (sumsum) of r1r_1 and r2r_2, and the absolute value of their difference (subsub).

distance=(x1x2)2+(y1y2)2distance = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2} sum=r1+r2sum = r_1 + r_2 sub=r1r2sub = \vert r_1 - r_2 \vert
  • Case 1 - The two circles overlap exactly

    This is the situation where the position and radius of both circles are identical.
    This holds when distancedistance is 0 and the lengths of r1r_1 and r2r_2 are equal.

  • Case 2 - The two circles overlap without touching

    This is the situation where the distance between the origins of the two circles is shorter than the difference of the radii.
    This holds when distance<subdistance < sub.

  • Case 3 - The two circles don't overlap and don't touch

    This is the situation where the distance between the origins of the two circles is greater than the sum of the radii.
    This holds when distance>sumdistance > sum.

  • Case 4 - The two circles overlap and are tangent

    This is the situation where the distance between the origins of the two circles matches the difference of the radii.
    This holds when distance==subdistance == sub.

  • Case 5 - The two circles don't overlap but are tangent

    This is the situation where the distance between the origins of the two circles matches the sum of the radii.
    This holds when distance==sumdistance == sum.

  • Case 6 - The two circles overlap without touching

    This is the situation where the two circles overlap each other to some moderate degree.
    This holds when distance<sumdistance < sum &&\&\& distance>subdistance > sub.

Even without expressing it as a formula, you can simply apply this to any situation that doesn't match the five cases above.
The above cases can be appropriately expressed using if statements. A switch statement determines branching based on a single variable, so it's somewhat unsuitable for applying to this algorithm.

JAVA

import java.util.Scanner;

/**
 * Baekjoon Problem 1002 algorithm class
 *
 * @author RWB
 * @see <a href="https://blog.itcode.dev/posts/2021/05/21/a1002">1002 solution</a>
 * @since 2021.04.21 Wed 21:56:10
 */
public class Main
{
	/**
	 * Main function
	 *
	 * @param args: [String[]] parameters
	 */
	public static void main(String[] args)
	{
		Scanner scanner = new Scanner(System.in);
		
		int length = scanner.nextInt();
		
		for (int i = 0; i < length; i++)
		{
			int x1 = scanner.nextInt();
			int y1 = scanner.nextInt();
			int r1 = scanner.nextInt();
			
			int x2 = scanner.nextInt();
			int y2 = scanner.nextInt();
			int r2 = scanner.nextInt();
			
			System.out.println(calcPoints(x1, y1, r1, x2, y2, r2));
		}
	}
	
	/**
	 * Function that returns the number of intersection points
	 *
	 * case 1 - The two circles overlap exactly (-1)
	 * case 2 - The two circles overlap without touching (0)
	 * case 3 - The two circles don't overlap and don't touch (0)
	 * case 4 - The two circles overlap and are tangent (1)
	 * case 5 - The two circles don't overlap but are tangent (1)
	 * case 6 - The two circles overlap without touching (2)
	 *
	 * @param x1: [int] A's x-coordinate
	 * @param y1: [int] A's y-coordinate
	 * @param r1: [int] distance between A and C
	 * @param x2: [int] B's x-coordinate
	 * @param y2: [int] B's y-coordinate
	 * @param r2: [int] distance between B and C
	 *
	 * @return [int] number of intersection points
	 */
	private static int calcPoints(int x1, int y1, int r1, int x2, int y2, int r2)
	{
		// Formula to calculate the distance between two points
		double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
		
		int sum = r1 + r2;
		int sub = Math.abs(r1 - r2);
		
		// case 1 - The two circles overlap exactly
		if (distance == 0 && r1 == r2)
		{
			return -1;
		}
		
		// case 2 - The two circles overlap without touching
		else if (distance < sub)
		{
			return 0;
		}
		
		// case 3 - The two circles don't overlap and don't touch
		else if (distance > sum)
		{
			return 0;
		}
		
		// case 4 - The two circles overlap and are tangent
		else if (distance == sub)
		{
			return 1;
		}
		
		// case 5 - The two circles don't overlap but are tangent
		else if (distance == sum)
		{
			return 1;
		}
		
		// case 6 - The two circles overlap without touching
		else
		{
			return 2;
		}
	}
}
  • Math
  • Geometry
# Baekjoon# Algorithm# JAVA# Geometry# SILVER# SILVER IV
ship
blog.itcode.dev

Notes from the π-th Alpaca

7.0.1
Developed by RWB since 2021.057th upgraded at 2026.08