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×cr×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 aa, passes by another country bb, they change the dominant religion of country bb to the dominant religion of country aa.

In particular, a single use of your power is this:
在这里插入图片描述
You choose a horizontal 1×x1×x subgrid or a vertical x×1x×1 subgrid. That value of xx is up to you;
You choose a direction dd. 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 ss of steps;
You command each country in the subgrid to send a missionary group that will travel ss steps towards direction dd. In each step, they will visit (and in effect convert the dominant religion of) all ss countries they pass through, as detailed above.
The parameters xx, dd, ss 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×41×4 subgrid, the direction NORTH, and s=2s=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 tt (1≤t≤2⋅1041≤t≤2⋅104) denoting the number of test cases.

The first line of each test case contains two space-separated integers rr and cc denoting the dimensions of the grid (1≤r,c≤601≤r,c≤60). The next rr lines each contains cc characters describing the dominant religions in the countries. In particular, the jj-th character in the ii-th line describes the dominant religion in the country at the cell with row ii and column jj, 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⋅cr⋅c in a single file is at most 3⋅1063⋅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
Input
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
Output
2
1
MORTAL
4
Note
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”.
思路:答案其实就6个,0,1,2,3,4,MORTAL。
①0的时候,全是A。
②MORTAL的时候,全是P。
③1的时候,在周围一圈任意一边全是A。
④2的时候,只要在四个角上有一个是A或者有一行或一列全是A。
⑤4的时候,在周围一圈没有A。
⑥其他情况就是3了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=61;
char s[maxx][maxx];
int n,m;

inline bool check1()
{
	int flag1=1,flag2=1,flag3=1,flag4=1;
	for(int i=0;i<m;i++) if(s[0][i]=='P') flag1=0;
	for(int i=0;i<m;i++) if(s[n-1][i]=='P') flag2=0;
	for(int i=0;i<n;i++) if(s[i][0]=='P') flag3=0;
	for(int i=0;i<n;i++) if(s[i][m-1]=='P') flag4=0;
	return flag1||flag2||flag3||flag4;
}
inline bool check2()
{
	if(s[0][0]=='A'||s[0][m-1]=='A'||s[n-1][0]=='A'||s[n-1][m-1]=='A') return 1;
	for(int i=0;i<n;i++)
	{
		int flag=1;
		for(int j=0;j<m;j++) if(s[i][j]=='P') flag=0;
		if(flag) return 1;
	}
	for(int j=0;j<m;j++)
	{
		int flag=1;
		for(int i=0;i<n;i++) if(s[i][j]=='P') flag=0;
		if(flag) return 1;
	}
	return 0;
}
inline bool check4()
{
	int flag1=1,flag2=1,flag3=1,flag4=1;
	for(int i=0;i<m;i++) if(s[0][i]=='A') flag1=0;
	for(int i=0;i<m;i++) if(s[n-1][i]=='A') flag2=0;
	for(int i=0;i<n;i++) if(s[i][0]=='A') flag3=0;
	for(int i=0;i<n;i++) if(s[i][m-1]=='A') flag4=0;
	return flag1&&flag2&&flag3&&flag4;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;i++) scanf("%s",s[i]);
		int flag1=0,flag2=0;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(s[i][j]=='A') flag1=1;
				else flag2=1;
			}
			if(flag1&&flag2) break; 
		}
		if(flag2==0) cout<<0<<endl;
		else if(flag1==0) cout<<"MORTAL"<<endl;
		else
		{
			if(check1()) cout<<1<<endl;
			else if(check2()) cout<<2<<endl;
			else if(check4()) cout<<4<<endl;
			else cout<<3<<endl; 
		}
	}
	return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
前台: (1)注册登录模块:按照学校的相关规定进行注册和登录。 (2)招聘信息查看:高校毕业生们可以网站首页上查看所有的招聘信息,除此之外还可以输入公司名称或岗位名称进行搜索。 (3)用人单位模块:此模块为宣传用人单位的主要功能模块,具体包括用人单位简介、岗位需求及职责及公司介绍等功能。 (4)就业指导:学生朋友们在就业前可以通过此模块获取指导。 (5)新闻信息:为了让用户们可以了解到最新的新闻动态,本系统可以通过新闻信息查看功能阅读近期的新闻动态。 (6)在线论坛:毕业季的同学们可以通过此模块相互交流。 后台: (1)系统用户管理模块:可以查看系统内的管理员信息并进行维护。 (2)学生管理模块:通过此功能可以添加学生用户,还可以对学生信息进行修改和删除。 (3)用人单位管理模块:管理员用户通过此模块可以管理用人单位的信息,还可以对用人单位信息进行查看和维护。 (4)招聘管理模块:管理员通过此功能发布和维护系统内的照片信息。 (5)就业指导管理模块:通过此模块可以编辑和发布就业指导信息,从而更好的帮助就业季的同学们。 (6)论坛管理:通过论坛管理可以查看论坛中的主题帖及里面的回复信息,除此之外还可以对论坛中的信息进行维护和管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值