Colorful board - MarisaOJ: Marisa Online Judge

Colorful board

Time limit: 2000 ms
Memory limit: 256 MB
Alice has an $m \times n$ grid, with rows numbered from $1$ to $m$ from top to bottom and columns numbered from $1$ to $n$ from left to right. The cell at the intersection of row $i$ ($1 \leq i \leq m$) and column $j$ ($1 \leq j \leq n$) is called cell $(i, j)$. Initially, the entire grid is white (color $0$). Alice performs $k$ coloring operations as follows: - She selects a rectangle within the grid, covering a contiguous set of cells, and chooses a color $c$ ($1 \leq c \leq 30$); - She then overwrites all cells within the selected rectangle to color $c$. After performing $k$ operations, Alice obtains a colored grid. Bob also wants to recreate Alice's colored grid but does not know the operations she used. Since this is challenging, Bob aims to use no more than $h$ operations to make his grid resemble Alice's as closely as possible. **Objective:** Given Alice's colored grid and the integer $k$, help Bob find a sequence of no more than $h$ operations to make his grid as similar to Alice's as possible. ### Input - The first line contains three integers $m$, $n$, and $k$ ($k \leq 100$); - Each of the next $m$ lines contains $n$ integers $a_{i1}, a_{i2}, \ldots, a_{in}$ describing the colors in row $i$ of Alice's color grid. ### Output - The first line should contain a non-negative integer $h$ ($h \leq k$); - Each of the next $h$ lines should contain five integers $x_s, y_s, u_s, v_s, c_s$, where $(x_s, y_s)$ and $(u_s, v_s)$ are the top-left and bottom-right corners of the rectangle chosen in the $s$-th coloring operation, and $c_s$ is the color for that rectangle. ### Constraints - 30% of the tests (worth 30% of points) have $m, n, k \leq 5$; - 50% of the tests (worth 50% of points) have $m, n \leq 50$; - 20% of the tests (worth 20% of points) have $m, n \leq 500$. ### Scoring There are 20 tests, each worth 5.0 points. Let $d$ be the number of cells in the generated grid that match Alice's grid. The score for each test is calculated as $5.0 \times \left( \frac{d}{m \times n} \right)^5$. ### Example Input: ``` 3 3 2 2 2 0 2 2 1 0 1 1 ``` Output: ``` 2 1 1 2 2 2 2 2 3 3 1 ``` Alice's color grid: | | | | |---|---|---| | 2 | 2 | 0 | | 2 | 2 | 1 | | 0 | 1 | 1 | Bob's color grid: | | | | |---|---|---| | 2 | 2 | 0 | | 2 | 2 | 1 | | 0 | 1 | 1 | With this coloring strategy, a score of $5.0 \times \left( \frac{8}{9} \right)^5$ is achieved.