def min_distance(x, y, target_x, target_y):
return abs(x - target_x) + abs(y - target_y)
def solve(N, coins, targets, index=0, total_cost=0):
if index == len(coins):
return total_cost
x, y = coins[index]
min_cost = float('inf')
for target_x, target_y in targets:
cost = min_distance(x, y, target_x, target_y)
new_targets = targets.copy()
new_targets.remove((target_x, target_y))
min_cost = min(min_cost, solve(N, coins, new_targets, index + 1, total_cost + cost))
return min_cost
def main():
import sys
input = sys.stdin.read
data = input().split()
N = int(data[0])
coins = [(int(data[i * 2 + 1]), int(data[i * 2 + 2])) for i in range(2 * N)]
targets = [(x, y) for x in range(1, N + 1) for y in range(1, 3)]
result = solve(N, coins, targets)
print(result)
Compilation message (stdout)
Compiling 'joi2019_ho_t4.py'...
=======
adding: __main__.pyc (deflated 42%)
=======
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |