【CSP-201909】字符画

大模拟,到处都是坑我快哭出声了。

  1. 用空格来充当像素点;
  2. 如果下一个像素的颜色与当前颜色不同,则需要变换背景色;
  3. 如果要变换的色与默认色相同则用\033[0m指令,否则用\033[48;2;r;g;bm指令;
  4. 每次换行前都要把背景色复原;
  5. 计算平均颜色时要用unsigned long long防止溢出;
  6. 输入和处理时一定要分清楚行和列的关系。(跪在这里了)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <array>
#include <iomanip>
using namespace std;

int xtoi(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
else
throw;
}

struct color
{
int r, g, b;
color(int rr, int gg, int bb)
{
r = rr;
g = gg;
b = bb;
}
color(const string &s)
{
if (s.size() < 2 || s[0] != '#')
throw;
if (s.size() == 2)
{
r = xtoi(s[1]);
r = r * 16 + r;
g = b = r;
}
else if (s.size() == 4)
{
r = xtoi(s[1]);
r = r * 16 + r;
g = xtoi(s[2]);
g = g * 16 + g;
b = xtoi(s[3]);
b = b * 16 + b;
}
else if (s.size() == 7)
{
r = xtoi(s[1]) * 16 + xtoi(s[2]);
g = xtoi(s[3]) * 16 + xtoi(s[4]);
b = xtoi(s[5]) * 16 + xtoi(s[6]);
}
else
throw;
}
color() : color(0, 0, 0) {}
};

bool operator==(const color &a, const color &b)
{
return a.r == b.r && a.g == b.g && a.b == b.b;
}
bool operator!=(const color &a, const color &b)
{
return !(a == b);
}

const color default_bg;

int m, n, p, q;
array<array<color, 1925>, 1085> c;

color average(int x, int y)
{
unsigned long long r = 0, g = 0, b = 0;
for (int i = x; i < x + q; i++)
{
for (int j = y; j < y + p; j++)
{
r += c[i][j].r;
g += c[i][j].g;
b += c[i][j].b;
}
}
unsigned long long size = p * q;
return color(r / size, g / size, b / size);
}
void commit(char c)
{
cout << "\\x" << hex << uppercase << setw(2) << setfill('0') << (int)c;
}
void commit(const string &s)
{
for (char c : s)
commit(c);
}
void change_background(const color &c)
{
if (c == default_bg)
commit("\033[0m");
else
{
commit("\033[48;2;");
commit(to_string(c.r));
commit(';');
commit(to_string(c.g));
commit(';');
commit(to_string(c.b));
commit('m');
}
}

int main()
{
ios::sync_with_stdio(false);
color bg = default_bg;
cin >> m >> n >> p >> q;

for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
string s;
cin >> s;
c[i][j] = color(s);
}
}

for (int i = 0; i < n; i += q)
{
for (int j = 0; j < m; j += p)
{
color nxt = average(i, j);
if (nxt != bg)
{
change_background(nxt);
bg = nxt;
}
commit(' ');
}
if (bg != default_bg)
{
change_background(default_bg);
bg = default_bg;
}
commit('\n');
}
return 0;
}
Author: ssttkkl
Link: https://ssttkkl.github.io/CSP/2019/12/%E3%80%90CSP-201909%E3%80%91%E5%AD%97%E7%AC%A6%E7%94%BB/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.