hdu 6400(构造)

Parentheses Matrix

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

思路: 

这里n和m可以互换》》》

1. 对于行列都是奇数的情况,肯定是一个没有

2.对于行列中有一个是奇数一个是偶数的情况,答案肯定是奇数

3. 对于行列都是偶数的情况我们这样考虑,如果我不放弃第一列,也就是使得第一列为匹配的,所以(1,1)肯定是左括号,如果第一列是匹配并且n>=6 ,那么就相当于我们放弃了n/2行, 并且,如果我们使得第一行为匹配的呢还是最后一列是匹配的呢。这两个也是一个矛盾,所以在n>=6 的情况下其实是放弃第一列,最优的状态就是n+m-4,

((((((((
()()()()
(()()())
()()()()
(()()())
))))))))
 

在n<=4 的状态下,如果我们考虑不放弃第一列,那么我们不放弃第一列,那么肯定会有n/2行是不匹配的,并且右上角如何选择会减掉一个,所以最优的状态其实是n+m-3 . 

((((((
)))(((
((()))
))))))

代码: 上半部分垃圾暴力,下半部分代码

/*#include<bits/stdc++.h>

using namespace std;
const int N =15;
int mp[N][N];
int n,m;
int maxx=-1;

void hang(int &sum)
{
    stack<char >s;
    for(int i=1;i<=n;i++){
        while(!s.empty()) s.pop();
        int f=0;
        for(int j=1;j<=m;j++){
            if(mp[i][j]==1){
                s.push('(');
            }
            else{
                if(s.empty()){
                    f=1; break;
                }
                else{
                    s.pop();
                }
            }
        }
        if(f||!s.empty()) continue;
        sum++;
    }
}

void lie(int &sum)
{
    stack< char >s;
    for(int j=1;j<=m;j++){
        while(!s.empty()) s.pop();
        int f=0;
        for(int i=1;i<=n;i++){
            if(mp[i][j]==1){
                s.push('(');
            }
            else{
                if(s.empty()){
                    f=1; break;
                }
                else s.pop();
            }
        }
        if(f||!s.empty()) continue;
        sum++;
    }
}

void jud()
{
    int tot=0;
    hang(tot);
    lie(tot);
    if(tot>maxx){
        maxx=tot;
        cout<<"maxx "<<maxx<<endl;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(mp[i][j]==1){
                    printf("( ");
                }
                else printf(") ");
            }
            printf("\n");
        }
    }
}

void dfs(int x,int y)
{
    if(x==n+1){
        jud();
        return ;
    }
    mp[x][y]=1;
    int tmpx,tmpy;
    if(y==m){
        tmpx=x+1; tmpy=1;
    }
    else{
        tmpx=x; tmpy=y+1;
    }
    dfs(tmpx,tmpy);
    mp[x][y]=2;
    dfs(tmpx,tmpy);
}

int main()
{
    while(cin>>n>>m)
    {
        maxx=-1;
        dfs(1,1);
    }


    return 0;
}
*/

#include<bits/stdc++.h>

using namespace std;
const int N =205;
int n,m;
int mp[N][N];

int main()
{
    //freopen("d.in","r",stdin);
    //freopen("dd.out","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&m);
        int odd=0,even=0;
        if(n%2==1) odd++;
        else even++;
        if(m%2==1) odd++;
        else even++;

        if(odd==2)
        {
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++) mp[i][j]=1;
            }
        }
        else if(even&&odd)
        {
            if(n%2==0)   /// 如果行是偶数
            {
                for(int i=1; i<=n/2; i++)
                {
                    for(int j=1; j<=m; j++)
                    {
                        mp[i][j]=1;
                    }
                }
                for(int i=n/2+1; i<=n; i++)
                {
                    for(int j=1; j<=m; j++)
                    {
                        mp[i][j]=2;
                    }
                }
            }
            else
            {
                for(int i=1; i<=n; i++)
                {
                    for(int j=1; j<=m; j++)
                    {
                        if(j<=m/2) mp[i][j]=1;
                        else mp[i][j]=2;
                    }
                }
            }
        }
        else
        {
            if(n<=m)
            {
                for(int i=1; i<=n/2; i++)
                {
                    if(i%2==1)
                    {
                        for(int j=1; j<=m; j++) mp[i][j]=1;
                    }
                    else
                    {
                        for(int j=1; j<=m; j++)
                        {
                            if(j<=m/2) mp[i][j]=1;
                            else mp[i][j]=2;
                        }
                    }
                }

                int tot=0;
                for(int i=n; i>n/2; i--)
                {
                    ++tot;
                    if(tot%2==1)
                    {
                        for(int j=1; j<=m; j++) mp[i][j]=2;
                    }
                    else
                    {
                        for(int j=1; j<=m; j++)
                        {
                            if(j<=m/2) mp[i][j]=2;
                            else mp[i][j]=1;
                        }
                    }
                }

            }
            else
            {
                for(int i=1; i<=n/2; i++)
                {
                    for(int j=1; j<=m; j++)
                    {
                        if(j%2==1) mp[i][j]=1;
                        else mp[i][j]=2;
                    }
                }

                for(int i=n/2+1; i<=n; i++)
                {
                    for(int j=1; j<=m; j++)
                    {
                        if(j<=m/2) mp[i][j]=1;
                        else mp[i][j]=2;
                    }
                }
            }

            if(n>=6&&m>=6)
            {
                for(int j=1; j<=m; j++) mp[1][j]=1;
                for(int i=2; i<=n-1; i++)
                {
                    if(i%2==0)
                    {
                        mp[i][1]=1;
                        mp[i][m]=2;
                        for(int j=2; j<m; j++)
                        {
                            if(j%2==0) mp[i][j]=1;
                            else mp[i][j]=2;
                        }
                    }
                    else
                    {
                        for(int j=1; j<=m; j++)
                        {
                            if(j%2==1) mp[i][j]=1;
                            else mp[i][j]=2;
                        }
                    }
                }
                for(int j=1; j<=m; j++) mp[n][j]=2;
            }

        }

        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                if(mp[i][j]==1) printf("(");
                else printf(")");
            }
            printf("\n");
        }
    }

    return 0;
}


/*

((((((
)))(((
((()))
))))))

((((((((
()()()()
(()()())
()()()()
(()()())
))))))))

*/

猜你喜欢

转载自blog.csdn.net/yjt9299/article/details/81712051