To solve the problem, we can use a priority queue (or heap) for BFS (Breadth-First Search). The priority queue will help us prioritize states with the least number of balls remaining and, among those, the least number of cracks used.
Here's the step-by-step approach:
- Define the State: Represent the balls array as a tuple (since lists are not hashable) to store states in a
visited set.
- Priority Queue: Use a priority queue where each element is a tuple containing:
- The number of balls remaining (to prioritize states with fewer balls).
- The number of cracks used (to prioritize states with fewer cracks when the number of balls is the same).
- The current state of the balls array.
- Chain Reaction Function: Implement a function to simulate the chain reactions that occur automatically when 3 or more same balls become adjacent after a crack.
- Main Loop:
- Pop the state with the highest priority (least balls left, then least cracks used).
- For each possible position where a manual crack can occur (groups of exactly 3 adjacent same balls not part of a larger group), perform the crack.
- After each crack, apply the chain reaction function.
- Add the new state to the priority queue if it hasn't been visited.
- Keep track of the least number of balls remaining and the least number of cracks used.
- Edge Case: If the initial balls array is empty, return (-1, -1).
Here's the implementation:
def crack(balls):
from collections import deque
from heapq import heappush, heappop
if not balls:
return (-1, -1)
import sys
sys.setrecursionlimit(100000)
def remove_chain_reactions(balls_list):
"""
Removes groups of 3 or more adjacent same balls recursively.
"""
changed = True
while changed:
changed = False
new_balls = []
i = 0
n = len(balls_list)
while i < n:
j = i
while j < n and balls_list[j] == balls_list[i]:
j += 1
# Now balls_list[i:j] are the same
if j - i >= 3:
# Remove this group
changed = True
i = j # Skip over this group
else:
# Keep these balls
new_balls.extend(balls_list[i:j])
i = j
balls_list = new_balls
return balls_list
import heapq
visited = set()
# The priority queue elements are tuples: (number of balls remaining, number of cracks used, balls list)
heap = []
initial_balls = tuple(balls)
heapq.heappush(heap, (len(balls), 0, initial_balls))
min_balls_remaining = len(balls)
min_cracks_used = 0 # Initialize with 0 cracks used
while heap:
balls_remaining, cracks_used, balls_state = heapq.heappop(heap)
if balls_state in visited:
continue
visited.add(balls_state)
# Update min_balls_remaining and min_cracks_used if necessary
if balls_remaining < min_balls_remaining:
min_balls_remaining = balls_remaining
min_cracks_used = cracks_used
elif balls_remaining == min_balls_remaining and cracks_used < min_cracks_used:
min_cracks_used = cracks_used
# If no balls remaining, we can stop
if balls_remaining == 0:
return (cracks_used, 0)
# Find all positions where we can perform a manual crack (groups of exactly 3 adjacent same balls not part of a larger group)
balls_list = list(balls_state)
n = len(balls_list)
for i in range(n - 2):
# Check if balls[i:i+3] are the same and form a group of exactly 3 (not part of a larger group)
if balls_list[i] == balls_list[i+1] == balls_list[i+2]:
left_same = i > 0 and balls_list[i-1] == balls_list[i]
right_same = i+3 < n and balls_list[i+3] == balls_list[i]
if not left_same and not right_same:
# We can perform a manual crack here
# Remove these three balls
new_balls = balls_list[:i] + balls_list[i+3:]
# Now simulate chain reactions
new_balls = remove_chain_reactions(new_balls)
new_balls_state = tuple(new_balls)
if new_balls_state not in visited:
heapq.heappush(heap, (len(new_balls), cracks_used + 1, new_balls_state))
# If we reach here, return the minimal balls remaining and corresponding cracks used
return (min_cracks_used, min_balls_remaining)
This function crack will return a tuple containing the minimum number of cracks used and the minimum number of balls remaining after optimally cracking the balls according to the game's rules.