QOJ.ac

QOJ

Süre Sınırı: 1 s Bellek Sınırı: 128 MB

#23585. 网格游戏

İstatistikler

Taeyang 用放在 $M\times N$ 网格上的 $MN$ 颗黑白棋子开始游戏,如下图所示。

problem_23585_ec8131d293e484e99357a6f8c0e3b23b.png

对于任意棋子 $X$,把它上、下、左、右相邻的 $4$ 颗棋子称为 $X$ 的邻居。Taeyang 可以选择一颗棋子改变其颜色。这颗棋子原先同色的邻居也会变成新的颜色。这个过程会不断重复:发生变色的棋子的邻居中,原先同色的棋子也会变色。

例如,将上图中的棋子 $A$ 变为白色后,呈“ㄷ”字形的黑色棋子都会变为白色,结果如下图所示。

problem_23585_d0af010b6c22f49813a5605257b3100e.png

接着将棋子 $C$ 变为白色,结果如下图所示。

problem_23585_74fc41bf0baf75fc892172fac72f2399.png

然后将棋子 $F$ 变为白色,最后将棋子 $E$ 变为白色,所有棋子就都变成白色了。因此,一共进行了四次变色,使所有棋子变为白色。

不过,只需两次变色就能使所有棋子颜色相同。先将棋子 $B$ 变为黑色,结果如下图所示。

problem_23585_e01e672992b8f344cec71a0526f0d1b5.png

之后,可以将棋子 $A$ 变为白色,使所有棋子都变为白色;也可以将棋子 $D$ 变为黑色,使所有棋子都变为黑色。因此,只进行了两次变色,就使所有棋子颜色相同。

给定 $M\times N$ 网格上的黑白棋子,编写一个程序,求使所有棋子颜色相同所需的最少变色次数。

输入格式

第一行给出表示 $M\times N$ 网格的两个正整数 $M$ 和 $N$,用空格分隔。$M$ 和 $N$ 均在 $2$ 到 $100$ 之间,包含两端。从第二行开始的 $M$ 行中,每行给出 $N$ 个用空格分隔的数,表示网格中的一行:$0$ 表示白色棋子,$1$ 表示黑色棋子。

输出格式

第一行输出使所有棋子颜色相同所需的最少变色次数。

样例

输入格式 1

5 6
1 1 0 1 0 0
1 0 0 1 1 1
1 1 0 1 1 1
0 0 0 0 0 0
1 1 1 0 1 1

输出格式 1

2

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.