cf1280B

题意:给出一个n*m的矩阵,矩阵中的元素要么P要么是A

每次可以选择一个的子矩形,然后将矩阵中每一行都变为第一行,或者将矩阵中每一列都变为第一列

要求用最少的次数将矩阵中所有元素都变成A

题解:分类讨论,最多四次操作

#include<bits/stdc++.h>
#define forn(i, n) for (int i = 0 ; i < int(n) ; i++)
#define fore(i, s, t) for (int i = s ; i < (int)t ; i++)
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define pf2(x,y) printf("%d %d
",x,y)
#define pf(x) printf("%d
",x)
#define each(x) for(auto it:x)  cout<<it<<endl;
#define pii pair<int,int>
using namespace std;
typedef long long ll;
const int maxn=4e5+5;
const int maxm=2e5+5;
const int inf=1e9;
int n,m;
vector<string> a;
int calc(){
	int total=0;
	vector<int> row(n,0),col(m,0);
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(a[i][j]=='A') {
				row[i]++;
				col[j]++;
				total++;
			}
	if(total==0) return -1;
	if(total==n*m) return 0;
	if(row[0]==m || row.back()==m || col[0]==n || col.back()==n) return 1;
	if(a[0][0]=='A' || a[0].back()=='A' || a[n-1][0]=='A' || a[n-1].back()=='A') return 2;
	if(*max_element(all(row))==m || *max_element(all(col))==n) return 2;
	if(row[0] || row.back() || col[0] || col.back()) return 3;
	return 4;
}
void solve(){
	cin>>n>>m;
	a.resize(n);
	for(int i=0;i<n;i++)
		cin>>a[i];
	int re=calc();
	if(re==-1) puts("MORTAL");
	else cout<<re<<"
";
}
int main(){
	int t;
	cin>>t;
	while(t--)
		solve();
}

  

原文地址:https://www.cnblogs.com/033000-/p/12378350.html