#include #define filein(str) freopen(str, "r", stdin) #define fileout(str) freopen(str, "w", stdout); using namespace std; static const int SIZE = 305; time_t TL = 4800; time_t curTime; bool timeUp() { return (clock() - curTime >= TL); } int answer = 0; string ans = ""; struct RGB { int r, g, b; RGB(int r = 255, int g = 255, int b = 255) : r(r), g(g), b(b) {} const bool operator <(const RGB& a) const { if(this->r == a.r and this->b == a.b) return this->g < a.g; if(this->r == a.r) return this->b < a.b; return this->r < a.r; } const bool operator ==(const RGB& a) const { return (this->r == a.r and this->g == a.g and this->b == a.b); } }; template string to_string(T pNumber) { ostringstream oOStrStream; oOStrStream << pNumber; return oOStrStream.str(); } int n, k; RGB activeColor(255, 255, 255); vector colors; map mv; RGB palet[1 << 4]; RGB target[305][305]; RGB canvas[305][305]; int getDif(int r, int c, RGB aColor) { int ret = 0; for(int i = r; i < r + 3; i++) { for(int j = c; j < c + 3; j++) { ret += sqrt((aColor.r - target[i][j].r) * (aColor.r - target[i][j].r) + (aColor.g - target[i][j].g) * (aColor.g - target[i][j].g) + (aColor.b - target[i][j].b) * (aColor.b - target[i][j].b)); } } return ret; } RGB mix(RGB a, RGB b) { a.r = (a.r + b.r) / 2; a.g = (a.g + b.g) / 2; a.b = (a.b + b.b) / 2; return a; } void paintRows(int r, int c) { int minDif = getDif(r, c, activeColor); string out = ""; bool change = false; for(int i = 0; i < colors.size(); i++) { if(getDif(r, c, mix(activeColor, colors[i])) < minDif) { minDif = getDif(r, c, mix(activeColor, colors[i])); activeColor = mix(activeColor, colors[i]); out = mv[colors[i]]; change = true; } if(timeUp()) return; } int diff = 0; for(int i = r; i < r + 3; i++) { for(int j = c; j < c + 3; j++) diff += sqrt((canvas[i][j].r - target[i][j].r) * (canvas[i][j].r - target[i][j].r) + (canvas[i][j].g - target[i][j].g) * (canvas[i][j].g - target[i][j].g) + (canvas[i][j].b - target[i][j].b) * (canvas[i][j].b - target[i][j].b)); } if(diff < minDif) return; answer++; if(change) answer++; ans += out; ans += "3 "; ans += to_string(r); ans += " "; ans += to_string(c); ans += " "; ans += to_string(r + 2); ans += " "; ans += to_string(c + 2); ans += "\n"; string a = to_string(r); string b = to_string(r); if(!mv.count(activeColor))colors.push_back(activeColor); mv[activeColor] = ""; mv[activeColor] += "2 "; mv[activeColor] += a; mv[activeColor] += " "; mv[activeColor] += b; mv[activeColor] += "\n"; } void paintCanvas(int r, int c) { paintRows(r, c); } void solve() { for(int i = 0; i < n; i += 3) { for(int j = 0; j < n; j += 3) { paintCanvas(i, j); if(timeUp()) return; } } } void input() { cin >> n >> k; for(int i = 0; i < k; i++) { cin >> palet[i].r >> palet[i].g >> palet[i].b; colors.push_back(palet[i]); string a = to_string(i); mv[palet[i]] = ""; mv[palet[i]] += "1 "; mv[palet[i]] += a; mv[palet[i]] += "\n"; } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) cin >> target[i][j].r; } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) cin >> target[i][j].g; } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) cin >> target[i][j].b; } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); curTime = clock(); filein("drawing.in"); fileout("drawing.out"); input(); solve(); cout << answer << "\n" << ans << endl; return 0; }