Codeforces Round #607 (Div. 1)-B. Beingawesomeism

Codeforces Round #607 (Div. 1)-B. Beingawesomeism

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.

Input
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.

Output
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.

Example
inputCopy
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
outputCopy
2
1
MORTAL
4
Note
In the first test case, it can be done in two usages, as follows:

Usage 1:
在这里插入图片描述
在这里插入图片描述

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”.

***思路:***这题相当简单,你只要分清楚什么情况操作几次就好了。全A的情况不用操作,在边上有一行A只要操作一次,在角落有A或者在中间有一行A只要操作2次,在边上有A但不是在角落只要操作3次,其他情况要操作四次,就是先把他变成一条直线在平铺把图铺满。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char a[100][100];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,m;
		scanf("%d%d",&n,&m);
		int flag=0;
		memset(a,0,sizeof(a));
		int sum=0;
		for(int i=1;i<=n;i++)
		{
			int cnt=0;
			getchar();
			for(int j=1;j<=m;j++)
			{
				scanf("%c",&a[i][j]);
				if(a[i][j]=='A'){
					if(flag<1)
					flag=1;
					cnt++;
				}
			}
			sum+=cnt;
			if((i==1||i==n)&&cnt==m&&flag<4)flag=4;
			else if(cnt==m&&flag<3)flag=3;
			else if(((i==1||i==n)&&cnt!=0)&&flag<2)flag=2;
		}
		if(sum==n*m)flag=5;
		if(flag!=0)
		{
			if(flag<3&&(a[1][1]=='A'||a[1][m]=='A'||a[n][1]=='A'||a[n][m]=='A'))
			{
				flag=3;
			}
			for(int i=1;i<=m;i++){
				int cnt=0;
				for(int j=1;j<=n;j++)
				{
					if(a[j][i]=='A')cnt++;
				}
				if((i==1||i==m)&&cnt==n&&flag<4)flag=4;
				else if(cnt==n&&flag<3)flag=3;
				else if(((i==1||i==m)&&cnt!=0)&&flag<2)flag=2;
			}			
		}
		if(flag==0)
		{
			printf("MORTAL\n");
		}else if(flag==1)
		{
			printf("4\n");
		}else if(flag==2){
			printf("3\n");
		}else if(flag==3)
		{
			printf("2\n");
		}else if(flag==4)
		{
			printf("1\n");
		}else if(flag==5){
			printf("0\n");
		}
	}
	return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值