题好难读,差评
实际上只需要处理背景就可以,题意干扰严重
大小写没注意,我好菜
n、m搞反了,我巨菜
好菜啊,啥时候才能一发a啊
#include <bits/stdc++.h>
using namespace std;
const int backColor = 0;
string ret; string res;
vector<vector<string>> iniColor;
int curBacR = 0, curBacG = 0, curBacB = 0;
int n,m,p,q;
void init() {
curBacR = 0, curBacG = 0, curBacB = 0;
}
string solveColor(const string& colors) {
string completeColors;
if (colors.size() == 6) {
return colors;
} else if (colors.size() == 3) {
for (int i = 0; i < 3; i++) {
completeColors.push_back(colors[i]);
completeColors.push_back(colors[i]);
}
} else if (colors.size() == 1) {
for (int i = 0; i < 6; i++) {
completeColors.push_back(colors[0]);
}
}
return completeColors;
}
vector<int> Parse(string& ret,const string& fina) {
string R = fina.substr(0,2);
string G = fina.substr(2,2);
string B = fina.substr(4,2);
auto toInt = [&](string& r) {
int ret = 0;
for (auto& c: r) {
if (isdigit(c)) {
ret = ret * 16 + c - '0';
} else {
if (c >= 'a' && c <= 'z') {
c = c - 'a' + 'A';
}
ret = ret * 16 + c - 'A' + 10;
}
}
return ret;
};
int r = toInt(R),g = toInt(G),b = toInt(B);
return vector<int>{r,g,b};
}
void printBack(string& ret, vector<int> rgb) {
int r = rgb[0], g = rgb[1], b = rgb[2];
//更改并不会影响显示效果,输出即可
if (r == curBacR && g == curBacG && b == curBacB) {
return;
}
//等于默认值,调用默认的方法
if (r == backColor && g == backColor && b == backColor) {
ret.push_back((char)(27));
ret += "[0m";
curBacR = r, curBacG = g, curBacB = b;
return;
} else {//进行修改
ret.push_back((char)(27));
ret += "[48;2;";
ret += to_string(r);
ret += ";";
ret += to_string(g);
ret += ";";
ret += to_string(b);
ret += "m";
curBacB = b, curBacG = g, curBacR = r;
return;
}
}
void handle(string& ret, vector<int> rgb) {
printBack(ret,rgb);
ret += " ";
}
int main(){
cin >> m >> n >> p >> q;
init();
iniColor.resize(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
string s;
cin >> s;
iniColor[i].push_back(s);
}
}
for (int i = 0; i < n; i += q) {
for (int j = 0; j < m; j += p) {
int r = 0,g = 0,b = 0;
for (int k = i; k < i + q; k++) {
for (int w = j; w < j + p; w++) {
string colors = iniColor[k][w];
colors = colors.substr(1);
string fina = solveColor(colors);
auto tmp = Parse(ret, fina);
r += tmp[0], g += tmp[1], b += tmp[2];
}
}
int div = p * q;
r /= div, g /= div, b /= div;
handle(ret, vector<int>{r,g,b});
}
if (curBacR != backColor|| curBacG != backColor || curBacB != backColor) {
ret.push_back((char)(27));
ret += "[0m";
init();
}
ret += "\n";
}
auto toHex = [](int c) {
char re;
if (c >= 10) {
return c - 10 + 'A';
}
return c + '0';
};
for (auto c : ret) {
res += "\\x";
int hi = c / 16;
int lo = c % 16;
res.push_back(toHex(hi));
res.push_back(toHex(lo));
}
cout << res;
}