Problem solution · Turing

CCC 1999 P3 - Divided Fractals

CCC 1999 P3 - Divided Fractals: a Turing 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
77 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 1999 P3 - Divided Fractals, 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

  • 77 lines of Turing from the credited upstream file ccc99s3.t.
  • 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 1999 P3 - Divided Fractals · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 1999% problem C: Divided Fractals%% Print a section of the classic square fractal  % create the full fractal square (s) using straight forward recursion% but only print a section of it. % file handling is used, the number of fractals is given% n = number of iterations% b,t,l,r (bottom, top, left and right) coordinates to print   var infile : string := "frac.in"var outfile : string := "frac.out"var fi, fo : intvar count : intvar n, t, b, l, r : intvar s : array 1 .. 243, 1 .. 243 of string (1) % create the fractal square:% erase stars, given the top left corner and level of square% by erasing 3^(level-1) stars in centre (width/height is 3^level)% recursively call the 8 sub squares.procedure square (r : int, c : int, level : int)    if level >= 1 then        var n : int := 3 ** (level - 1)        for i : r + n .. r + 2 * n - 1            for j : c + n .. c + 2 * n - 1                s (i, j) := " "            end for        end for        square (r, c, level - 1)        square (r, c + n, level - 1)        square (r, c + 2 * n, level - 1)        square (r + n, c, level - 1)        square (r + n, c + 2 * n, level - 1)        square (r + 2 * n, c, level - 1)        square (r + 2 * n, c + n, level - 1)        square (r + 2 * n, c + 2 * n, level - 1)    end ifend square open : fi, infile, getopen : fo, outfile, putget : fi, countfor ii : 1 .. count    get : fi, n    get : fi, b    get : fi, t    get : fi, l    get : fi, r    var k : int := 3 ** n    for i : 1 .. k        for j : 1 .. k            s (i, j) := "*"        end for    end for    square (1, 1, n)    for decreasing i : t .. b        for j : l .. r            put : fo, s (i, j), " " ..        end for       put : fo, ""    end for    put : fo, ""end for close : ficlose : fo      

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 ↗