http://acm.hdu.edu.cn/showproblem.php?pid=6400
| 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:
Input The first line of input is a single integer T (1≤T≤50) , the number of test cases.
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都为偶数n<=6的规律,后面的那种神操作我没有想到

代码:【虽然长,但是超级好理解】
#include<bits/stdc++.h>
#define ll long long
using namespace std;
char a[505][505];
int main()
{
int t,i,j,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
if(n%2&&m%2)
{
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("(");
printf("\n");
}
continue;
}
else
if(n%2)
{
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%2)
{
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<=6||m<=6){
if(n>=m)
{
for(i=0;i<n;i++)
{
if(i%2==1)
{
a[i][0]='(';
for(j=1;j<m-1;j++){
if(j%2==1)
a[i][j]='(';
else
a[i][j]=')';
}
a[i][j]=')';
}
else
{
for(j=0;j<m;j++){
if(j%2==0)
a[i][j]='(';
else
a[i][j]=')';
}
}
}
}
else
{
for(i=0;i<m;i++)
{
if(i%2==1)
{
int len;
a[0][i]='(';
len=n-1;
for(j=1;j<len;j++){
if(j%2==1)
a[j][i]='(';
else
a[j][i]=')';
}
a[j][i]=')';
}
else
{
for(j=0;j<n;j++){
if(j%2==0)
a[j][i]='(';
else
a[j][i]=')';
}
}
}
}
}
else
{
for(i=0;i<m;i++)
a[0][i]='(',a[n-1][i]=')';
for(i=0;i<n;i++)
a[i][0]='(',a[i][m-1]=')';
for(i=1;i<n-1;i++)
{
if(i%2){
for(j=1;j<m-1;j++){
if(j%2)
a[i][j]=')';
else
a[i][j]='(';
}
}
else
{
for(j=1;j<m-1;j++){
if(j%2)
a[i][j]='(';
else
a[i][j]=')';
}
}
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%c",a[i][j]);
printf("\n");
}
}
return 0;
}
本文探讨了如何构造一个括号矩阵,使其行和列的平衡数量最大化。通过分析不同情况下的规律,提供了一种算法实现,适用于宽度和高度不超过200的矩阵。
516

被折叠的 条评论
为什么被折叠?



