牛客网多校第四场 F Beautiful Garden(模拟)

链接:https://www.nowcoder.com/acm/contest/142/F

题意:在一个n*m的花园,我们想要这个花园变得更加完美,也就是行对称,列对称,但是花园开始可能是不对称,也可能对称,
中间我们可以把中间的花铲走挖一个p*q池子,花园的中心就是池子的中心,来使花园看上去对称,问挖池子的方法有多少个,
而且n,m,p,q都是偶数,p<n q<m

思路:分别按行与列找最大满足对称的值,最后相乘即可。
#include <bits/stdc++.h>
using namespace std;
const int N = 2E3 + 7;
string s[N];
int a[N], b[N];
int main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    int T;
    cin >> T;
    while(T--)
    {
        int n, m;
        cin >> n >> m;
        for(int i = 0; i < n; i++)
        {
            cin >> s[i];
        }
        int num1 = 0, num2 = 0;
        for(int i = 0; i < n/2; i++)//这里我们直接用num1作间隔,然后直接匹配字符串是否相等,实际上是匹配了列上的字符是否对称
        {
            if(s[i] == s[n-i-1])
            {
                num1++;
            }
            else
            {
                break;
            }
        }
        for(int i = 0; i < m/2; i++)//因为行的字符不能直接用字符串相等,所以我们判断下回文
        {
            int flag = 1;
            for(int j = 0; j < n; j++)
            {
                if(s[j][i] != s[j][m-i-1])
                {
                    flag = 0;
                    break;
                }
            }
            if(flag)
            {
                num2++;
            }
            else
            {
                break;
            }
        }
        if(num1 == 0 || num2 == 0)
        {
            cout << 0 << endl;
        }
        else
        {
            if(num1*2 == n) num1--;//如果到了边界减一,我们不能把花园边界挖掉
            if(num2*2 == m) num2--;
            cout << num1*num2 << endl;//最终答案
        }
    }
 
}

猜你喜欢

转载自blog.csdn.net/reallsp/article/details/81316449