#include <iostream> #include <vector> #include <queue> #include <tuple> #include <stack> using namespace std; const int M = 2005, A = 26; int n, m, rem[2][M][A + 1], s[2]; bool don[2][M]; char board[M][M]; int32_t main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> board[i][j]; rem[0][i][board[i][j] - 'A']++; rem[1][j][board[i][j] - 'A']++; } } s[0] = n; s[1] = m; queue <tuple <bool, int, int>> q; stack <tuple<char, int, char>> r; for (int k = 0; k < 2; k++) for (int i = 0; i < s[k]; i++) for (int j = 0; j < A; j++) if (rem[k][i][j] == s[!k]) q.push({ k, i, j }); while (!q.empty()) { auto el = q.front(); int i = get<1>(el), j = get<2>(el); bool k = get<0>(el); q.pop(); if (don[k][i]) continue; don[k][i] = 1; r.push({ (k ? 'K' : 'R'), i + 1, 'A' + j }); for (int l = 0; l < s[!k]; l++) { if (!don[!k][l]) { --rem[!k][l][j]; rem[!k][l][A]++; for (int a = 0; a < A; a++) if (rem[!k][l][a] > 0 && rem[!k][l][a] + rem[!k][l][A] == s[k]) q.push({ !k, l, a }); } } } cout << r.size() << '\n'; while (!r.empty()) { auto el = r.top(); r.pop(); cout << get<0>(el) << ' ' << get<1>(el) << ' ' << get<2>(el) << '\n'; } }
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 | #include <iostream> #include <vector> #include <queue> #include <tuple> #include <stack> using namespace std; const int M = 2005, A = 26; int n, m, rem[2][M][A + 1], s[2]; bool don[2][M]; char board[M][M]; int32_t main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> board[i][j]; rem[0][i][board[i][j] - 'A']++; rem[1][j][board[i][j] - 'A']++; } } s[0] = n; s[1] = m; queue <tuple <bool, int, int>> q; stack <tuple<char, int, char>> r; for (int k = 0; k < 2; k++) for (int i = 0; i < s[k]; i++) for (int j = 0; j < A; j++) if (rem[k][i][j] == s[!k]) q.push({ k, i, j }); while (!q.empty()) { auto el = q.front(); int i = get<1>(el), j = get<2>(el); bool k = get<0>(el); q.pop(); if (don[k][i]) continue; don[k][i] = 1; r.push({ (k ? 'K' : 'R'), i + 1, 'A' + j }); for (int l = 0; l < s[!k]; l++) { if (!don[!k][l]) { --rem[!k][l][j]; rem[!k][l][A]++; for (int a = 0; a < A; a++) if (rem[!k][l][a] > 0 && rem[!k][l][a] + rem[!k][l][A] == s[k]) q.push({ !k, l, a }); } } } cout << r.size() << '\n'; while (!r.empty()) { auto el = r.top(); r.pop(); cout << get<0>(el) << ' ' << get<1>(el) << ' ' << get<2>(el) << '\n'; } } |