Editorial for Baltic OI '10 P6 - Mines
Submitting an official solution before solving the problem yourself is a bannable offence.
Consider the grid with rows and
columns. Since we don't know how to solve cases when
efficiently, there were no such test cases given. Let's look at the solution for other cases. We may assume that
. If
then
and we rotate the grid by
.

Let's number the rows from bottom to top and the columns from left to right. The cell with row number and column number
is denoted by
. By
we denote the table given in input and by
– the mine field we are looking for. We say
if there is a mine in cell
and
otherwise. We denote by
the total number of mines in cell
and its adjacent cells. Then, for instance,
,
and so on.
Let's first solve two subtasks:
- Find the third row of the grid;
- Solve the task for the grid of size
.
Finding the third row of the grid
Note that:
- If for some
we know the value of
, we can calculate
:
.
Therefore we can calculate values of ,
,
and so on. Similarly we can calculate the values from the other end:
,
,
and so on. Now one of these two conditions holds:
. In this case either
or
will be known. It is easy to see that if the values of two out of three consecutive cells
,
,
are known, we can easily calculate the value of the unknown cell because the sum of all three cells equals
. Therefore we can find all three values
,
and
and then, moving from left to right, calculate values for the entire third row.
. In this case we will only know the values of
. Let's consider the values of some three consecutive cells
,
and
. Exactly one of these three values is known and, as we established before, the sum of all three values is known as well. So the sum of two other values is known and we will denote it by
. For example, in the grid below, the sum of values can be calculated for the following pairs of cells:
. What can this sum be? If
then it is clear that both values are
. Similarly, if
then both values are
. In these cases we know the values of three consecutive cells and we can then easily find the entire third row (as described in case 1). If, however, the sum of two cell values equals
, it is unclear which of the values is
and which is
. So, if for some three consecutive cells
or
, we can find the entire third row. The only "bad" case is when for every three consecutive cells
. In fact, this is not really a bad case. Observe that any
value which in its sum includes some unknown cell from the third row, includes exactly two unknown cells from the third row, and the sum of these two cells is known (and is equal to
). It means that we don't care about the exact locations of the mines on the third row in these unknown cells and, if we consider them consecutively, there are two valid sequences: either
or
. We can choose any of these two sequences (the solution is not unique in this case).
The grid of size 
Positions of the mines are determined exactly the same way we found the third row. We will use notations meaning
and
meaning
:
- First we find
.
- Then we find
,
,
and so on.
- From the other end we find
,
,
and so on.
- We find the entire row (in a way described before).
Finally, the solution of the original task (complexity is
)
The solution consists of these steps:
- First we find mine locations in the third row of the grid. Then in the same way we can find the sixth row, then the ninth row and so on.
- In the opposite direction (from top to bottom) we find the rows
,
,
and so on.
- Assuming that
, the unknown rows will be either
(when
) or
(when
).
- Since the unknown rows are apart from each other at a distance of
, for each of them we can find mine locations independently, solving the
subtask.
Comments