HDU 6400 Parentheses Matrix(构造)

Parentheses Matrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 268    Accepted Submission(s): 101
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.

扫描二维码关注公众号,回复: 2878287 查看本文章

Sample Input

 

3 1 1 2 2 2 3

Sample Output

 

( () )( ((( )))

代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
char a[505][505];
int main()
{
    int t,i,j,n,m;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        if((n&1)&&(m&1))
        {
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                     a[i][j]='(';
            }
        }
        else if(n&1)
        {
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {
                    if(j%2)
                        a[i][j]=')';
                    else
                        a[i][j]='(';
                }
            }
        }
        else if(m&1)
        {
            for(i=0;i<m;i++)
            {
                for(j=0;j<n;j++)
                {
                    if(j%2)
                        a[j][i]=')';
                    else
                        a[j][i]='(';
                }
            }
        }
        else
        {
            if(n==2)
            {
                for(int i=0;i<m;i++)
                {
                    a[0][i]='(';
                    a[1][i]=')';
                }
            }
            else if(m==2)
            {
                for(int i=0;i<n;i++)
                {
                    a[i][0]='(';
                    a[i][1]=')';
                }
            }
            else if(n==4)
            {
                for(i=0;i<n;i++)
                {
                    for(int j=0;j<m;j++)
                    {
                        if(i==0) a[i][j]='(';
                        else if(i==n-1) a[i][j]=')';
                        else if((i&1)&&(j<m/2)) a[i][j]=')';
                        else if(i&1) a[i][j]='(';
                        else if(j<m/2) a[i][j]='(';
                        else a[i][j]=')';
                    }
                }
            }
            else if(m==4)
            {
                for(i=0;i<m;i++)
                {
                    for(int j=0;j<n;j++)
                    {
                        if(i==0) a[j][i]='(';
                        else if(i==m-1) a[j][i]=')';
                        else if((i&1)&&(j<n/2)) a[j][i]=')';
                        else if(i&1) a[j][i]='(';
                        else if(j<n/2) a[j][i]='(';
                        else a[j][i]=')';
                    }
                }
            }
            else if(n>=m)
            {
                for(i=0;i<m;i++)
                {
                    for(int j=0;j<n;j++)
                    {
                        if(i==0) a[j][i]='(';
                        else if(i==m-1) a[j][i]=')';
                        else if((i&1)&&(j&1)) a[j][i]=')';
                        else if(i&1) a[j][i]='(';
                        else if(j==0) a[j][i]='(';
                        else if(j==n-1) a[j][i]=')';
                        else if(j&1) a[j][i]='(';
                        else a[j][i]=')';
                    }
                }
            }
            else
            {
                for(i=0;i<n;i++)
                {
                    for(int j=0;j<m;j++)
                    {
                        if(i==0) a[i][j]='(';
                        else if(i==n-1) a[i][j]=')';
                        else if((i&1)&&(j&1)) a[i][j]=')';
                        else if(i&1) a[i][j]='(';
                        else if(j==0) a[i][j]='(';
                        else if(j==m-1) a[i][j]=')';
                        else if(j&1) a[i][j]='(';
                        else a[i][j]=')';
                    }
                }
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                cout<<a[i][j];
                  cout<<endl;
        }
    }
    return 0;
}

简洁版:

#include<bits/stdc++.h>
#define f for(int i=0;i<(int)n;i++,puts("")) for(int j=0;j<(int)m;j++)
#define e else if
using namespace std;
void p(char ch){cout<<ch;}
int main(){
    int t,n,m;
    cin>>t;
    while(t--){
        cin>>n>>m;
        if((n&1)&&(m&1)) f p('(');
        e (n&1) f
        if(j&1) p(')');
        else p('(');
        e(m&1) f if(i&1) p(')');
        else p('(');
        e(n==2) f if(i&1) p(')');
        else p('(');
        e(m==2) f if(j&1) p(')');
        else p('(');
        e(n==4) f if(i==0) p('(');
        e(i==n-1) p(')');
        e((i&1)&&(j<m/2)) p(')');
        e(i&1) p('(');
        e(j<m/2) p('(');
        else p(')');
        e(m==4) f if(j==0) p('(');
        e(j==m-1) p(')');
        e((j&1)&&(i<n/2)) p(')');
        e(j&1) p('(');
        e(i<n/2) p('(');
        else p(')');
        e(n>=m) f if(j==0) p('(');
        e(j==m-1) p(')');
        e((i&1)&&(j&1)) p(')');
        e(j&1) p('(');
        e(i==0) p('(');
        e(i==n-1) p(')');
        e(i&1) p('(');
        else p(')');
        else f if(i==0) p('(');
        e(i==n-1) p(')');
        e((i&1)&&(j&1)) p(')');
        e(i&1) p('(');
        e(j==0) p('(');
        e(j==m-1) p(')');
        e(j&1) p('(');
        else p(')');
    }
}

猜你喜欢

转载自blog.csdn.net/LSD20164388/article/details/81710607