2018牛客多校训练---Beautiful Garden(思维题)

链接:https://www.nowcoder.com/acm/contest/142/F
来源:牛客网
 

题目描述

There's a beautiful garden whose size is n x m in Chiaki's house. The garden can be partitioned into n x m equal-sized square chunks. There are some kinds of flowers planted in each square chunk which can be represented by using lowercase letters.
However, Chiaki thinks the garden is not beautiful enough. Chiaki would like to build a water pool in the garden. So that the garden would look like symmetric (both horizontally and vertically). The water pool is a rectangle whose size is p x q and the center of the water pool is also the center of the garden.
Something else important you should know is:

  • n, m, p and q are all even.
  • p is always less than n.
  • q is always less than m.
  • The borders of the water pool are parallel to the border of garden.

Chiaki would like to know the number of different pairs of (p, q) she can choose.

输入描述:

There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 100) indicating the number of test cases. For each test case:
The first line contains two integers n and m (1 ≤ n, m ≤ 2000, n and m are even) -- the size of the garden. For next n lines, each line contains m characters showing the garden. It is guaranteed that only lowercase letters will appear.

输出描述:

For each test case, output an integer indicating the number of choices to build the water pool.

 

示例1

输入

3
6 8
acbbbbca
dcaccacd
cdaddadc
cdaddadc
dcaccacd
acbbbbca
6 8
acbcbbca
dcaccacd
cdaddadc
cdaddadc
dcaccacd
acbbbbca
6 8
acbbbbca
dcadcacd
cdaddadc
cdaddadc
dcaccacd
acbbbbca

输出

6

0

3

 

 一个n*m的花园,每一个格子上有一个小写字母代表上面种的花的种类,在花园上挖一个p*q的水池,问有多少种挖法使花园上下左右对称(花的种类)。m,n,p,q都为偶数,p,q严格小于n,m。

  思路:

  数据范围比较小,直接暴力对称比较就可以。找每一行和每一列最小的对称长度,即挖水池可以选择的边界。相乘。

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

const int N=2010;

int n,m;
char ch[N][N];
int cnt1[N],cnt2[N];

void judge()
{
    int i,j;
    bool f=0;
    for(i=1;i<=n;i++)
    {
        f=0;
        for(j=1;j<=m/2;j++)
        {
            if(ch[i][j]!=ch[i][m-j+1])
            {
                f=1;
                break;
            }
        }
        if(f)
            cnt1[i]=j;
        else
            cnt1[i]=m/2;
    }
    f=0;
    for(i=1;i<=m;i++)
    {
        f=0;
        for(j=1;j<=n/2;j++)
        {
            if(ch[j][i]!=ch[n-j+1][i])
            {
                f=1;
                break;
            }
        }
        if(f)
            cnt2[i]=j;
        else
            cnt2[i]=n/2;
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int i,j;
        scanf("%d%d",&n,&m);
        getchar();
        for(i=1;i<=n;i++)
        {
            scanf("%s",ch[i]+1);
        }
        judge();
        int t=2010;
        for(i=1;i<=n;i++)
        {
            t=min(t,cnt1[i]);
        }
        int tt=2010;
        for(i=1;i<=m;i++)
        {
            tt=min(tt,cnt2[i]);
        }
        printf("%d\n",(t-1)*(tt-1));
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值