Parentheses Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 216 Accepted Submission(s): 82
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
(
()
)(
(((
)))
只为了记录了一下自己补的题
看了题解才知道没考虑n或m为4的这个特殊情况,赛场上一直没找到
附上题解图片一张,解释的很清楚
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d %d",&n,&m);
//n,m为奇数时
if((n & 1) && (m & 1)){
for(int i = 0;i < n;++i)
{
for(int j = 0;j < m;++j)
{
printf("(");
}
printf("\n");
}
}
//n为奇,m为偶
if((n & 1) && !(m & 1)){
for(int i = 0;i < n;++i)
{
for(int j = 0;j < m;j += 2)
{
printf("()");
}
printf("\n");
}
}
//n为偶,m为奇
if((m & 1) && !(n & 1)){
for(int i = 0;i < n;++i)
{
if(i % 2 == 0){
for(int j = 0;j < m;++j)
{
printf("(");
}
}
else{
for(int j = 0;j < m;++j)
{
printf(")");
}
}
printf("\n");
}
}
//n,m为偶时,对2,4特殊处理一下
if(!(n & 1) && !(m & 1)){
if(n == 2){
for(int i = 0;i < m;++i)
{
printf("(");
}
printf("\n");
for(int i = 0;i < m;++i)
{
printf(")");
}
printf("\n");
continue;
}
if(m == 2){
for(int i = 0;i < n;++i)
{
printf("()\n");
}
continue;
}
if(n == 4){
for(int i = 0;i < m;++i)
{
printf("(");
}
printf("\n");
for(int i = 0;i < m / 2;++i)
{
printf(")");
}
for(int i = 0;i < m / 2;++i)
{
printf("(");
}
printf("\n");
for(int i = 0;i < m / 2;++i)
{
printf("(");
}
for(int i = 0;i < m / 2;++i)
{
printf(")");
}
printf("\n");
for(int i = 0;i < m;++i)
{
printf(")");
}
printf("\n");
continue;
}
if(m == 4){
for(int i = 0;i < n / 2;++i)
{
printf("()()\n");
}
for(int i = 0;i < n / 2;++i)
{
printf("(())\n");
}
continue;
}
if(n >= 6){
for(int i = 0;i < m;++i)
{
printf("(");
}
printf("\n");
for(int i = 1;i < n - 1;++i)
{
if(i % 2 != 0){
for(int j = 0;j < m;j += 2){
printf("()");
}
printf("\n");
}
else{
printf("(");
for(int j = 1;j < m - 1;j += 2){
printf("()");
}
printf(")\n");
}
}
for(int i = 0;i < m;++i)
{
printf(")");
}
printf("\n");
}
}
}
return 0;
}