[ZOJ 3810 A Volcanic Island] 构造+调整

题目

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3810

分析

赛中想了个不完善的想法,没有AC,赛后膜拜了上交大神的代码,看到了这个巧妙的构造方法

空出最后一列,然后前n/2个从左往右放,之后n/2+1到n-1个按照蛇形放,此时第一个和第n-1个相同,调整前面三个即可使得不重复即可

特判掉1,5,6,也容易发现2,3,4无解

代码

/**************************************************
 *        Problem:  ZOJ 3810
 *         Author:  clavichord93
 *          State:  Accepted
 **************************************************/

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

char character[] = {'B', 'R', 'G'};

const int MAX_N = 105;

int n;
int ans[MAX_N][MAX_N];

int main() {
    int T;
    scanf("%d", &T);
    for (int cas = 1; cas <= T; cas++) {
        scanf("%d", &n);

        if (n == 1) {
            printf("R\n");
            continue;
        }

        if (n <= 4) {
            printf("No solution!\n");
            continue;
        }
        
        if (n == 5) {
            printf("YYYGR\nYGGGR\nYGYYR\nBYYYR\nBBBBR\n");
            continue;
        }

        if (n == 6) {
            printf("RRRRRY\nRGGGGY\nGGRRRY\nRRRGBY\nGGGGBY\nGBBBBY\n");
            continue;
        }

        memset(ans, 0, sizeof(ans));
        int x = 0;
        int y = 0;
        for (int i = 1; i <= n / 2; i++) {
            for (int j = 0; j < n; j++) {
                ans[x][y] = i;
                y++;
                if (y == n - 1) {
                    y = 0;
                    x++;
                }
            }
        }
        int delta = 1;
        for (int i = n / 2 + 1; i < n; i++) {
            for (int j = 0; j < n; j++) {
                ans[x][y] = i;
                y += delta;
                if (y == n - 1) {
                    y = n - 2;
                    x++;
                    delta = -1;
                }
                if (y == -1) {
                    y = 0;
                    x++;
                    delta = 1;
                }
            }
        }
        ans[1][n - 3] = 1;
        ans[0][n - 2] = 2;
        ans[2][n - 2] = 2;
        ans[2][n - 3] = 2;
        ans[2][n - 4] = 2;
        ans[2][0] = 3;
        ans[1][1] = 3;
        ans[2][1] = 3;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (ans[i][j] == 0) {
                    printf("Y");
                }
                else {
                    printf("%c", character[ans[i][j] % 3]);
                }
            }
            printf("\n");
        }
    }

    return 0;
}


你可能感兴趣的:([ZOJ 3810 A Volcanic Island] 构造+调整)