#include <cstdio> #include <set> using namespace std; char tab[2001][2001]; int sums_row[2001]; int sums_col[2001]; int sums_row_char[26][2001]; int sums_col_char[26][2001]; int usun_elem[2001]; pair<pair<char, char>, int> ret[4001]; set<int> left_rows; set<int> left_cols; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%s", tab[i]); } for (int i = 0; i < n; i++) { sums_row[i] = m; left_rows.insert(i); } for (int i = 0; i < m; i++) { sums_col[i] = n; left_cols.insert(i); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { sums_row_char[tab[i][j] - 'A'][i]++; } } for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { sums_col_char[tab[i][j] - 'A'][j]++; } } int ile_ret = 0; //printf("%d %d\n", left_cols.size(), left_rows.size()); for (int step = 0; step < n + m; step++) { int ile_usun = 0; for (auto rows : left_rows) { if (left_cols.empty()) break; int first_col = *left_cols.begin();; int i = rows; if (sums_row_char[tab[i][first_col] - 'A'][i] == sums_row[i]) { ret[ile_ret] = {{'R', tab[i][first_col]}, i}; ile_ret++; usun_elem[ile_usun] = i; ile_usun++; for (auto it : left_cols) { if (tab[i][it] != '0') { sums_col_char[tab[i][it] - 'A'][it]--; sums_col[it]--; tab[i][it] = '0'; } } } } for (int i = 0; i < ile_usun; i++) left_rows.erase(usun_elem[i]); ile_usun = 0; for (auto cols : left_cols) { if (left_rows.empty()) break; int first_row = *left_rows.begin();; int j = cols; if (sums_col_char[tab[first_row][j] - 'A'][j] == sums_col[j]) { ret[ile_ret] = {{'K',tab[first_row][j]}, j}; ile_ret++; usun_elem[ile_usun] = j; ile_usun++; for (auto it : left_rows) { if (tab[it][j] != '0') { sums_row_char[tab[it][j] - 'A'][it]--; sums_row[it]--; tab[it][j] = '0'; } } } } for (int i = 0; i < ile_usun; i++) left_cols.erase(usun_elem[i]); } //printf("%d %d\n", left_cols.size(), left_rows.size()); printf("%d\n", ile_ret); for (int i = ile_ret - 1; i >= 0; i--) { printf("%c %d %c\n", ret[i].first.first, ret[i].second + 1, ret[i].first.second); } 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 | #include <cstdio> #include <set> using namespace std; char tab[2001][2001]; int sums_row[2001]; int sums_col[2001]; int sums_row_char[26][2001]; int sums_col_char[26][2001]; int usun_elem[2001]; pair<pair<char, char>, int> ret[4001]; set<int> left_rows; set<int> left_cols; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%s", tab[i]); } for (int i = 0; i < n; i++) { sums_row[i] = m; left_rows.insert(i); } for (int i = 0; i < m; i++) { sums_col[i] = n; left_cols.insert(i); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { sums_row_char[tab[i][j] - 'A'][i]++; } } for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { sums_col_char[tab[i][j] - 'A'][j]++; } } int ile_ret = 0; //printf("%d %d\n", left_cols.size(), left_rows.size()); for (int step = 0; step < n + m; step++) { int ile_usun = 0; for (auto rows : left_rows) { if (left_cols.empty()) break; int first_col = *left_cols.begin();; int i = rows; if (sums_row_char[tab[i][first_col] - 'A'][i] == sums_row[i]) { ret[ile_ret] = {{'R', tab[i][first_col]}, i}; ile_ret++; usun_elem[ile_usun] = i; ile_usun++; for (auto it : left_cols) { if (tab[i][it] != '0') { sums_col_char[tab[i][it] - 'A'][it]--; sums_col[it]--; tab[i][it] = '0'; } } } } for (int i = 0; i < ile_usun; i++) left_rows.erase(usun_elem[i]); ile_usun = 0; for (auto cols : left_cols) { if (left_rows.empty()) break; int first_row = *left_rows.begin();; int j = cols; if (sums_col_char[tab[first_row][j] - 'A'][j] == sums_col[j]) { ret[ile_ret] = {{'K',tab[first_row][j]}, j}; ile_ret++; usun_elem[ile_usun] = j; ile_usun++; for (auto it : left_rows) { if (tab[it][j] != '0') { sums_row_char[tab[it][j] - 'A'][it]--; sums_row[it]--; tab[it][j] = '0'; } } } } for (int i = 0; i < ile_usun; i++) left_cols.erase(usun_elem[i]); } //printf("%d %d\n", left_cols.size(), left_rows.size()); printf("%d\n", ile_ret); for (int i = ile_ret - 1; i >= 0; i--) { printf("%c %d %c\n", ret[i].first.first, ret[i].second + 1, ret[i].first.second); } return 0; } |