#include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back // #define st first #define nd second #define fi first #define se second #define vt vector #define FOR(a, b, c) for(int a=b; a<c; ++a) #define all(a) (a).begin(),(a).end() #define sz(a) (int)(a).size() typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef pii PII; typedef pll PLL; constexpr ll nax = 2969, INF = 1e9+2137; constexpr ld eps = 1e-9; mt19937_64 rng(6969); // mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); inline ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); } int n, m; vector<string> A; vector<pair<char, pair<int, char>>> ans; int row[nax][30], col[nax][30]; bool vis_col[nax], vis_row[nax]; void do_row(int i) { for(int j = 0; j < m; j++) { if(A[i][j] == '?') continue; col[j][A[i][j] - 'A']--; row[i][A[i][j] - 'A']--; col[j][26]++; row[i][26]++; A[i][j] = '?'; } } void do_col(int i) { for(int j = 0; j < n; j++) { if(A[j][i] == '?') continue; col[i][A[j][i] - 'A']--; row[j][A[j][i] - 'A']--; col[i][26]++; row[j][26]++; A[j][i] = '?'; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; A.resize(n); for(auto &e: A) { cin >> e; } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { row[i][A[i][j] - 'A']++; col[j][A[i][j] - 'A']++; } } queue<pair<char, pair<int, char>>> q; for(int i = 0; i < n; i++) { for(int letter = 0; letter < 26; letter++) { if(row[i][letter] > 0 && row[i][letter] + row[i][26] == m) { q.push(mp('R', mp(i, (char)(letter + 'A')))); vis_row[i] = true; do_row(i); } } } for(int i = 0; i < m; i++) { for(int letter = 0; letter < 26; letter++) { if(col[i][letter] > 0 && col[i][letter] + col[i][26] == n) { q.push(mp('K', mp(i, (char)(letter + 'A')))); vis_col[i] = true; do_col(i); } } } while(!q.empty()) { pair<char, pair<int, char>> cur = q.front(); q.pop(); ans.pb(cur); int nr = cur.se.fi; char letter = cur.se.se; char which = cur.fi; if(which == 'R') { // vis_row[nr] = true; for(int i = 0; i < m; i++) { if(vis_col[i]) continue; // col[i][26]++; for(int j = 0; j < 26; j++) { if(col[i][j] > 0 && col[i][j] + col[i][26] == n) { q.push(mp('K', mp(i, (char)(j + 'A')))); vis_col[i] = true; do_col(i); break; } } } } else { for(int i = 0; i < n; i++) { if(vis_row[i]) continue; // row[i][26]++; for(int j = 0; j < 26; j++) { if(row[i][j] > 0 && row[i][j] + row[i][26] == m) { q.push(mp('R', mp(i, (char)(j + 'A')))); vis_row[i] = true; do_row(i); break; } } } } } cout << ans.size() << "\n"; reverse(all(ans)); for(auto e: ans) { cout << e.fi << " " << e.se.fi + 1 << " " << e.se.se << "\n"; } 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 | #include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back // #define st first #define nd second #define fi first #define se second #define vt vector #define FOR(a, b, c) for(int a=b; a<c; ++a) #define all(a) (a).begin(),(a).end() #define sz(a) (int)(a).size() typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef pii PII; typedef pll PLL; constexpr ll nax = 2969, INF = 1e9+2137; constexpr ld eps = 1e-9; mt19937_64 rng(6969); // mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); inline ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); } int n, m; vector<string> A; vector<pair<char, pair<int, char>>> ans; int row[nax][30], col[nax][30]; bool vis_col[nax], vis_row[nax]; void do_row(int i) { for(int j = 0; j < m; j++) { if(A[i][j] == '?') continue; col[j][A[i][j] - 'A']--; row[i][A[i][j] - 'A']--; col[j][26]++; row[i][26]++; A[i][j] = '?'; } } void do_col(int i) { for(int j = 0; j < n; j++) { if(A[j][i] == '?') continue; col[i][A[j][i] - 'A']--; row[j][A[j][i] - 'A']--; col[i][26]++; row[j][26]++; A[j][i] = '?'; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; A.resize(n); for(auto &e: A) { cin >> e; } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { row[i][A[i][j] - 'A']++; col[j][A[i][j] - 'A']++; } } queue<pair<char, pair<int, char>>> q; for(int i = 0; i < n; i++) { for(int letter = 0; letter < 26; letter++) { if(row[i][letter] > 0 && row[i][letter] + row[i][26] == m) { q.push(mp('R', mp(i, (char)(letter + 'A')))); vis_row[i] = true; do_row(i); } } } for(int i = 0; i < m; i++) { for(int letter = 0; letter < 26; letter++) { if(col[i][letter] > 0 && col[i][letter] + col[i][26] == n) { q.push(mp('K', mp(i, (char)(letter + 'A')))); vis_col[i] = true; do_col(i); } } } while(!q.empty()) { pair<char, pair<int, char>> cur = q.front(); q.pop(); ans.pb(cur); int nr = cur.se.fi; char letter = cur.se.se; char which = cur.fi; if(which == 'R') { // vis_row[nr] = true; for(int i = 0; i < m; i++) { if(vis_col[i]) continue; // col[i][26]++; for(int j = 0; j < 26; j++) { if(col[i][j] > 0 && col[i][j] + col[i][26] == n) { q.push(mp('K', mp(i, (char)(j + 'A')))); vis_col[i] = true; do_col(i); break; } } } } else { for(int i = 0; i < n; i++) { if(vis_row[i]) continue; // row[i][26]++; for(int j = 0; j < 26; j++) { if(row[i][j] > 0 && row[i][j] + row[i][26] == m) { q.push(mp('R', mp(i, (char)(j + 'A')))); vis_row[i] = true; do_row(i); break; } } } } } cout << ans.size() << "\n"; reverse(all(ans)); for(auto e: ans) { cout << e.fi << " " << e.se.fi + 1 << " " << e.se.se << "\n"; } return 0; } |