HDU 多校 6400 Parentheses Matrix(分情况构造)

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

这里写图片描述

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i=0; i<(n); i++)
using namespace std;
int m, n;
bool rev;
char g[256][256];
int main()
{
    //freopen("D://rush.txt","r",stdin);
    //freopen("D://out.txt","w",stdout);
    int T; cin >> T;
    while (T--)
    {
        cin >> m >> n;
        memset(g, 0, sizeof g);
        rev = false;
        if (m & 1)
        {
            for(int i=0;i<m;i++)
                for(int j=0;j<n;j++)
                    g[i][j] = (j >= n/2 ? ')' : '(');
        }
        else if (n & 1)
        {
            for(int i=0;i<m;i++)
                for(int j=0;j<n;j++)
                    g[i][j] = (i >= m/2 ? ')' : '(');
        }
        else
        {
            if (m > n)
            {
                swap(m, n);
                rev = true;
            }
            if(m==2)
                for(int i=0;i<n;i++)
                {
                    g[0][i] = '(';
                    g[1][i] = ')';
                }
            else if(m==4)
                for(int i=0;i<n;i++)
                {
                    g[0][i] = '(';
                    g[1][i] = (i < n/2 ? ')' : '(');
                    g[2][i] = (i < n/2 ? '(' : ')');
                    g[3][i] = ')';
                }
            else
            {
                for(int i=0;i<m;i++)
                    for(int j=0;j<n;j++)
                    {
                        if (i == 0 || j == 0) g[i][j] = '(';
                        else if (i == m-1 || j == n-1) g[i][j] = ')';
                        else if ((i^j)&1) g[i][j] = ')';
                        else g[i][j] = '(';
                    }
            }
        }
        if (rev)
        {
            for(int j=0;j<n;j++)
            {
                for(int i=0;i<m;i++)
                    cout<<g[i][j];
                cout<<endl;
            }
        }
        else
        {
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<n;j++)
                    cout<<g[i][j];
                cout<<endl;
            }
        }
    }
  return 0;
}

猜你喜欢

转载自blog.csdn.net/ffgcc/article/details/81742118