hdu 6400-Parentheses Matrix(多校8-1004)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38749759/article/details/81775090

                          Parentheses Matrix

Time Limit: 2000/1000 MS (Java/Others)    

Memory Limit: 131072/131072 K (Java/Others)

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

好了,先直接上代码,然后再附上具体思路

#include<algorithm>
  #include<cstring>
   #include<cstdio>
using namespace std;
const int maxn=1000;
char ans[maxn][maxn];
int main()
{
    int n,m,T;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        if(n%2&&m%2){
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++)
                    printf("(");
                printf("\n");
            }
        }
        else if(n%2==0&&m%2){
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    if(i%2==0)    printf("(");
                    else          printf(")");       
                }
                printf("\n");
            }
        }
        else if(n%2&&m%2==0){
            for(int i=0;i<n;i++){
                for(int j=0;j<m/2;j++)   printf("()");
                printf("\n");
            } 
        }
        else{
            if(min(m,n)<=6){
                if(n==min(n,m)){
                        for(int i=0;i<m;i++)
                        {  ans[0][i]='(';   ans[n-1][i]=')';  }       
                         int ct=0;
                          for(int i=1;i<n-1;i++){
                            ct=0;
                                for(int j=0;j<m;j++){
                                    if(i%2!=0){
                                        if(ct==0)    ans[i][j]=')';
                                        else         ans[i][j]='(';
                                    }
                                    else{
                                        if(ct==0)    ans[i][j]='(';
                                        else         ans[i][j]=')';   
                                }
                                ct=(ct+1)%2;
                        }
                    }    
                }
                else{
                        for(int i=0;i<n;i++)
                         {   ans[i][0]='(';      ans[i][m-1]=')'; }
                        int ct=0;
                        for(int i=1;i<m-1;i++){
                            ct=0;
                            for(int j=0;j<n;j++){
                                if(i%2!=0){
                                    if(ct==0)     ans[j][i]=')';
                                    else          ans[j][i]='(';    
                                 }
                                else{
                                    if(ct==0)     ans[j][i]='(';
                                    else          ans[j][i]=')';
                                 }
                             ct=(ct+1)%2;
                             }
                        }    
                }
            }
            else{
                  for(int i=1;i<m-1;i++)
                      {  ans[0][i]='(';  ans[n-1][i]=')';    }
                  for(int i=1;i<n-1;i++)
                      {  ans[i][0]='(';  ans[i][m-1]=')';    }
                    ans[0][0]=ans[n-1][0]='(';
                    ans[0][m-1]=ans[n-1][m-1]=')';
                        int ct=0;
                    for(int i=1;i<n-1;i++){
                        ct=0;
                           for(int j=1;j<m-1;j++){
                            if(i%2!=0){
                                if(ct==0)         ans[i][j]=')';
                                else              ans[i][j]='(';
                            }
                            else{
                                if(ct==0)        ans[i][j]='(';
                                else             ans[i][j]=')';
                            }
                        ct=(ct+1)%2;
                    }
                }
            }
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++)
                    printf("%c",ans[i][j]);
                printf("\n");
            }
        }    
    }
    return 0;    
} 

###############################################################################################

题目大意:

给你l两个整数n,m,让你输出一个(n行m)括号矩阵让它的行和列的匹配度最大,

匹配就要一行一行全都匹配,一列一列的全部匹配

  每个位置只能是左括号或者右括号, 这三种“()”,“(())”,“()()”是匹配的。

ps:但这个‘())’就不是匹配的

例如

4 4
((((
)()(
()()
))))

这括号的匹配度为 5;

按行数,第三行符合要求(1)

按列数,每一列都符合要求(4)

所以这个匹配度为(4+1)5;

解题思路:

找规律+分类讨论

当n,m都为奇数时,

          无论那你怎么画,匹配度都为0;所有我是默认全是‘(’;

当n为偶数,m为奇数,

         我找到的矩阵最大匹配画法:奇数行数全是‘(’,偶数行全是‘)’;

当m为偶数,n为奇数,

         我找到的矩阵最大匹配画法:每行都用“()”填充;

当m为偶数,n为偶数(ps:比赛时就关于它的画法找的有点问题了啊,ε=(´ο`*))) ~~)

       当min(m,n)<=6时这一类有两种画法

             当n为最小的那个数是:

                   第一行全为‘(’,最后一行为‘)’

                  中间按行填充(对于填充第奇数行时“)” , “(”填充,对于填充第偶数行时“(”,“)”填充)

             当m为最小的那个数是:

                  第一列全为‘(’,最后一列为‘)’

                  中间按列填充(对于填充第奇数列时“)” , “(”填充,对于填充第偶数列时“(”,“)”填充)

       当min(m,n)>6时

                    通过自己多画几个找规律,最后得知矩阵最大匹配的画法:先加个框,然后在框里按照如下规则填入。

         框模式(8*8矩阵为例)     框里括号填充规则

         ((((((()                                 对于填充第奇数行时“)”,“(”填充
         (......)                                  对于填充第偶数行时“(”,“)”填充
         (......)
         (......)
         (......)
         (......)
         (......)
         ()))))))

    ############################################################################################

下面就是关于m,n都为偶数的推到过程(参考博客http://www.cnblogs.com/tobyw/p/9483865.html

     贪心一下,起点位于第一行和第一列,所以应该尽量在这些位置填'(',

首先想到的是把矩形的左上边界填充为'(',右下边界填充为')'
因为第一行,第n行,第1列,第m列一定不是序列,所以这样最多有n+m-4个合法括号序列。

但有一个情况比较特殊,当n=4的时候,上面的方法比较亏,以牺牲第一列和最后一列的代价,却只得到了两行合法括号序列。

考虑另外一种填充方法:当n比较小的时候,把第一行全部填充为'(',最后一行全部填充为')'

这样以后发现,可以通过调整剩下的位置,让剩下一半的行数成为合法的序列,于是最多有(n-2)/2+m=n/2-1+m个合法括号序列

比较一下上面两种方案,因为n和m是可以互换的,不妨假设m>n,

第一种方案最多有m+n-4个合法序列,

第二种方案最多应该有m+n/2-1,当他们相等时,m+n-4=m+n/2-1,解得n=6,

也就是n,m较小的那个比6小的时候,采用第二种方案可以获得更多序列,而n,m都大于等于6的时候应该选择第一种情况。

猜你喜欢

转载自blog.csdn.net/qq_38749759/article/details/81775090
今日推荐