Approach
Breadth-first search
For CCC 2024 J5 - Harvest Waterloo, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 60 lines of Python from the credited upstream file ccc24j5.py.
- The implementation visibly relies on sequence storage, work queue.
- No explicit loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3from collections import deque4 5r = int(input())6c = int(input())7 8 9def valid(node):10 x, y = node11 if 0 <= x < r:12 if 0 <= y < c:13 if not vis[x][y]:14 if patch[x][y] != "*":15 return True16 return False17 18def neighbours(node):19 x, y = node20 return [[x+1, y], [x-1, y], [x, y+1], [x, y-1]]21 22patch = []23vis = []24 25for i in range(r):26 patch.append(list(input()))27 vis.append([False] * c)28 29a = int(input())30b = int(input())31 32queue = deque([[a, b]])33score = 034vis[a][b] = True35cur = patch[a][b]36if cur == "S":37 score += 138if cur == "M":39 score += 540if cur == "L":41 score += 1042 43while queue:44 node = queue.popleft()45 46 for n in neighbours(node):47 if valid(n):48 a, b = n[0], n[1]49 vis[a][b] = True50 cur = patch[a][b]51 if cur == "S":52 score += 153 if cur == "M":54 score += 555 if cur == "L":56 score += 1057 58 queue.append(n)59 60print(score)