HDU 6400 Parentheses Matrix (数学打表找规律)*

Parentheses Matrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 973    Accepted Submission(s): 385
Special Judge

 

Problem Description

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

 

( () )( ((( )))

 

Source

2018 Multi-University Training Contest 8

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6425 6424 6423 6422 6421 

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

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define read(x,y) scanf("%d%d",&x,&y)
/*
给定规格要求构造规格中的一个括号矩阵,
每行每列能够构造出的规范括号序列数量最多。

没啥其他高大上的推导,
感觉就是打表找规律,
唯一麻烦的情况就是n和m均为偶数的情况,
当规格中有一个是2的时候,也很好观察,
最大的数量是max(n,m),
但当一个是4的时候,就需要打表了。。。
因为最大值是n+m-3,因为4的那个规模,其中可以多凑出来一个括号序列,
((((((
)))(((
((()))///多出来的一个序列
))))))

其余情况时,最大值是n+m-4.
((((((
奇数:()()()
偶数:(()())
))))))
牺牲了第一行和第一列,最后一行和最后一列。

*/
const int  maxn =2e5+5;
int n,m,table[250][250];
void draw(int n,int m,int v)
{
    if(v==0)   rep(i,0,n) rep(j,0,m)   table[i][j]=1;
    else if(v==1||v==3)  rep(i,0,n)   for(int j=0;j<m;j+=2) table[i][j]=1,table[i][j+1]=-1;
    else if(v==2||v==4) rep(i,0,m) for(int j=0;j<n;j+=2) table[j][i]=1,table[j+1][i]=-1;
}

int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        if(n&1)
        {
            if(m&1) draw(n,m,0);/// 无法构成
            else draw(n,m,1);
        }
        else
        {
            if(m&1) draw(n,m,2);
            else
            {///最复杂的部分
            ///n和m中有2
            if(min(n,m)==2)
            {
                if(n==2)    for(int i=0;i<m;i++) table[0][i]=1,table[1][i]=-1;
                else   for(int i=0;i<n;i++) table[i][0]=1,table[i][1]=-1;
            }
            else if(min(n,m)==4)
            {
                ///debug
                if(n==4)
                {
                    for(int i=0;i<m;i++) table[0][i]=1,table[3][i]=-1;
                    for(int i=0;i<m;i++)
                    {
                        if(i<m/2) table[1][i]=-1,table[2][i]=1;
                        else table[1][i]=1,table[2][i]=-1;
                    }
                }
                else if(m==4)
                {
                    for(int i=0;i<n;i++) table[i][0]=1,table[i][3]=-1;
                    for(int i=0;i<n;i++)
                    {
                        if(i<n/2) table[i][1]=-1,table[i][2]=1;
                        else table[i][1]=1,table[i][2]=-1;
                    }
                }
            }
            else
            {
            for(int i=0;i<m;i++) table[0][i]=1,table[n-1][i]=-1;
            for(int i=0;i<m;i+=2) table[1][i]=1,table[1][i+1]=-1;

            table[2][0]=1,table[2][m-1]=-1;
            for(int i=1;i<m-1;i+=2) table[2][i]=1,table[2][i+1]=-1;

            for(int i=1;i<n-1;i++)
            {
                if(i&1)   memcpy(table[i],table[1],sizeof(table[1]));
                else memcpy(table[i],table[2],sizeof(table[2]));
            }

            }
            }
        }

        for(int i=0;i<n;i++,puts(""))   for(int j=0;j<m;j++)   printf("%c",(table[i][j]==1)?'(':')');
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81900782