http://acm.hdu.edu.cn/showproblem.php?pid=6400
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: 6408 6407 6406 6405 6404
h>=6时,第一行发现((()))这样构造会浪费三列,所以((((((构造更高,最后一行,第一列,最后一列类似
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t,r,c,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&r,&c);
if(r%2&&c%2)
{
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("(");
}
printf("\n");
}
}
else if(r%2&&c%2==0)
{
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j+=2)
{
printf("()");
}
printf("\n");
}
}
else if(r%2==0&&c%2)
{
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(i%2)
printf("(");
else
printf(")");
}
printf("\n");
}
}
else if(r==2||c==2)
{
if(r==2)
{
for(j=1;j<=c;j++)
printf("(");
printf("\n");
for(j=1;j<=c;j++)
printf(")");
printf("\n");
}
else
{
for(i=1;i<=r;i++)
printf("()\n");
}
}
else if(r==4||c==4)
{
if(r==4)
{
for(j=1;j<=c;j++)
printf("(");
printf("\n");
for(j=1;j<=c/2;j++)
printf(")");
for(j=c/2+1;j<=c;j++)
printf("(");
printf("\n");
for(j=1;j<=c/2;j++)
printf("(");
for(j=c/2+1;j<=c;j++)
printf(")");
printf("\n");
for(j=1;j<=c;j++)
printf(")");
printf("\n");
}
else
{
for(i=1;i<=r;i+=2)
{
printf("()()\n");
printf("(())\n");
}
}
}
else if(r%2==0&&c%2==0)
{
for(i=1;i<=r;i++)
{
if(i==1)
{
for(j=1;j<=c;j++)
printf("(");
printf("\n");
continue;
}
if(i==r)
{
for(j=1;j<=c;j++)
printf(")");///
printf("\n");
continue;
}
for(j=1;j<=c;j++)
{
if(i%2==0)
{
for(j=1;j<=c;j+=2)
{
printf("()");
}
printf("\n");
}
else
{
printf("(");
for(j=2;j<=c-1;j+=2)
{
printf("()");
}
printf(")\n");
}
}
}
}
}
}