Farmer John has the following manure movement options:
Drive directly from to , covering the distance ;
Drive a tractor from to , teleport from to , and then drive the tractor again from to . The total distance covered by the tractor is |a - x| + |b - y|;
Drive a tractor from to , teleport from to , and then drive the tractor again from to . The total distance covered by the tractor is |a - y| + |b - x|;
The smallest of these three values will be the answer.
Example
In the given example, the best strategy is to haul the manure from position to position , teleport it to position , and then haul it to position . The total distance covered by the tractor is therefore 1 + 2 = 3.
Algorithm implementation
Read the input data.
scanf("%d %d %d %d", &a, &b, &x, &y);Find the answer — the minimum of the three values.
res = abs(a - b);
res = min(res, abs(a - x) + abs(b - y));
res = min(res, abs(a - y) + abs(b - x));Print the answer.
printf("%d\n", res);Python implementation
Read the input data.
a, b, x, y = map(int, input().split())Find the answer — the minimum of the three values.
res = abs(a - b)
res = min(res, abs(a - x) + abs(b - y))
res = min(res, abs(a - y) + abs(b - x))Print the answer.
print(res)