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.
- 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
- 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.
Use this to learn the idea, then write your own version.
12345678910 11 121314def taller(a, N, p, q):15 16 v = []17 for r in range(N):18 v.append(False) 19 queue = []20 for x in a[p]:21 queue.append (x)22 while len(queue) > 0:23 h = queue.pop(0)24 if h == q:25 return True26 if not v[h]:27 v[h] = True28 for x in a[h]:29 queue.append(x)30 return False31 32 33file = open("s4.6-5.in", 'r')34x = file.readline().strip().split()35N = int(x[0])36M = int(x[1])37 3839a = []40for r in range(N):41 row = []42 a.append(row)43 44 45for i in range(M):46 x = file.readline().strip().split()47 p = int(x[0]) - 148 q = int(x[1]) - 149 a[p].append(q)50 51x = file.readline().strip().split()52p = int(x[0]) - 153q = int(x[1]) - 154 55if taller(a, N, p, q):56 print "yes"57elif taller(a, N, q, p):58 print "no"59else:60 print "unknown"61