<hdu多校round 8>

Solved:2

rank:141

D. Parentheses Matrix

n,m有一个小于6的时候是一种构造方法 答案是n + (m - 2) / 2 (n > m)

都大于6的时候 可以在外面先填一圈 然后没两行间错开 答案是 n + m - 4 

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n, m;
        scanf("%d%d", &n, &m);
        if(n % 2 == 1 && m % 2 == 1)
        {
            for(int i = 1; i <= n; i++)
            {
                for(int j = 1; j <= m; j++) printf("(");
                puts("");
            }
        }
        else if(n % 2 == 0 && m % 2 == 0)
        {
            if(n >= 6 && m >= 6)
            {
                for(int i = 1; i <= m; i++) printf("(");
                puts("");
                for(int i = 1; i <= n - 2; i++)
                {
                    if(i % 2 == 1)
                    {
                        printf("(");
                        for(int j = 1; j <= (m - 2) / 2; j++) printf("()");
                        puts(")");
                    }
                    else
                    {
                        printf("(");
                        for(int j = 1; j <= (m - 2) / 2; j++) printf(")(");
                        puts(")");
                    }
                }
                for(int i = 1; i <= m; i++) printf(")");
                puts("");
            }
            else 
            {
                if(n >= m)
                {
                    for(int i = 1; i <= n; i++)
                    {
                        if(i % 2 == 1)
                        {
                            printf("(");
                            for(int j = 1; j <= (m - 2) / 2; j++) printf("()");
                            puts(")");
                        }
                        else
                        {
                            for(int j = 1; j <= m / 2; j++) printf("()");
                            puts("");
                        }
                    }
                }
                else
                {
                
                    for(int i = 1; i <= m; i++) printf("(");
                    puts("");
                    for(int i = 1; i <= n - 2; i++)
                    {
                        if(i % 2 == 1) for(int j = 1; j <= m / 2; j++) printf("()");
                        else for(int j = 1; j <= m / 2; j++) printf(")(");
                        puts("");
                    }
                    for(int i = 1; i <= m; i++) printf(")");
                    puts("");
                }
            }
        }
        else if(n % 2 == 0 && m % 2 == 1)
        {
            for(int i = 1; i <= n; i++)
            {
                if(i <= n / 2)
                    for(int j = 1; j <= m; j++) printf("(");
                else
                    for(int j = 1; j <= m; j++) printf(")");
                puts("");
            }
        }
        else if(n % 2 == 1 && m % 2 == 0)
        {
            for(int i = 1; i <= n; i++)
            {
                for(int j = 1; j <= m / 2; j++) printf("(");
                for(int j = 1; j <= m / 2; j++) printf(")");
                puts("");
            }
        }
    }
    return 0;
}
View Code

E. Magic Square

#include <bits/stdc++.h>
using namespace std;

char tu[5][5];
int ax, ay, bx, by, cx, cy, dx, dy;
void rotate(int cl)
{
    if(cl == -1)
    {
        char tmp = tu[ax][ay];
        tu[ax][ay] = tu[bx][by];
        tu[bx][by] = tu[cx][cy];
        tu[cx][cy] = tu[dx][dy];
        tu[dx][dy] = tmp;
    }
    else
    {
        char tmp = tu[ax][ay];
        tu[ax][ay] = tu[dx][dy];
        tu[dx][dy] = tu[cx][cy];
        tu[cx][cy] = tu[bx][by];
        tu[bx][by] = tmp;
    }
}

int main()
{
    int T;
    scanf("%d", &T);
    char s[5];
    while(T--)
    {
        int n; scanf("%d", &n);
        for(int i = 1; i <= 3; i++) scanf("%s", tu[i] + 1);
        for(int i = 1; i <= n; i++)
        {
            scanf("%s", s);
            if(s[0] == '1')
            {
                ax = 1, ay = 1;
                bx = 1, by = 2;
                cx = 2, cy = 2;
                dx = 2, dy = 1;
            }
            else if(s[0] == '2')
            {
                ax = 1, ay = 2;
                bx = 1, by = 3;
                cx = 2, cy = 3;
                dx = 2, dy = 2;
            }
            else if(s[0] == '3')
            {
                ax = 2, ay = 1;
                bx = 2, by = 2;
                cx = 3, cy = 2;
                dx = 3, dy = 1;
            }
            else if(s[0] == '4')
            {
                ax = 2, ay = 2;
                bx = 2, by = 3;
                cx = 3, cy = 3;
                dx = 3, dy = 2;
            }
            
            if(s[1] == 'C') rotate(1);
            else rotate(-1);
        }
        for(int i = 1; i <= 3; i++)
        {
            for(int j = 1; j <= 3; j++) printf("%c", tu[i][j]);
            puts("");
        }
        
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/lwqq3/p/9503477.html