Problem solution · Turing

CCC 2006 J5 - CCC Othello

CCC 2006 J5 - CCC Othello: 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
269 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 2006 J5 - CCC Othello, 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

  • 269 lines of Turing from the credited upstream file ccc06j5.t.
  • The implementation keeps its working state in language-native values and containers.
  • 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 2006 J5 - CCC Othello · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 2006 Junior problem 5% CCC Othello%% the main othello board is the 2d array b% 0 = empty% 1 = white% 2 = black var b : array 1 .. 8, 1 .. 8 of intvar config : stringvar n, row, col : intvar player : string := "b"var countb, countw : intvar f : string (1) %for testing onlyprocedure print    for r : 1 .. 8        for c : 1 .. 8            if b (r, c) = 0 then                put " " ..            elsif b (r, c) = 1 then                put "w" ..            else                put "b" ..            end if        end for        put ""    end forend print %starting at r,c look in all directions to reverse markers% first determine if a marker of the same color exists% in a direction, if it does, flip the others in betweenprocedure reverse (r, c : int)    var i, j, k : int    var found : boolean    if b (r, c) = 1 then        k := 2    else        k := 1    end if     %up    i := r - 1    found := false    loop        exit when i = 0 or found or b (i, c) = 0        if b (i, c) = b (r, c) then            found := true        end if        i := i - 1    end loop    if found then        i := r - 1        loop            exit when b (i, c) = b (r, c)            b (i, c) := b (r, c)            i := i - 1        end loop    end if     %down    i := r + 1    found := false    loop        exit when i = 9 or found or b (i, c) = 0        if b (i, c) = b (r, c) then            found := true        end if        i := i + 1    end loop    if found then        i := r + 1        loop            exit when b (i, c) = b (r, c)            b (i, c) := b (r, c)            i := i + 1        end loop    end if     %left    i := c - 1    found := false    loop        exit when i = 0 or found or b (r, i) = 0        if b (r, i) = b (r, c) then            found := true        end if        i := i - 1    end loop    if found then        i := c - 1        loop            exit when b (r, i) = b (r, c)            b (r, i) := b (r, c)            i := i - 1        end loop    end if     %right    i := c + 1    found := false    loop        exit when i = 9 or found or b (r, i) = 0        if b (r, i) = b (r, c) then            found := true        end if        i := i + 1    end loop    if found then        i := c + 1        loop            exit when b (r, i) = b (r, c)            b (r, i) := b (r, c)            i := i + 1        end loop    end if     %up left    i := r - 1    j := c - 1    found := false    loop        exit when i = 0 or j = 0 or found or b (i, j) = 0        if b (i, j) = b (r, c) then            found := true        end if        i := i - 1        j := j - 1    end loop    if found then        i := r - 1        j := c - 1        loop            exit when b (i, j) = b (r, c)            b (i, j) := b (r, c)            i := i - 1            j := j - 1        end loop    end if     %up right    i := r - 1    j := c + 1    found := false    loop        exit when i = 0 or j = 9 or found or b (i, j) = 0        if b (i, j) = b (r, c) then            found := true        end if        i := i - 1        j := j + 1    end loop    if found then        i := r - 1        j := c + 1        loop            exit when b (i, j) = b (r, c)            b (i, j) := b (r, c)            i := i - 1            j := j + 1        end loop    end if     %down left    i := r + 1    j := c - 1    found := false    loop        exit when i = 9 or j = 0 or found or b (i, j) = 0        if b (i, j) = b (r, c) then            found := true        end if        i := i + 1        j := j - 1    end loop    if found then        i := r + 1        j := c - 1        loop            exit when b (i, j) = b (r, c)            b (i, j) := b (r, c)            i := i + 1            j := j - 1        end loop    end if     %down right    i := r + 1    j := c + 1    found := false    loop        exit when i = 9 or j = 9 or found or b (i, j) = 0        if b (i, j) = b (r, c) then            found := true        end if        i := i + 1        j := j + 1    end loop    if found then        i := r + 1        j := c + 1        loop            exit when b (i, j) = b (r, c)            b (i, j) := b (r, c)            i := i + 1            j := j + 1        end loop    end ifend reverse % get setupfor r : 1 .. 8    for c : 1 .. 8        b (r, c) := 0    end forend forget configif config = "a" then    b (4, 4) := 1    b (5, 5) := 1    b (4, 5) := 2    b (5, 4) := 2elsif config = "b" then    for r : 1 .. 8        b (r, r) := 2        b (r, 9 - r) := 1    end forelsif config = "c" then    for r : 1 .. 8        b (r, 3) := 2        b (r, 4) := 2        b (r, 5) := 1        b (r, 6) := 1    end forend if %play gameget nfor i : 1 .. n    get row, col % assuming valid moves    if player = "b" then        b (row, col) := 2        player := "w"    else        b (row, col) := 1        player := "b"    end if    reverse (row, col)end for %do countingcountb := 0countw := 0for r : 1 .. 8    for c : 1 .. 8        if b (r, c) = 1 then            countw := countw + 1        elsif b (r, c) = 2 then            countb := countb + 1        end if    end forend forput countb, " ", countw    

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 ↗