140 likes | 204 Views
HKOI2006 Analysis and Solution Senior Q2 – Toggle. HKOI Training Team 2006-01-07. Statistics. Attempts: 46 (out of 76) Mean: 35.98 Max: 100 (1) Min: 0 Std Dev: 23.66. Statistics. The Problem.
E N D
HKOI2006 Analysis and Solution Senior Q2 – Toggle HKOI Training Team 2006-01-07
Statistics • Attempts: 46 (out of 76) • Mean: 35.98 • Max: 100 (1) • Min: 0 • Std Dev: 23.66
The Problem • Given an initial board configuration, maximize the number of white cells through toggling column and/or rows • 1 ≤ N ≤ 16 • Partial score will be given if number of white cells ≥ Optimal solution
Observations (1) • A row / column will be toggled only once • There is no point to toggle twice or more • Why?
Observations (2) • Order of toggle moves does not matter • We only need to determine which rows/columns to toggle
Observations (3) • On a N x N board, if a row has K white sides, it will have (N-K) white cells after the row is toggled • Max(N,N-K) ≥ N/2
Algorithms (1) • Try all combinations of toggling moves • There are 2(2N-1) combinations • Good if N is small (e.g. ≤ 8)
Algorithms (2) • Do not toggle any columns • For each row, determine whether to toggle or not, which ever gives a better result • Guarantees ≥ 50% white cells (From Observation 3), • but cannot guarantee optimal solution
Algorithm (3) • Pick the row/column with most number of black cells, and toggle • Repeat the above step until toggling gives no improvement • Greedy Approach • Also guarantees ≥ 50% white cells (Why?) • but cannot guarantee optimal solution as well…
The “Official” Solution • Algorithm 2 toggles rows only, what happened to the columns? • The algorithm produce the best possible result for rows-only moves • Suppose we toggle a column, then re-run Algorithm 2, what will we get?
The “Official” Solution • Try all possible combinations of toggling columns and run Algorithm 2 for each combination
Common Mistakes • Outputting invalid commands • e.g. @