CF1280B.Beingawesomeism
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are an all-powerful being and you have created a rectangular world. In fact, your world is so bland that it could be represented by a r×c grid. Each cell on the grid represents a country. Each country has a dominant religion. There are only two religions in your world. One of the religions is called Beingawesomeism, who do good for the sake of being good. The other religion is called Pushingittoofarism, who do murders for the sake of being bad.
Oh, and you are actually not really all-powerful. You just have one power, which you can use infinitely many times! Your power involves missionary groups. When a missionary group of a certain country, say a, passes by another country b, they change the dominant religion of country b to the dominant religion of country a.
In particular, a single use of your power is this:
- You choose a horizontal 1×x subgrid or a vertical x×1 subgrid. That value of x is up to you;
- You choose a direction d. If you chose a horizontal subgrid, your choices will either be NORTH or SOUTH. If you choose a vertical subgrid, your choices will either be EAST or WEST;
- You choose the number s of steps;
- You command each country in the subgrid to send a missionary group that will travel s steps towards direction d. In each step, they will visit (and in effect convert the dominant religion of) all s countries they pass through, as detailed above.
- The parameters x, d, s must be chosen in such a way that any of the missionary groups won't leave the grid.
The following image illustrates one possible single usage of your power. Here, A represents a country with dominant religion Beingawesomeism and P represents a country with dominant religion Pushingittoofarism. Here, we've chosen a 1×4 subgrid, the direction NORTH, and s=2 steps.

You are a being which believes in free will, for the most part. However, you just really want to stop receiving murders that are attributed to your name. Hence, you decide to use your powers and try to make Beingawesomeism the dominant religion in every country.
What is the minimum number of usages of your power needed to convert everyone to Beingawesomeism?
With god, nothing is impossible. But maybe you're not god? If it is impossible to make Beingawesomeism the dominant religion in all countries, you must also admit your mortality and say so.
你是一位全能的存在,创造了一个矩形世界。事实上,你的世界如此单调,以至于可以用一个 r×c 的网格来表示。网格中的每个格子代表一个国家,每个国家都有一种主导宗教。在你的世界中仅有两种宗教:一种名为“卓越主义(Beingawesomeism)”,其信徒行善只为行善本身;另一种名为“过火主义(Pushingittoofarism)”,其信徒作恶只为作恶本身。
哦,实际上你并非真正全能——你只拥有一种能力,但可无限次使用!该能力涉及传教团。当某国 a 的传教团经过另一国 b 时,他们会将 b 国的主导宗教更改为 a 国的主导宗教。
具体而言,单次使用该能力的操作如下:
- 你选择一个水平方向的 1×x 子网格,或一个垂直方向的 x×1 子网格;其中 x 的值由你决定;
- 你选择一个方向 d:若选择的是水平子网格,则方向 d 只能是 NORTH(北)或 SOUTH(南);若选择的是垂直子网格,则方向 d 只能是 EAST(东)或 WEST(西);
- 你选择步数 s;
- 你命令该子网格中的每个国家,各自派出一个传教团,沿方向 d 行进 s 步。每一步中,他们将途经(并实际转化其主导宗教)沿途所经过的所有 s 个国家,如上所述;
- 参数 x、d、s 必须满足:任何传教团均不会走出网格边界。
下图展示了一次能力使用的可能情形。其中 A 表示主导宗教为“卓越主义”的国家,P 表示主导宗教为“过火主义”的国家。本例中,我们选择了一个 1×4 的子网格、方向 NORTH(北)、步数 s=2。

你本质上信奉自由意志——至少在大多数情况下如此。然而,你实在不想再因自己的名义而收到那些归咎于你的谋杀事件了。因此,你决定动用能力,尝试使“卓越主义”成为所有国家的主导宗教。
将所有国家全部转化为“卓越主义”所需的最少能力使用次数是多少?
有神则无所不能。但也许……你并非神明?若不可能使所有国家的主导宗教均变为“卓越主义”,你也必须坦然承认自己的凡人之躯,并如实说明。
输入格式
The first line of input contains a single integer t (1≤t≤2⋅104) denoting the number of test cases.
The first line of each test case contains two space-separated integers r and c denoting the dimensions of the grid (1≤r,c≤60). The next r lines each contains c characters describing the dominant religions in the countries. In particular, the j-th character in the i-th line describes the dominant religion in the country at the cell with row i and column j, where:
- "A" means that the dominant religion is Beingawesomeism;
- "P" means that the dominant religion is Pushingittoofarism.
It is guaranteed that the grid will only contain "A" or "P" characters. It is guaranteed that the sum of the r⋅c in a single file is at most 3⋅106.
输入的第一行包含一个整数 t(1≤t≤2⋅104),表示测试用例的数量。
每个测试用例的第一行包含两个以空格分隔的整数 r 和 c,表示网格的尺寸(1≤r,c≤60)。接下来的 r 行,每行包含 c 个字符,描述各国的主导宗教。具体而言,第 i 行的第 j 个字符表示位于第 i 行、第 j 列的国家的主导宗教,其中:
- “A” 表示该国主导宗教为 Beingawesomeism;
- “P” 表示该国主导宗教为 Pushingittoofarism。
保证网格中仅包含字符 “A” 或 “P”。还保证单个输入文件中所有测试用例的 r⋅c 之和不超过 3⋅106。
输出格式
For each test case, output a single line containing the minimum number of usages of your power needed to convert everyone to Beingawesomeism, or the string "MORTAL" (without quotes) if it is impossible to do so.
对于每个测试用例,输出一行,包含将所有人转化为“Beingawesomeism”所需的最少使用能力次数;如果无法实现,则输出字符串 "MORTAL"(不带引号)。
输入输出样例
输入#1
4 7 8 AAPAAAAA PPPPAAAA PPPPAAAA APAAPPPP APAPPAPP AAAAPPAP AAAAPPAA 6 5 AAAAA AAAAA AAPAA AAPAP AAAPP AAAPP 4 4 PPPP PPPP PPPP PPPP 3 4 PPPP PAAP PPPP
输出#1
2 1 MORTAL 4
说明/提示
In the first test case, it can be done in two usages, as follows:
Usage 1:

Usage 2:

In the second test case, it can be done with just one usage of the power.
In the third test case, it is impossible to convert everyone to Beingawesomeism, so the answer is "MORTAL".
在第一个测试用例中,可以通过两次使用能力完成,如下所示:
使用方式 1:

使用方式 2:

在第二个测试用例中,仅需一次使用该能力即可完成。
在第三个测试用例中,无法将所有人转化为“Beingawesomeism”,因此答案为 “MORTAL”。
输入解题思路,AI测评打分。不知道怎么写?