CF1638D.Big Brush

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You found a painting on a canvas of size n×mn \times m. The canvas can be represented as a grid with nn rows and mm columns. Each cell has some color. Cell (i,j)(i, j) has color ci,jc_{i,j}.

Near the painting you also found a brush in the shape of a 2×22 \times 2 square, so the canvas was surely painted in the following way: initially, no cell was painted. Then, the following painting operation has been performed some number of times:

  • Choose two integers ii and jj (1≤i<n1 \le i \lt n, 1≤j<m1 \le j \lt m) and some color kk (1≤k≤nm1 \le k \le nm).
  • Paint cells (i,j)(i, j), (i+1,j)(i + 1, j), (i,j+1)(i, j + 1), (i+1,j+1)(i + 1, j + 1) in color kk.

All cells must be painted at least once. A cell can be painted multiple times. In this case, its final color will be the last one.

Find any sequence of at most nmnm operations that could have led to the painting you found or state that it's impossible.

你在一块大小为 n×mn \times m 的画布上发现了一幅画。该画布可表示为一个具有 nn 行和 mm 列的网格。每个格子具有某种颜色。格子 (i,j)(i, j) 的颜色为 ci,jc_{i,j}。

在画作旁边,你还发现了一把形状为 2×22 \times 2 正方形的画笔,因此该画布必定是按如下方式绘制的:初始时,没有任何格子被涂色。随后,执行了若干次以下绘画操作:

  • 选择两个整数 ii 和 jj(满足 1≤i<n1 \le i < n,1≤j<m1 \le j < m)以及某个颜色 kk(满足 1≤k≤nm1 \le k \le nm);
  • 将格子 (i,j)(i, j)、(i+1,j)(i + 1, j)、(i,j+1)(i, j + 1)、(i+1,j+1)(i + 1, j + 1) 涂成颜色 kk。

所有格子必须至少被涂色一次。一个格子可以被多次涂色;此时,其最终颜色为最后一次涂上的颜色。

请找出任意一个长度不超过 nmnm 的操作序列,使其能生成你所发现的这幅画;若不存在这样的序列,则判定为不可能。

输入格式

The first line of input contains two integers nn and mm (2≤n,m≤10002 \le n, m \le 1000) — the dimensions of the canvas.

On the ii-th of the next nn lines of input, there will be mm integers. The jj-th of them is ai,ja_{i,j} (1≤ai,j≤nm1 \le a_{i,j} \le nm) — the color of cell (i,j)(i, j).

输入的第一行包含两个整数 nn 和 mm(2≤n,m≤10002 \le n, m \le 1000)——表示画布的尺寸。

接下来的 nn 行中,第 ii 行包含 mm 个整数。其中第 jj 个整数为 ai,ja_{i,j}(1≤ai,j≤nm1 \le a_{i,j} \le nm)——表示单元格 (i,j)(i, j) 的颜色。

输出格式

If there is no solution, print a single integer −1-1.

Otherwise, on the first line, print one integer qq (1≤q≤nm1 \le q \le nm) — the number of operations.

Next, print the operations in order. On the kk-th of the next qq lines, print three integers ii, jj, cc (1≤i<n1 \le i \lt n, 1≤j<m1 \le j \lt m, 1≤c≤nm1 \le c \le nm) — the description of the kk-th operation.

If there are multiple solutions, print any.

如果无解,请输出单个整数 −1-1。

否则,在第一行输出一个整数 qq(1≤q≤nm1 \le q \le nm)—— 表示操作次数。

接下来,按顺序输出各操作。在接下来的 qq 行中,第 kk 行输出三个整数 ii、jj、cc(1≤i<n1 \le i \lt n,1≤j<m1 \le j \lt m,1≤c≤nm1 \le c \le nm)—— 表示第 kk 个操作的描述。

若有多种解法,输出任意一种即可。

输入输出样例

  • 输入#1

    4 4
    5 5 3 3
    1 1 5 3
    2 2 5 4
    2 2 4 4

    输出#1

    6
    1 3 3
    3 3 4
    2 2 5
    1 1 5
    2 1 1
    3 1 2
  • 输入#2

    3 4
    1 1 1 1
    2 2 3 1
    2 2 1 1

    输出#2

    -1

说明/提示

In the first test case, the solution is not unique. Here's one of them:

In the second test case, there is no way one could obtain the given painting, thus the answer is −1-1.

在第一个测试用例中,解不唯一。以下是其中一个解:

在第二个测试用例中,无法通过任何方式得到给定的画作,因此答案为 −1-1。

输入解题思路,AI测评打分。不知道怎么写?

首页