Codeforces Round #607 (Div. 1) B. Beingawesomeism(思维)

题目连接:http://codeforces.com/contest/1280/problem/B

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

题意

给出一个包含 A 和 P 的矩阵,按照题目中的规则最少需要几步将矩阵中全部变成 A (如果不行,输出 MORTAL)

分析

我们发现答案有以下几种情况:
1.如果存在 A 在矩阵其中一个角上,那么最少可能要 2 步;
2.如果存在一整行或一整列 A 在矩阵最边上,那么最少可能要 1 步;
3.如果存在一整行或一整列 A 在矩阵中间(不在边上),那么最少可能需要 2 步;
4.如果存在 A 在矩阵边上,但是不经过角,那么最少可能需要 3 步;
5.如果不存在 A ,则不可能实现;
6.如果上述都不符合,答案就为 4。

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

int t,r,c;
string a[67];
string b[67];

int main()
{
	scanf("%d",&t);
	while(t--)
	{
		int ans = 4;
		scanf("%d%d",&r,&c);getchar();
		for(int i=0;i<r;i++)
		{
			a[i].clear();
			getline(cin, a[i]);	
		}
		for(int i=0;i<c;i++) b[i].clear();
		int havep = 0;
		int havea = 0;
		for(int i=0;i<r;i++)
			for(int j=0;j<c;j++)
			{
				b[j] += a[i][j];
				if(a[i][j] == 'P') havep = 1;
				if(a[i][j] == 'A') havea = 1;
			}
		//for(int i=0;i<c;i++) cout<<b[i]<<endl;
		if(havea == 0)
		{
			printf("MORTAL\n");
			continue;
		}
		if(havep == 0)
		{
			printf("0\n");
			continue;
		}
		if(a[0][0] == 'A' || a[0][c - 1] == 'A' || a[r - 1][0] == 'A' || a[r - 1][c - 1] == 'A') ans = min(ans, 2);
		for(int i=0;i<r;i++)
			if(a[i].find('P') > c)
			{
				if(i == 0 || i == r - 1)
					ans = min(ans, 1);
				else
				{
					ans = min(ans, 2);
				}
			}
		for(int i=0;i<c;i++)
			if(b[i].find('P') > r)
			{
				if(i == 0 || i == c - 1)
					ans = min(ans, 1);
				else
				{
					ans = min(ans, 2);
				}
			}
		
		if(a[0].find('A') < c || a[r - 1].find('A') < c)
		{
			ans = min(ans, 3);
		}
		if(b[0].find('A') < r || b[c - 1].find('A') < r)
		{
			ans = min(ans, 3);
		}
		printf("%d\n",ans);
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值