← Practice problem bank

Jan 7, 2026

The Enchanted Forest Walk

Daily Challenge Archive · Open

easygraphs

100 pts · 10000 ms · 128 MB · Practice only

Problem

Welcome to the mystical Enchanted Forest, where each tree glows at a unique coordinate on a 2D plane. As a daring adventurer, your mission is to collect a rare fruit from each glowing tree. You begin your journey at the origin point (0, 0). Your task is to devise a route that allows you to visit each tree exactly once, collect the fruits, and return to the origin, while ensuring the total distance traveled is minimized. This problem is a simplified version of the Traveling Salesman Problem, crucial for efficient route planning. To solve this challenge, follow these steps: 1. Identify the coordinates of each glowing tree. 2. Plan a route that visits all the trees, minimizing the distance traveled. 3. Ensure you return to the starting point (0, 0) after collecting all the fruits. Consider this

Examples
If trees are located at coordinates (2, 3), (5, 8), and (6, 1), your goal is to determine the shortest path that visits each point once and returns to (0, 0). Edge cases to consider include: - Trees located at the same coordinates, which might simplify the problem. - Large numbers of trees, which increase the complexity.
Hints
  • Consider using algorithms like nearest neighbor or dynamic programming to find an optimal path.
  • Think about how to efficiently calculate distances between points; a hash map might be useful for storing and retrieving these distances quickly.
  • Pay attention to edge cases, such as very few or very many trees, and how they might affect your approach.
  • For optimization, focus on reducing the computational complexity of your solution, especially as the number of trees increases.

Input format

The first line contains a single integer n (1 <= n <= 100) representing the number of magical trees. Each of the next n lines contains two integers x and y (-1000 <= x, y <= 1000) representing the coordinates of a tree.

Output format

Output a single integer, the shortest possible path that covers all trees, starting at the origin.

Constraints

1 <= n <= 100, -1000 <= x, y <= 1000

Sample input

3
0 2
2 0
1 1

Sample output

4

Sign in to submit solutions

Run your code against all test cases and get instant feedback.

Sign in