Problem solution · Python

CCC 2000 S3 - Surfing

CCC 2000 S3 - Surfing: 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
80 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 2000 S3 - Surfing, 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

  • 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.

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 2000 S3 - Surfing · PythonPython
Use this to learn the idea, then write your own version.
# CCC 2000 - J5 Surfin'## This algoritm by Ahmed Sabie of Glenforest Secondary School## This approach involves assigning each website a numerical node# str.find used to get all the links## Using the sample data given in the problem:## after the initial read loop to determine who is linked to whom;## nodeassign = {'http://ccc.uwaterloo.ca': 0,#               'http://abc.def/ghi': 1,#               'http://xxx': 3,#               'http://www.www.www.com': 2}# Graph =      [[False, True, False, False],#               [False, False, True, True],#               [False, False, False, False],#               [False, False, False, False]]## Standard BFS is used for checking for a possible surf path.# flag is used to hold the answer.## after the first query (uwaterloo to www?)#    flag = [True, True, True, True]# and after the second query (www to uwaterloo?)#    flag = [False, False, True, False] file = open ("surf.in2", "r") nodeassign = {}node = 0 graph = [[False for i in xrange(100)]for i in xrange(100)] n = int(file.readline()) for i in range(n):    webpage = file.readline().strip()     HTMLcode = ""    HTML = ""    while HTML != "</HTML>":        HTML = file.readline().strip()        HTMLcode += HTML            while HTMLcode.find("A HREF=") != -1:         HTMLlink = HTMLcode.find("A HREF=")         start = HTMLcode.find('"', HTMLlink)         end = HTMLcode.find('"', start + 1)        link = HTMLcode[start + 1: end]        print "Link from", webpage, "to", link        if webpage not in nodeassign:            nodeassign[webpage] = node            node += 1         if link not in nodeassign:            nodeassign[link] = node            node += 1        graph[nodeassign[webpage]][nodeassign[link]] = True        HTMLcode = HTMLcode[end + 1:]  print here = file.readline().strip() while here != "The End":    there = file.readline().strip()     flag = [False for i in xrange(100)]    queue = [nodeassign[here]]    flag[nodeassign[here]] = True     end = nodeassign[there]    while queue:        u = queue.pop(0)        for i in xrange(100):             if graph[u][i] and not flag[i]:                 flag[i] = True                queue.append(i)    if flag[end]:        print "Can surf from", here, "to", there     else:        print "Can't surf from", here, "to", there                    here = file.readline().strip()  

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 ↗