Editorial for COCI '12 Contest 3 #1 Sahovnica


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

There are multiple different approaches to this problem: we can use a matrix of characters, but we don't need to.

First method

Using two for loops, where one iterates over the total number of output rows, and the other (nested) over output columns, we output the appropriate characters. Here, we need a function to determine, given the current row and column (r, c), whether the character is red or white.

Notice that, if we number the rows and columns from 0 (in which case relations 0 \le r < R \times A and 0 \le c < C \times B hold), the transformation (r, c) \to (r \mathbin{div} A, c \mathbin{div} B) results in the row and column of the corresponding chessboard cell (where 0 \le r \mathbin{div} A < R, 0 \le c \mathbin{div} B < C). A simple observation leads to the conclusion that the cell is red if the sum of the row and column (r \mathbin{div} A + c \mathbin{div} B) is even, and white otherwise.

Second method

We use a character matrix in which we draw chessboard cells one by one. We choose the current cell using two nested for loops and draw it, again using two nested for loops. Here we need to compute the starting coordinates of the current chessboard cell: they are (1 + r \times A, 1 + c \times B), where r and c (0 \le r < R, 0 \le c < C) are the row and column of the cell, and the character matrix is indexed from (1, 1).


Comments

There are no comments at the moment.