문제번호: 1002
문제명: 터렛
문제링크: https://www.acmicpc.net/problem/1002
문제내용과 예제 입/출력은 위 문제 링크에서 확인해 주시기 바랍니다
오늘은 '터렛' 문제를 풀었습니다. 두 플레이어가 상대편의 좌표 거리를 알아냈고 이 거리를 이용해서 상대방이 있을 수 있는 위치의 개수를 구하는 문제입니다. 저는 다음과 같이 판별조건을 나누었습니다.
무한개일 때(-1): 거리가 같고 가 같은 위치에 있을 때
1개일 때: r1+ r2 == distance, distance == 거리 차이
2개일 때: distance < r1 + r2이고 (subtract < distance) 일 때
모두 다 아니라면 0 출력하기.
다음은 정답코드입니다.
static void Main()
{
using StreamReader sr = new StreamReader(Console.OpenStandardInput());
using StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
int n = int.Parse(sr.ReadLine());
for (int i = 0; i < n; i++)
{
int[] nums = Array.ConvertAll(sr.ReadLine().Split(), int.Parse);
int x1 = nums[0];
int y1 = nums[1];
int r1 = nums[2];
int x2 = nums[3];
int y2 = nums[4];
int r2 = nums[5];
int subtract = Math.Abs(r1 - r2);
float distance = MathF.Sqrt(MathF.Pow(x2 - x1, 2) + MathF.Pow(y2 - y1, 2));
int result = 0;
if (distance == 0 && r1 == r2) result = -1;
else if (distance < r1 + r2 && (subtract < distance)) result = 2;
else if (distance == r1 + r2 || distance == subtract) result = 1;
else result = 0;
sw.WriteLine(result);
}
}
'알고리즘 풀이' 카테고리의 다른 글
| [C#] 백준 14911번 문제풀이 (0) | 2026.04.01 |
|---|---|
| [C#] 백준 18870번 문제풀이 (0) | 2026.03.25 |
| [C#] 백준 9465번 문제풀이 (0) | 2026.03.22 |
| [C#] 백준 15311번 문제풀이 (0) | 2026.03.21 |
| [C#] 백준 33846번 문제풀이 (0) | 2026.03.20 |