HDU6400 多校第8场 Parentheses Matrix 构造思路

A parentheses matrix is a matrix where every element is either ‘(’ or ‘)’. We define the goodness of a parentheses matrix as the number of balanced rows (from left to right) and columns (from up to down). Note that:

  • an empty sequence is balanced;
  • if A is balanced, then (A) is also balanced;
  • if A and B are balanced, then AB is also balanced.

For example, the following parentheses matrix is a 2×4 matrix with goodness 3, because the second row, the second column and the fourth column are balanced:

)()(
()()

Now, give you the width and the height of the matrix, please construct a parentheses matrix with maximum goodness.
Input
The first line of input is a single integer T (1≤T≤50), the number of test cases.

Each test case is a single line of two integers h,w (1≤h,w≤200), the height and the width of the matrix, respectively.
Output
For each test case, display h lines, denoting the parentheses matrix you construct. Each line should contain exactly w characters, and each character should be either ‘(’ or ‘)’. If multiple solutions exist, you may print any of them.
Sample Input
3
1 1
2 2
2 3
Sample Output
(
()
)(
(((
)))

分析
1.对于n和m都是奇数的情况,随便构造,因为这样每一行都是不完美匹配的
2.对于n和m中有一个是奇数的情况下,很容易想到
(((
)))
也就是最多可以让奇数个匹配
3.关于奇数的都好想,关键是对于偶数的情况如何去讨论
首先,对于min(n,m)=2的情况很好想
((((((((
))))))))
然后对于min(n,m)=4的情况
最多是n+m-3匹配成功
()()
()()
(())
(())
对于其他情况,就是n+m-4种,也就是牺牲掉第一行最后一行第一列,最后一列。
( ( ( ( ( (
( ) ( ) ( )
( ( ) ( ) )
( ) ( ) ( )
( ( ) ( ) )
) ) ) ) ) )

很巧妙的构造方法

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


char a[210][210];

int main()
{
    int T;
      //  freopen("in.txt","r",stdin);
    //freopen("d.txt","w",stdout);
    scanf("%d",&T);

    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        if(n%2&&m%2)
        {
            for(int i=0;i<n;i++)
              for(int j=0;j<m;j++)
                a[i][j]='(';
        }
        else if(n%2&&m%2==0)
        {
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                    a[i][j] =  j%2 ? ')' : '(';
        }
        else if(n%2==0&&m%2)
        {
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                    a[i][j]= i%2 ? ')' : '(';
        }
        else
        {
            int m1=min(n,m),m2=max(m,n);
            if(m1==2)
            {
                if(n<=m)
                    for(int i=0;i<m;i++)
                      a[0][i]='(',a[1][i]=')';
                else
                    for(int i=0;i<n;i++)
                      a[i][0]='(',a[i][1]=')';
            }
            else if(m1==4)
            {
                if(n<=m)
                {
                    for(int i=0;i<m/2;i++)
                    {
                        a[0][i]=a[2][i]='(';
                        a[1][i]=a[3][i]=')';
                    }
                    for(int i=m/2;i<m;i++)
                    {
                        a[0][i]=a[1][i]='(';
                        a[2][i]=a[3][i]=')';
                    }
                }
                else
                {
                    for(int i=0;i<n/2;i++)
                    {
                        a[i][0]=a[i][2]='(';
                        a[i][1]=a[i][3]=')';
                    }
                    for(int i=n/2;i<n;i++)
                    {
                        a[i][0]=a[i][1]='(';
                        a[i][2]=a[i][3]=')';
                    }
                }
            }
            else
            {
                for(int j=0;j<m;j++)
                 a[0][j]='(',a[n-1][j]=')';
                for(int i=1;i<n-1;i++)
                    a[i][0]='(',a[i][m-1]=')';
                for(int i=1;i<n-1;i++)
                    for(int j=1;j<m-1;j++)
                {
                    if(i%2&&j%2) a[i][j]='(';
                    if(i%2&&j%2==0) a[i][j]=')';
                    if(i%2==0&&j%2) a[i][j]=')';
                    if(i%2==0&&j%2==0) a[i][j]='(';
                }
            }
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                putchar(a[i][j]);
            }
            puts("");
        }
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值