Problem solution · Python

CCC 2019 S3 - Arithmetic Square

CCC 2019 S3 - Arithmetic Square: 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
81 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 2019 S3 - Arithmetic Square, 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

  • 81 lines of Python from the credited upstream file ccc19s3.py.
  • The implementation keeps its working state in language-native values and containers.
  • No explicit loop blocks detected, together with recursive traversal.

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 2019 S3 - Arithmetic Square · PythonPython
Use this to learn the idea, then write your own version.
# By Daniel Zhang, Pinetree Secondary import sys, randomfrom copy import deepcopy grid = [input().split() for i in range(3)]for i in range(3):    for j in range(3):        if grid[i][j] != 'X':            grid[i][j] = int(grid[i][j]) def deduce(a, b, c):    if a != 'X' and b != 'X' and c != 'X':        return (None, None, None)    elif a != 'X' and b != 'X':        return a, b, b + b-a    elif a != 'X' and c != 'X':        return a, (a+c)//2, c    elif b != 'X' and c != 'X':        return b - (c-b), b, c    else:        return (None, None, None) def full(grid):    for r in grid:        if 'X' in r:            return False    return True def valid(grid):    def ok(a, b, c):        return c - b == b - a     # Check rows    for i in range(3):        if not ok(grid[i][0], grid[i][1], grid[i][2]):            return False     # Check columns    for j in range(3):        if not ok(grid[0][j], grid[1][j], grid[2][j]):            return False     return True def fill(grid):    cont = True    while cont:        cont = False        for i in range(3):            a, b, c = deduce(grid[i][0], grid[i][1], grid[i][2])            if a is not None:                grid[i][0], grid[i][1], grid[i][2] = a, b, c                cont = True                        a, b, c = deduce(grid[0][i], grid[1][i], grid[2][i])            if a is not None:                grid[0][i], grid[1][i], grid[2][i] = a, b, c                cont = True     return grid  def backtrack(grid):    fill(grid)     if full(grid) and valid(grid):        for r in grid:            print(*r)        sys.exit()        for i in range(3):        for j in range(3):            if grid[i][j] == 'X':                newgrid = deepcopy(grid)                newgrid[i][j] = 1                backtrack(newgrid)     while True:    backtrack(grid)

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 ↗