牛客网暑期ACM多校训练营(第四场)F Beautiful Garden 简单的切割问题

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

Beautiful Garden

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

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 x m的矩阵花园,。花园可以分成n x m个相同大小的方块。每个方块中都种有一些花,用小写字母表示。
Chiaki想在花园里建一个水池,水池的中心也是花园的中心。使花园的花(小写字母) 是对称的(水平和垂直)。
你应该知道的其他重要事项是:
n,m,p和q都是偶数。
p总是小于n。
q总是小于m。
水池的边界与花园的边界平行。
Chiaki想知道她可以选择的不同对(p,q)的数量。

思路:从最外围出发,寻找不对称的点,并记录下最靠近边缘的点的位置,可切割数就是(x-1)*(y-1);

即//最边缘不对称的点(x,y) 距离边缘 可切的范围为  x-1,y-1;

AC代码:

#include <bits/stdc++.h>
using namespace std;
int n,m,t;
char ch[2005][2005];
int main()
{
    scanf("%d",&t);
        while(t--) {
            scanf("%d%d",&n,&m);
            for(int i=1;i<=n;i++) scanf("%s",ch[i]+1);
            int x=n/2,y=m/2;
            for(int i=1;i<=n/2;i++) {
                for(int j=1;j<=m/2;j++) {
                    int a=i,b=n+1-i;
                    int c=j,d=m+1-j;
                    if(ch[a][c]!=ch[a][d]) { x=min(x,i);y=min(y,j); break; }//不对称的点,取边缘的点记录下来
                    if(ch[a][c]!=ch[b][c]) { x=min(x,i);y=min(y,j); break; }
                    if(ch[a][c]!=ch[b][d]) { x=min(x,i);y=min(y,j); break; }
                }
              
            }
            printf("%d\n",( x-1)*(y-1 ));//最边缘不对称的点 距离边缘可切的距离则为x-1,y-1;
        }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41668093/article/details/81271421