#include <iostream> const int MAX = 2010; const int MAXZ = 26; std::string M[MAX]; std::string Q[2*MAX]; int qn = 0; int RC[MAX][MAXZ]; int CC[MAX][MAXZ]; int IC[MAX]; int IR[MAX]; int ic; int ir; void push_command(bool row, int i, char c) { Q[qn] = (row ? "R " : "K ") + std::to_string(i) + " " + c; ++qn; } int w,h; char one_color_col(int j) { char c = 0; for (int z=0;z<26;++z) { if (c == 0 && CC[j][z]) c = z+'A'; else if (c != 0 && CC[j][z]) return 0; } return c; } char one_color_row(int i) { char c = 0; for (int z=0;z<26;++z) { if (c == 0 && RC[i][z]) c = z+'A'; else if (c != 0 && RC[i][z]) return 0; } return c; } int main() { std::ios_base::sync_with_stdio(0); std::cin >> h >> w; for (int i=0;i<h;++i) std::cin >> M[i]; for (int i=0;i<h;++i) IR[i]=i; for (int i=0;i<w;++i) IC[i]=i; for (int i=0;i<h;++i) for (int j=0;j<w;++j) { ++CC[j][M[i][j]-'A']; ++RC[i][M[i][j]-'A']; } int cn = 0; int rn = 0; ic = w; ir = h; while (cn < w && rn < h) { bool update = false; for (int ii=0;ii<ic;++ii) { int j = IC[ii]; if (char c = one_color_col(j)) { for (int ii=0;ii<ir;++ii) { int i = IR[ii]; --CC[j][M[i][j]-'A']; --RC[i][M[i][j]-'A']; } push_command(false, j+1, c); std::swap(IC[ii], IC[ic-1]); --ic; --ii; ++cn; update = true; } } for (int ii=0;ii<ir;++ii) { int i = IR[ii]; if (char c = one_color_row(i)) { for (int ii=0;ii<ic;++ii) { int j = IC[ii]; --CC[j][M[i][j]-'A']; --RC[i][M[i][j]-'A']; } push_command(true, i+1, c); std::swap(IR[ii], IR[ir-1]); --ir; --ii; ++rn; update = true; } } if (!update) { std::clog << " :: " << "no update, something went wrong" << std::endl; break; } } std::cout << qn << std::endl; for (int i=qn-1;i>=0;--i) std::cout << Q[i] << std::endl; return 0; }
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 | #include <iostream> const int MAX = 2010; const int MAXZ = 26; std::string M[MAX]; std::string Q[2*MAX]; int qn = 0; int RC[MAX][MAXZ]; int CC[MAX][MAXZ]; int IC[MAX]; int IR[MAX]; int ic; int ir; void push_command(bool row, int i, char c) { Q[qn] = (row ? "R " : "K ") + std::to_string(i) + " " + c; ++qn; } int w,h; char one_color_col(int j) { char c = 0; for (int z=0;z<26;++z) { if (c == 0 && CC[j][z]) c = z+'A'; else if (c != 0 && CC[j][z]) return 0; } return c; } char one_color_row(int i) { char c = 0; for (int z=0;z<26;++z) { if (c == 0 && RC[i][z]) c = z+'A'; else if (c != 0 && RC[i][z]) return 0; } return c; } int main() { std::ios_base::sync_with_stdio(0); std::cin >> h >> w; for (int i=0;i<h;++i) std::cin >> M[i]; for (int i=0;i<h;++i) IR[i]=i; for (int i=0;i<w;++i) IC[i]=i; for (int i=0;i<h;++i) for (int j=0;j<w;++j) { ++CC[j][M[i][j]-'A']; ++RC[i][M[i][j]-'A']; } int cn = 0; int rn = 0; ic = w; ir = h; while (cn < w && rn < h) { bool update = false; for (int ii=0;ii<ic;++ii) { int j = IC[ii]; if (char c = one_color_col(j)) { for (int ii=0;ii<ir;++ii) { int i = IR[ii]; --CC[j][M[i][j]-'A']; --RC[i][M[i][j]-'A']; } push_command(false, j+1, c); std::swap(IC[ii], IC[ic-1]); --ic; --ii; ++cn; update = true; } } for (int ii=0;ii<ir;++ii) { int i = IR[ii]; if (char c = one_color_row(i)) { for (int ii=0;ii<ic;++ii) { int j = IC[ii]; --CC[j][M[i][j]-'A']; --RC[i][M[i][j]-'A']; } push_command(true, i+1, c); std::swap(IR[ii], IR[ir-1]); --ir; --ii; ++rn; update = true; } } if (!update) { std::clog << " :: " << "no update, something went wrong" << std::endl; break; } } std::cout << qn << std::endl; for (int i=qn-1;i>=0;--i) std::cout << Q[i] << std::endl; return 0; } |