Problem solution · Python

CCC 2011 S3 - Alice Through the Looking Glass

CCC 2011 S3 - Alice Through the Looking Glass: a Python solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Depth-first search
Source
CCCSolutions
Length
62 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

Depth-first search

For CCC 2011 S3 - Alice Through the Looking Glass, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 62 lines of Python from the credited upstream file ccc11s3.py.
  • The implementation keeps its working state in language-native values and containers.
  • No explicit loop blocks detected.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

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 2011 S3 - Alice Through the Looking Glass · PythonPython
Use this to learn the idea, then write your own version.
# CCC 2011 Senior 3: Alice through the Looking Glass# written by C. Robart# March 2011 # Recursion. It's simple, once done, but# it took a LONG while to think of it and debug :-)## Consider the original configuration of blocks#   _____#   __X__#   _XXX_## As the grid expands, with increased m, I think of the blocks# getting ever smaller, with the same shape of 4 blocks placed on the# top of the existing blocks.# So imagine that the above picture is of a 125x125 grid (m=3)# each of those "blocks" is 25x25, the bottom left one starting at# x = 25 and running to x = 49. Let's consider x = 35.# If you divide x by 5^m-1 or 35 by 25, you get 1. More or less# right back at the beginning. There is a block over x=1, two over x=2# and a block over x = 3, nothing over x = 0 or 4.# HOWEVER, and this is the point, we are in the world of m=3, so our "block"# is really 25 high. Hence there are 1*power (25) blocks above x = 35 when m=3,# PLUS all the blocks above that!# The recursive call piles up the smaller blocks,# with m-1 (=2) and the x in this new world is 10. (35 % 25 = 10).# Draw a picture and you'll see it. :-)## Back in the main pgm, if y is < the number of blocks above x,# then x,y is IN the crystal! ged  def crystalSquaresatX(m,x):    if m >= 1:        power = 5 ** (m-1)        location = x // power        if location == 0 or location == 4:            return 0        elif location == 1 or location == 3:            return 1 * power + crystalSquaresatX(m - 1, x % power)        elif location == 2:            return 2 * power + crystalSquaresatX(m - 1, x % power)        return maxheightatx    return 0  # file inputfile = open("s3.1.in", 'r')T = eval(file.readline())for t in range(0,T):    line = (file.readline())    space = line.find(" ")    m = eval(line[0:space])    line = line[space+1:]    space = line.find(" ")    x = eval(line[0:space])    y = eval(line[space+1:])    if y < crystalSquaresatX(m,x):        print "crystal"    else:        print "empty" 

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 ↗