CF37E.Trial for Chief

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Having unraveled the Berland Dictionary, the scientists managed to read the notes of the chroniclers of that time. For example, they learned how the chief of the ancient Berland tribe was chosen.

As soon as enough pretenders was picked, the following test took place among them: the chief of the tribe took a slab divided by horizontal and vertical stripes into identical squares (the slab consisted of N lines and M columns) and painted every square black or white. Then every pretender was given a slab of the same size but painted entirely white. Within a day a pretender could paint any side-linked set of the squares of the slab some color. The set is called linked if for any two squares belonging to the set there is a path belonging the set on which any two neighboring squares share a side. The aim of each pretender is to paint his slab in the exactly the same way as the chief’s slab is painted. The one who paints a slab like that first becomes the new chief.

Scientists found the slab painted by the ancient Berland tribe chief. Help them to determine the minimal amount of days needed to find a new chief if he had to paint his slab in the given way.

在破译了贝尔兰词典后,科学家们成功解读了那个时代编年史家的笔记。例如,他们了解了古代贝尔兰部落首领的选拔方式。

一旦选出了足够数量的候选人,便会在他们之间举行如下测试:部落首领取来一块被水平与垂直条纹划分为若干相同方格的石板(该石板由 NN 行和 MM 列组成),并将每个方格涂成黑色或白色。随后,每位候选人会获得一块尺寸完全相同的石板,但初始时整块石板均为白色。每位候选人每天可将石板上任意一个边连通的方格集合涂成某种颜色(黑或白)。所谓“连通”,是指该集合中任意两个方格之间均存在一条路径,路径上的每一对相邻方格均共享一条边。每位候选人的目标是将其石板涂成与首领石板完全一致的图案。最先完成者即成为新任首领。

科学家们已找到了古代贝尔兰部落首领所绘制的石板图案。请帮助他们确定:若某位候选人需将石板涂成给定图案,则其所需的最少天数是多少?

输入格式

The first line contains two integers N and M (1 ≤ N, M ≤ 50) — the number of lines and columns on the slab. The next N lines contain M symbols each — the final coloration of the slab. W stands for the square that should be painted white and B — for the square that should be painted black.

第一行包含两个整数 NN 和 MM(1≤N,M≤501 \leq N, M \leq 50)—— 分别表示石板的行数和列数。接下来的 NN 行,每行包含 MM 个字符,表示石板的最终着色结果。字符 W 表示该方格应被涂成白色,字符 B 表示该方格应被涂成黑色。

输出格式

In the single line output the minimal number of repaintings of side-linked areas needed to get the required coloration of the slab.

在单行输出中,输出为使石板达到所需着色而需重涂的侧向相连区域的最少次数。

输入输出样例

  • 输入#1

    3 3
    WBW
    BWB
    WBW

    输出#1

    2
  • 输入#2

    2 3
    BBB
    BWB

    输出#2

    1

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

首页