2024. 10. 29. 16:04ㆍIT/BaekJoon
문제 링크
https://www.acmicpc.net/problem/15751
문제
One of the farming chores Farmer John dislikes the most is hauling around lots of cow manure. In order to streamline this process, he comes up with a brilliant invention: the manure teleporter! Instead of hauling manure between two points in a cart behind his tractor, he can use the manure teleporter to instantly transport manure from one location to another.
Farmer John's farm is built along a single long straight road, so any location on his farm can be described simply using its position along this road (effectively a point on the number line). A teleporter is described by two numbers x and y, where manure brought to location x can be instantly transported to location y, or vice versa.
Farmer John wants to transport manure from location a to location b, and he has built a teleporter that might be helpful during this process (of course, he doesn't need to use the teleporter if it doesn't help). Please help him determine the minimum amount of total distance he needs to haul the manure using his tractor.
입력
The first and only line of input contains four space-separated integers: a and b, describing the start and end locations, followed by x and y, describing the teleporter. All positions are integers in the range 0…100, and they are not necessarily distinct from each-other.
출력
Print a single integer giving the minimum distance Farmer John needs to haul manure in his tractor.
통과한 답안
namespace _15751
{
internal class Program
{
static void Main(string[] args)
{
int[] inputs = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
int a = inputs[0];
int b = inputs[1];
int x = inputs[2];
int y = inputs[3];
int distance = Math.Abs(a - b);
int telpoEnter = Math.Abs(Math.Min(a, b) - Math.Min(x, y));
int telpoExit = Math.Abs(Math.Max(a, b) - Math.Max(x, y));
int telpoDistance = telpoExit + telpoEnter;
Console.WriteLine(Math.Min(distance, telpoDistance));
}
}
}
거름을 옮겨야하는 시작 위치 a와 도착 위치 b가 주어지고
텔레포트의 입구 x와 출구 y가 주어졌을 때,
a에서 b까지 이동하는 경우 최소이동거리를 구하는 문제이다.
텔레포트를 사용해도 되고, 사용하지 않아도 되는 문제로
두 경우의 길이를 모두 구한 뒤에, 더 작은 값을 출력하도록 구현하였다.
직접 이동하는 경우에는 시작 위치와 도착 위치의 차이가 이동거리가 되며,
텔레포트를 사용하느 경우에는 a와 b의 위치에서 가까운 텔레포트까지의 거리이므로
Math.Min, Math.Max를 사용하여 입력값에 따라 식이 달라지지 않고
통일성을 유지하는 코드로 구현되도록 작성하였다.
'IT > BaekJoon' 카테고리의 다른 글
[BAEKJOON] 백준 15236: Dominos (C#) (0) | 2024.10.30 |
---|---|
[BAEKJOON] 백준 16316: Baby Bites (C#) (1) | 2024.10.29 |
[BAEKJOON] 백준 29730: 임스의 데일리 인증 스터디 (C#) (0) | 2024.10.25 |
[BAEKJOON] 백준 1755: 숫자놀이 (C#) (0) | 2024.10.24 |
[BAEKJOON] 백준 14843: 정보갓 영훈이 (C#) (0) | 2024.10.24 |