Approach
Breadth-first search
For CCC 2000 S3 - Surfing, 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
- 80 lines of Python from the credited upstream file ccc00s3.py.
- The implementation keeps its working state in language-native values and containers.
- 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.
12345678910111213141516171819202122232425262728 29file = open ("surf.in2", "r")30 31nodeassign = {}32node = 0 33graph = [[False for i in xrange(100)]for i in xrange(100)] 34n = int(file.readline())35 36for i in range(n):37 webpage = file.readline().strip() 38 HTMLcode = ""39 HTML = ""40 while HTML != "</HTML>":41 HTML = file.readline().strip()42 HTMLcode += HTML43 44 while HTMLcode.find("A HREF=") != -1: 45 HTMLlink = HTMLcode.find("A HREF=") 46 start = HTMLcode.find('"', HTMLlink) 47 end = HTMLcode.find('"', start + 1)48 link = HTMLcode[start + 1: end]49 print "Link from", webpage, "to", link50 if webpage not in nodeassign:51 nodeassign[webpage] = node52 node += 1 53 if link not in nodeassign:54 nodeassign[link] = node55 node += 156 graph[nodeassign[webpage]][nodeassign[link]] = True57 HTMLcode = HTMLcode[end + 1:] 58 59print60 61here = file.readline().strip() 62while here != "The End":63 there = file.readline().strip() 64 flag = [False for i in xrange(100)]65 queue = [nodeassign[here]]66 flag[nodeassign[here]] = True 67 end = nodeassign[there]68 while queue:69 u = queue.pop(0)70 for i in xrange(100): 71 if graph[u][i] and not flag[i]: 72 flag[i] = True73 queue.append(i)74 if flag[end]:75 print "Can surf from", here, "to", there 76 else:77 print "Can't surf from", here, "to", there 78 79 here = file.readline().strip() 80