QOJ.ac

QOJ

时间限制: 1.0 s 内存限制: 32 MB 总分: 100

#17081. 画家

统计

Josip 是一位奇特的画家。他想画一幅由 $N \times N$ 个像素组成的画,其中 $N$ 是 $2$ 的幂($1, 2, 4, 8, 16$ 等)。每个像素要么是黑色,要么是白色。Josip 已经对每个像素应该涂什么颜色有了想法。

如果 Josip 的绘画过程不那么奇特的话,这本不是什么问题。他使用以下递归过程:

  • 如果画作只有一个像素,他就按照预期的颜色为其着色。
  • 否则,将正方形分割成四个更小的正方形,然后:
    1. 选择四个正方形中的一个,并将其涂成白色。
    2. 选择剩下的三个正方形中的一个,并将其涂成黑色。
    3. 将剩下的两个正方形视为新的画作,并对它们使用相同的步骤。

很快他注意到,用这种方法无法将他所有的设想都转化为画作。你的任务是编写一个程序,画出一幅与期望画作差异尽可能小的画。两幅画之间的差异定义为对应位置上颜色不同的像素对数。

输入格式

第一行包含一个整数 $N$($1 \le N \le 512$),表示 Josip 想要绘制的画作大小。$N$ 保证是 $2$ 的幂。

接下来的 $N$ 行,每行包含 $N$ 个数字 01,代表目标画作中的白色和黑色像素。

输出格式

第一行输出可以达到的最小差异。

接下来的 $N$ 行,输出一幅可以通过 Josip 的绘制过程得到且达到最小差异的画作。画作的格式应与输入相同。

说明

输出的第二部分(即画作)可能不唯一。任何正确的输出都将被接受。

子任务

在占总分 $50\%$ 的测试数据中,$N$ 最大为 $8$。

样例

输入 1

4
0001
0001
0011
1110

输出 1

1
0001
0001
0011
1111

输入 2

4
1111
1111
1111
1111

输出 2

6
0011
0011
0111
1101

输入 3

8
01010001
10100011
01010111
10101111
01010111
10100011
01010001
10100000

输出 3

16
00000001
00000011
00000111
00001111
11110111
11110011
11110001
11110000

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.