#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < n; i++) #define f first #define s second #define pb push_back #define all(s) s.begin(), s.end() #define sz(s) (int)s.size() using vi = vector<int>; using ll = long long; using ull = unsigned long long; using pll = pair<ll, ll>; using pull = pair<ull, ull>; using vll = vector<ll>; using ii = pair<int, int>; void solve() { int n, m; cin >> n >> m; vector<string> mapa(n); FOR (i, n) { cin >> mapa[i]; } vector<map<char, int>> cols(m); vector<map<char, int>> rows(n); FOR (i, n) { FOR (j, m) { rows[i][mapa[i][j]]++; cols[j][mapa[i][j]]++; } } auto odejmij = [&](map<char, int> &mp, char c) { mp[c]--; if (mp[c] == 0) { mp.erase(c); } }; vector<pair<int, char>> ans; FOR (i, n + m) { // maybe rows bool found = false; FOR (row, n) { if (sz(rows[row]) == 1) { ans.pb({-(row + 1), rows[row].begin()->f}); found = true; FOR (col, m) { if (mapa[row][col] != '#') { odejmij(rows[row], mapa[row][col]); odejmij(cols[col], mapa[row][col]); mapa[row][col] = '#'; } } break; } } if (found) { continue; } // well then some column FOR (col, m) { if (sz(cols[col]) == 1) { ans.pb({col + 1, cols[col].begin()->f}); found = true; FOR (row, n) { if (mapa[row][col] != '#') { odejmij(rows[row], mapa[row][col]); odejmij(cols[col], mapa[row][col]); mapa[row][col] = '#'; } } break; } } if (not found) { break; } } cout << sz(ans) << '\n'; reverse(all(ans)); for (auto & [nr, col] : ans) { cout << (nr < 0 ? "R " : "K ") << ' ' << abs(nr) << ' ' << col << endl; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int tests = 1; // cin >> tests; for (int test = 1; test <= tests; test++) { solve(); } 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 | #pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < n; i++) #define f first #define s second #define pb push_back #define all(s) s.begin(), s.end() #define sz(s) (int)s.size() using vi = vector<int>; using ll = long long; using ull = unsigned long long; using pll = pair<ll, ll>; using pull = pair<ull, ull>; using vll = vector<ll>; using ii = pair<int, int>; void solve() { int n, m; cin >> n >> m; vector<string> mapa(n); FOR (i, n) { cin >> mapa[i]; } vector<map<char, int>> cols(m); vector<map<char, int>> rows(n); FOR (i, n) { FOR (j, m) { rows[i][mapa[i][j]]++; cols[j][mapa[i][j]]++; } } auto odejmij = [&](map<char, int> &mp, char c) { mp[c]--; if (mp[c] == 0) { mp.erase(c); } }; vector<pair<int, char>> ans; FOR (i, n + m) { // maybe rows bool found = false; FOR (row, n) { if (sz(rows[row]) == 1) { ans.pb({-(row + 1), rows[row].begin()->f}); found = true; FOR (col, m) { if (mapa[row][col] != '#') { odejmij(rows[row], mapa[row][col]); odejmij(cols[col], mapa[row][col]); mapa[row][col] = '#'; } } break; } } if (found) { continue; } // well then some column FOR (col, m) { if (sz(cols[col]) == 1) { ans.pb({col + 1, cols[col].begin()->f}); found = true; FOR (row, n) { if (mapa[row][col] != '#') { odejmij(rows[row], mapa[row][col]); odejmij(cols[col], mapa[row][col]); mapa[row][col] = '#'; } } break; } } if (not found) { break; } } cout << sz(ans) << '\n'; reverse(all(ans)); for (auto & [nr, col] : ans) { cout << (nr < 0 ? "R " : "K ") << ' ' << abs(nr) << ' ' << col << endl; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int tests = 1; // cin >> tests; for (int test = 1; test <= tests; test++) { solve(); } return 0; } |