#include<cstdio> #include<vector> #include<list> #define N 2002 #define C 'Z' - 'A' + 1 using namespace std; char znaki[N][N]; int wiersze[N][C]; int kolumny[N][C]; int pomniejszoneWiersze[C]; int pomniejszoneKolumny[C]; class Move { public: bool row; int number; char color; Move(bool row, int number, char color) { this->row = row; this->number = number; this->color = color; } void print() { printf("%c %d %c\n", this->row ? 'R' : 'K', this->number + 1, this->color + 'A'); } }; int main() { vector<Move> result; int n, m; scanf("%d %d\n", &n, &m); list<int> dostepneWiersze; list<int> dostepneKolumny; for (int i = 0; i < n; i++) { dostepneWiersze.push_back(i); } for (int i = 0; i < m; i++) { dostepneKolumny.push_back(i); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { znaki[i][j] = getc(stdin); wiersze[i][znaki[i][j] - 'A']++; kolumny[j][znaki[i][j] - 'A']++; } getc(stdin); } //for (int i = 0; i < n; i++) { // printf("%d %d\n", kolumny[i]['A' - 'A'], kolumny[i]['P' - 'A']); //} //for (int i = 0; i < n; i++) { // for (int j = 0; j < m; j++) { // printf("%c", znaki[i][j]); // } // printf("\n"); //} while (true) { bool cont = false; for (auto it = dostepneWiersze.begin(); it != dostepneWiersze.end();) { int count = 0; char x; for (char c = 'A' - 'A'; c < C; c++) { if (wiersze[*it][c] - pomniejszoneWiersze[c] > 0) { count++; x = c; } } if (count == 1) { result.push_back(Move(true, *it, x)); it = dostepneWiersze.erase(it); pomniejszoneKolumny[x]++; cont = true; break; } else { ++it; } } if (cont) { continue; } for (auto it = dostepneKolumny.begin(); it != dostepneKolumny.end();) { int count = 0; char x; for (char c = 'A' - 'A'; c < C; c++) { if (kolumny[*it][c] - pomniejszoneKolumny[c] > 0) { count++; x = c; } } if (count == 1) { result.push_back(Move(false, *it, x)); it = dostepneKolumny.erase(it); pomniejszoneWiersze[x]++; cont = true; break; } else { ++it; } } if (cont) { continue; } break; } printf("%lld\n", result.size()); for (auto it = result.rbegin(); it != result.rend(); ++it) { it->print(); } 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 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 | #include<cstdio> #include<vector> #include<list> #define N 2002 #define C 'Z' - 'A' + 1 using namespace std; char znaki[N][N]; int wiersze[N][C]; int kolumny[N][C]; int pomniejszoneWiersze[C]; int pomniejszoneKolumny[C]; class Move { public: bool row; int number; char color; Move(bool row, int number, char color) { this->row = row; this->number = number; this->color = color; } void print() { printf("%c %d %c\n", this->row ? 'R' : 'K', this->number + 1, this->color + 'A'); } }; int main() { vector<Move> result; int n, m; scanf("%d %d\n", &n, &m); list<int> dostepneWiersze; list<int> dostepneKolumny; for (int i = 0; i < n; i++) { dostepneWiersze.push_back(i); } for (int i = 0; i < m; i++) { dostepneKolumny.push_back(i); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { znaki[i][j] = getc(stdin); wiersze[i][znaki[i][j] - 'A']++; kolumny[j][znaki[i][j] - 'A']++; } getc(stdin); } //for (int i = 0; i < n; i++) { // printf("%d %d\n", kolumny[i]['A' - 'A'], kolumny[i]['P' - 'A']); //} //for (int i = 0; i < n; i++) { // for (int j = 0; j < m; j++) { // printf("%c", znaki[i][j]); // } // printf("\n"); //} while (true) { bool cont = false; for (auto it = dostepneWiersze.begin(); it != dostepneWiersze.end();) { int count = 0; char x; for (char c = 'A' - 'A'; c < C; c++) { if (wiersze[*it][c] - pomniejszoneWiersze[c] > 0) { count++; x = c; } } if (count == 1) { result.push_back(Move(true, *it, x)); it = dostepneWiersze.erase(it); pomniejszoneKolumny[x]++; cont = true; break; } else { ++it; } } if (cont) { continue; } for (auto it = dostepneKolumny.begin(); it != dostepneKolumny.end();) { int count = 0; char x; for (char c = 'A' - 'A'; c < C; c++) { if (kolumny[*it][c] - pomniejszoneKolumny[c] > 0) { count++; x = c; } } if (count == 1) { result.push_back(Move(false, *it, x)); it = dostepneKolumny.erase(it); pomniejszoneWiersze[x]++; cont = true; break; } else { ++it; } } if (cont) { continue; } break; } printf("%lld\n", result.size()); for (auto it = result.rbegin(); it != result.rend(); ++it) { it->print(); } return 0; } |