Problem solution · Python

CCC 2013 S4 - Who is Taller?

CCC 2013 S4 - Who is Taller?: a Python solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Breadth-first search
Source
CCCSolutions
Length
61 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For CCC 2013 S4 - Who is Taller?, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 61 lines of Python from the credited upstream file ccc13s4.py.
  • The implementation visibly relies on sequence storage.
  • 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.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2013 S4 - Who is Taller? · PythonPython
Use this to learn the idea, then write your own version.
# CCC 2013 Senior 4: Who is Taller?## relatively straight forward Breadth First Search thru a "tree".## Generally speaking The BFS approach is likely better than# a Depth First Search that a recursive approach would take. # Going deep, could lead to a stack overflow, while the BFS# approach avoids that. Also in this case the BFS is very easy # to implement.   # returns true if p is taller than q# uses a Breadth first Search thru the "a" tree of size Ndef taller(a, N, p, q):    # v is a "visited" list for track (don't go to same guy twice)    v = []    for r in range(N):        v.append(False)        queue = []    for x in a[p]:        queue.append (x)    while len(queue) > 0:        h = queue.pop(0)        if h == q:            return True        if not v[h]:            v[h] = True            for x in a[h]:                queue.append(x)    return False         file = open("s4.6-5.in", 'r')x = file.readline().strip().split()N = int(x[0])M = int(x[1]) # a is the main array holding all the linksa = []for r in range(N):    row = []    a.append(row)  for i in range(M):    x = file.readline().strip().split()    p = int(x[0]) - 1    q = int(x[1]) - 1    a[p].append(q) x = file.readline().strip().split()p = int(x[0]) - 1q = int(x[1]) - 1 if taller(a, N, p, q):    print "yes"elif taller(a, N, q, p):    print "no"else:    print "unknown" 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗