洛谷P4327 [COCI2006-2007#1] Okviri 题解 简单模拟

题目链接:https://www.luogu.com.cn/problem/P4327

解题思路:

简单模拟。

首先,设字符串长度为 (n),则输出的图形是一个 (5 imes (4 cdot n + 1)) 的图形。

英文字符都在第 (3) 行,列对应的数字均为 (mod 4 = 3) 的那些列,每三个字符有特殊的,特殊的列号都是 (mod 12 = 11) 的那些列。

其中 # 或者 * 和字符的汉密尔顿距离都是 (2)

因为 #* 可能有重叠的,所以先涂 # 再涂 *

基于这个规律,我们可以编写代码如下:

#include <bits/stdc++.h>
using namespace std;
char s[22], ans[6][110];
int n;
int dir[8][2] = {
    -2, 0,
    -1, -1, -1, 1,
    0, -2, 0, 2,
    1, -1, 1, 1,
    2, 0
};
int main() {
    scanf("%s", s+1);
    n = strlen(s+1);
    memset(ans, '.', sizeof(ans));
    for (int i = 1; i <= n; i ++) {
        int p = i * 4 - 1;
        ans[3][p] = s[i];
        if (i % 3 == 0) continue;
        for (int j = 0; j < 8; j ++) {
            int x = 3 + dir[j][0], y = p + dir[j][1];
            ans[x][y] = '#';
        }
    }
    for (int i = 3; i <= n; i += 3) {
        int p = i * 4 - 1;
        ans[3][p] = s[i];
        for (int j = 0; j < 8; j ++) {
            int x = 3 + dir[j][0], y = p + dir[j][1];
            ans[x][y] = '*';
        }
    }
    for (int i = 1; i <= 5; i ++) {
        for (int j = 1; j <= 4*n+1; j ++)
            putchar(ans[i][j]);
        putchar('
');
    }
    return 0;
}
原文地址:https://www.cnblogs.com/quanjun/p/14380611.html