#include <bits/stdc++.h> #define pb push_back #define inf 1999999999 #define turbo ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; const int N = 2015; int tab[N][N]; int k[N][27]; int r[N][27]; signed main() { turbo; int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { string s; cin >> s; for (int j = 1; j <= m; j++) { tab[i][j] = s[j - 1] - ('A' - 1); r[i][tab[i][j]]++; k[j][tab[i][j]]++; } } set <int> srows; set <int> scols; vector<pair<int, int>> res; // Add initial rows and cols. for (int i = 1; i <= n; i++) { int distinct = 0; for (int j = 1; j <= 26; j++) { if (r[i][j] > 0) { distinct++; } } if (distinct == 1) { srows.insert(i); } } for (int j = 1; j <= m; j++) { int distinct = 0; for (int i = 1; i <= 26; i++) { if (k[j][i] > 0) { distinct++; } } if (distinct == 1) { scols.insert(j); } } int leftrows = n; int leftcols = m; while (scols.size() > 0 || srows.size() > 0) { int x; if (leftrows < leftcols) { if (srows.size() > 0) { x = *srows.begin(); srows.erase(srows.begin()); leftrows--; } else { x = *scols.begin() + n; scols.erase(scols.begin()); leftcols--; } } else { if (scols.size() > 0) { x = *scols.begin() + n; scols.erase(scols.begin()); leftcols--; } else { x = *srows.begin(); srows.erase(srows.begin()); leftrows--; } } if (x <= n) { int i = x; int color = 0; for (int j = 1; j <= m; j++) { if (tab[i][j] != 0) { color = tab[i][j]; r[i][color]--; k[j][color]--; tab[i][j] = 0; int distinct = 0; int sum = 0; for (int l = 1; l <= 26; l++) { if (k[j][l] > 0) { sum += k[j][l]; distinct++; } } if (distinct == 1) { scols.insert(j); } } } if (color != 0) { res.pb({x, color}); } } else { int j = x - n; int color = 0; for (int i = 1; i <= n; i++) { if (tab[i][j] != 0) { color = tab[i][j]; r[i][color]--; k[j][color]--; tab[i][j] = 0; int distinct = 0; int sum = 0; for (int l = 1; l <= 26; l++) { if (r[i][l] > 0) { sum += r[i][l]; distinct++; } } if (distinct == 1) { srows.insert(i); } } } if (color != 0) { res.pb({x, color}); } } } reverse(res.begin(), res.end()); if (res.size() > n + m) { while (true) {} } cout << res.size() << endl; for (auto p : res) { if (p.first <= n) { cout << 'R' << " " << p.first << " " << (char)('A' + p.second - 1) << endl; } else { cout << 'K' << " " << p.first - n << " " << (char)('A' + p.second - 1) << 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 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 140 141 142 143 144 145 146 147 148 149 150 | #include <bits/stdc++.h> #define pb push_back #define inf 1999999999 #define turbo ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; const int N = 2015; int tab[N][N]; int k[N][27]; int r[N][27]; signed main() { turbo; int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { string s; cin >> s; for (int j = 1; j <= m; j++) { tab[i][j] = s[j - 1] - ('A' - 1); r[i][tab[i][j]]++; k[j][tab[i][j]]++; } } set <int> srows; set <int> scols; vector<pair<int, int>> res; // Add initial rows and cols. for (int i = 1; i <= n; i++) { int distinct = 0; for (int j = 1; j <= 26; j++) { if (r[i][j] > 0) { distinct++; } } if (distinct == 1) { srows.insert(i); } } for (int j = 1; j <= m; j++) { int distinct = 0; for (int i = 1; i <= 26; i++) { if (k[j][i] > 0) { distinct++; } } if (distinct == 1) { scols.insert(j); } } int leftrows = n; int leftcols = m; while (scols.size() > 0 || srows.size() > 0) { int x; if (leftrows < leftcols) { if (srows.size() > 0) { x = *srows.begin(); srows.erase(srows.begin()); leftrows--; } else { x = *scols.begin() + n; scols.erase(scols.begin()); leftcols--; } } else { if (scols.size() > 0) { x = *scols.begin() + n; scols.erase(scols.begin()); leftcols--; } else { x = *srows.begin(); srows.erase(srows.begin()); leftrows--; } } if (x <= n) { int i = x; int color = 0; for (int j = 1; j <= m; j++) { if (tab[i][j] != 0) { color = tab[i][j]; r[i][color]--; k[j][color]--; tab[i][j] = 0; int distinct = 0; int sum = 0; for (int l = 1; l <= 26; l++) { if (k[j][l] > 0) { sum += k[j][l]; distinct++; } } if (distinct == 1) { scols.insert(j); } } } if (color != 0) { res.pb({x, color}); } } else { int j = x - n; int color = 0; for (int i = 1; i <= n; i++) { if (tab[i][j] != 0) { color = tab[i][j]; r[i][color]--; k[j][color]--; tab[i][j] = 0; int distinct = 0; int sum = 0; for (int l = 1; l <= 26; l++) { if (r[i][l] > 0) { sum += r[i][l]; distinct++; } } if (distinct == 1) { srows.insert(i); } } } if (color != 0) { res.pb({x, color}); } } } reverse(res.begin(), res.end()); if (res.size() > n + m) { while (true) {} } cout << res.size() << endl; for (auto p : res) { if (p.first <= n) { cout << 'R' << " " << p.first << " " << (char)('A' + p.second - 1) << endl; } else { cout << 'K' << " " << p.first - n << " " << (char)('A' + p.second - 1) << endl; } } return 0; } |