Editorial for CCC '24 J5 - Harvest Waterloo


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

For the final problem in this year's competition, the fundamental task is to determine all the locations that the farmer can reach. By "visiting" each of these locations and summing the values of the pumpkins at these locations, we can produce the total value harvested by the farmer. This requires us to store the pumpkin patch in memory which must ultimately be done with a two-dimensional structure (e.g. a list of strings or two-dimensional array).

For the first subtask, the farmer can reach every location. Visiting every location can be done with a nested loop.

For the second subtask, we can begin at the farmer's starting location and move in one direction until blocked by hay or the edge of the patch. Since we know that the locations the farmer can reach form a rectangle, doing this up, down, left and right, allows us to determine the dimensions of the rectangle and the location of a corner of the rectangle. This allows us to use a nested loop as in the first subtask to visit all the locations that the farmer can reach. A different strategy is required for the final two subtasks. We can maintain a collection of locations to visit. Initially this collection will consist only of the starting location of the farmer. Then we can repeatedly remove a location from the collection and if it has not yet been accounted for, add the value of the pumpkin at this location to a running total. Also, since the farmer can only move in four possible directions, every location we visit presents up to four potential new locations to add to the collection. Locations should not be added if they are not reachable (contain hay or do not exist because we are at the edge of the patch) or if they have already been accounted for. One clever way to record that a location has been accounted for is to change the S, M or L at that location to a *. This general technique is an example of what is called a flood fill algorithm.

Notice that the collection of locations to visit can theoretically grow very large and contain many duplicates. This is because, for example, visiting the location at row 4 and column 7 could result in us adding the location at row 3 and column 7 to the collection, but this same location might be added when visiting row 3 and column 8 (or row 2 and column 7, or row 3 and column 6). Avoiding this is needed to earn 15 marks.

Depending on how the collection is implemented, the full solution described here is called breadth-first search (BFS) or depth-first search (DFS). DFS can also be implemented using a recursive function or method which is a function or method that calls itself. For recursive functions, the collection is implicit and stored in a portion of memory usually referred to as the call stack. Different languages and machines can put different limits on how large the call stack can be. Python tends to be especially restrictive and so using recursion and Python for this problem likely requires you to instruct the system to make enough room by importing the sys module and issuing a command such as sys.setrecursionlimit(100000).


Comments

There are no comments at the moment.