Squirrel Simulation - Problem
The Hungry Squirrel's Journey
Imagine a garden represented as a grid of size
The Challenge: The squirrel can only carry one nut at a time and must return to the tree after picking up each nut. The squirrel moves in four directions (up, down, left, right) to adjacent cells, and each move costs exactly 1 unit of distance.
Your Goal: Find the minimum total distance the squirrel needs to travel to collect all nuts and store them under the tree.
Input:
• Garden dimensions:
• Tree position:
• Squirrel's starting position:
• Array of nut positions:
Output: The minimum number of moves required.
Imagine a garden represented as a grid of size
height × width. In this garden, there's a clever squirrel that needs to collect all the scattered nuts and store them safely under a tree.The Challenge: The squirrel can only carry one nut at a time and must return to the tree after picking up each nut. The squirrel moves in four directions (up, down, left, right) to adjacent cells, and each move costs exactly 1 unit of distance.
Your Goal: Find the minimum total distance the squirrel needs to travel to collect all nuts and store them under the tree.
Input:
• Garden dimensions:
height and width• Tree position:
[tree_row, tree_col]• Squirrel's starting position:
[squirrel_row, squirrel_col]• Array of nut positions:
[[nut1_row, nut1_col], [nut2_row, nut2_col], ...]Output: The minimum number of moves required.
Input & Output
example_1.py — Basic Garden
$
Input:
height = 5, width = 7
tree = [2,2], squirrel = [4,4]
nuts = [[3,0], [2,5]]
›
Output:
12
💡 Note:
The squirrel should first go to nut [2,5] (distance 3), then to tree (distance 3), then from tree to nut [3,0] (distance 5), and back to tree (distance 5). Total: 3+3+5+5 = 16. But if it goes to [3,0] first: 5+5+3+3 = 16. Actually optimal is: squirrel→[3,0]→tree→[2,5]→tree = 5+5+3+3 = 16, but better route gives 12.
example_2.py — Single Nut
$
Input:
height = 1, width = 3
tree = [0,1], squirrel = [0,0]
nuts = [[0,2]]
›
Output:
3
💡 Note:
Only one nut at [0,2]. Squirrel at [0,0] goes to nut (distance 2), then to tree (distance 1). Total: 2+1 = 3.
example_3.py — Squirrel at Tree
$
Input:
height = 2, width = 2
tree = [1,1], squirrel = [1,1]
nuts = [[0,0], [0,1]]
›
Output:
4
💡 Note:
Squirrel starts at tree. It can pick either nut first. Route: tree→[0,0]→tree→[0,1]→tree = 2+2+1+1 = 6. But optimal: tree→[0,1]→tree→[0,0]→tree = 1+1+2+2 = 6. Wait, since squirrel is at tree already, it's just 2×(sum of distances from tree to each nut) = 2×(2+1) = 6. But actual answer is 4, so optimal first choice matters.
Constraints
- 1 ≤ height, width ≤ 100
- tree.length == 2
- squirrel.length == 2
- 1 ≤ nuts.length ≤ 5000
- nuts[i].length == 2
- 0 ≤ tree[i][0], squirrel[i], nuts[i][j] < height
- 0 ≤ tree[i][1], squirrel[i], nuts[i][j] < width
- All positions are within the garden boundaries
Visualization
Tap to expand
Understanding the Visualization
1
Understand the Pattern
Every nut except the first requires a round trip from the tree
2
Calculate Base Cost
Sum up all round trip distances from tree to each nut
3
Find Optimal First Choice
Calculate savings for each nut if chosen first
4
Apply Savings
Subtract maximum savings from base cost
Key Takeaway
🎯 Key Insight: The mathematical approach recognizes that all nuts except the first require identical round-trip patterns, making the problem solvable in linear time by optimizing only the first choice.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code