Problem solution · Python

CCC 2012 S5 - Mouse Journey

CCC 2012 S5 - Mouse Journey: a Python solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Sliding window or two pointers
Source
CCCSolutions
Length
69 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

Sliding window or two pointers

For CCC 2012 S5 - Mouse Journey, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 69 lines of Python from the credited upstream file ccc12s5.py.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

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 2012 S5 - Mouse Journey · PythonPython
Use this to learn the idea, then write your own version.
# S5 : Mouse Journey## a relatively straight forward 2D array problem## the idea is that you maintain the total number of moves# from the top left corner, to ALL positions in the array# then the bottom right element contains the answer## if you had;# 1 1 1 # 1 2 3# C 2 x## then the value at x is 2 + 3# because if you gat get to the cage to the left in 2 paths# and to the cage above in 3 paths, then to this cage,# there are 5 paths.## Kevin Luo, Eric Hamber Secondary School (Vancouver)# suggested several improvements (simlifications) to my original.# These included# 1. adding an extra row of 0's, top and left#    so no special processing of the first row and column of the#    lab is required.# 2. Add an extra 2D array, fixed which basically says,#    "don't change this position" (used for the original 1,1#    and the cats) then processing is a joke:#    just add the guy above and to the left.  file = open("s5.7.in", 'r') # create the lab (the 2d aray of max paths to get to this cage)x = file.readline().split()r = eval(x[0]) c = eval(x[1])lab = []fixed = []for i in range(r+1):    row = []    frow = []    for j in range (c+1):        row.append(0)        frow.append(False)    lab.append(row)    fixed.append(frow) # fill the lab with catsk = eval(file.readline())for i in range(k):    x = file.readline().split()    catr = eval(x[0])    catc = eval(x[1])    lab[catr][catc] = 0    fixed[catr][catc] = True # get startedlab[1][1] = 1fixed[1][1] = True # process the labfor i in range(1,r+1):    for j in range (1,c+1):        if not fixed[i][j]:            lab[i][j] = lab[i-1][j] + lab[i][j-1] print lab[r][c]  

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 ↗