Problem solution · Turing

CCC 2001 S2 - Spirals

CCC 2001 S2 - Spirals: a Turing 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
82 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 2001 S2 - Spirals, 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

  • 82 lines of Turing from the credited upstream file ccc01s2.t.
  • The implementation visibly relies on hash lookup.
  • 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 2001 S2 - Spirals · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 2001% Problem J4S2: Spirals % Given a start and end values, print a number spiral.% Start in centre and then go down, right, up, and left, repeating to end % down and right both print the same number of numbers. Then increase by 1% to print the up and laft numbers. Increase up 1 to start down and right% again. % starting location was arbitrarily choosen out in the centre of screen,% big enough to handle 100 numbers. (5 columns to the left at 3 places each) % keybd/screen I/O var start, stop : intvar i, j : int % countersvar r, c : int % current positionvar k : int % the number of numbers to print in a direction put "Start value:"get startput "End value:"get stop  r := 12c := 20k := 0i := startloop    exit when i > stop    k := k + 1     %down    j := 1    loop        exit when j > k or i > stop        Text.Locate (r, c)        put i : 3 ..        r := r + 1        j := j + 1        i := i + 1    end loop     %right    j := 1    loop        exit when j > k or i > stop        Text.Locate (r, c)        put i : 3 ..        c := c + 3        j := j + 1        i := i + 1    end loop     k := k + 1     %up    j := 1    loop        exit when j > k or i > stop        Text.Locate (r, c)        put i : 3 ..        r := r - 1        j := j + 1        i := i + 1    end loop     %left    j := 1    loop        exit when j > k or i > stop        Text.Locate (r, c)        put i : 3 ..        c := c - 3        j := j + 1        i := i + 1    end loopend loop  

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 ↗