/* * Opis: Główny nagłówek */ #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif const int A = 26; struct Game { int n, m; vector<string> mapa; array<vector<int>, A> let_in_row; array<vector<int>, A> let_in_col; vector<int> diff_in_row; vector<int> diff_in_col; vector<pair<int, int> > ready; Game(int _n, int _m, vector<string> &_mapa) { n = _n; m = _m; swap(mapa, _mapa); diff_in_col.resize(m); diff_in_row.resize(n); REP(i, A) { let_in_col[i].resize(m); let_in_row[i].resize(n); } REP(i, n) { REP(j, m) { int ind = mapa[i][j] - 'A'; let_in_row[ind][i]++; if (let_in_row[ind][i] == 1) { diff_in_row[i]++; } let_in_col[ind][j]++; if (let_in_col[ind][j] == 1) { diff_in_col[j]++; } } } REP(i, n) { if (diff_in_row[i] == 1) { ready.push_back({0, i}); } } REP(i, m) { if (diff_in_col[i] == 1) { ready.push_back({1, i}); } } } char remove(int num, bool col) { char val = 'A'; if (col) { REP(i, n) { if (mapa[i][num] == ' ') { continue; } val = mapa[i][num]; int ind = mapa[i][num] - 'A'; mapa[i][num] = ' '; let_in_row[ind][i]--; if (let_in_row[ind][i] == 0) { diff_in_row[i]--; if (diff_in_row[i] == 1) { ready.push_back({0, i}); } } } } else { REP(j, m) { if (mapa[num][j] == ' ') { continue; } val = mapa[num][j]; int ind = mapa[num][j] - 'A'; mapa[num][j] = ' '; let_in_col[ind][j]--; if (let_in_col[ind][j] == 0) { diff_in_col[j]--; if (diff_in_col[j] == 1) { ready.push_back({1, j}); } } } } return val; } bool finished() { return ready.empty(); } }; struct Op { char type; int num; char let; }; int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector<string> mapa(n); REP(i, n) { cin >> mapa[i]; } Game game(n, m, mapa); vector<Op> ops; while (!game.finished()) { pair<int, int> curr = game.ready.back(); game.ready.pop_back(); Op op{curr.first ? 'K' : 'R', curr.second+1, game.remove(curr.second, curr.first)}; ops.push_back(op); } reverse(ops.begin(), ops.end()); cout << ssize(ops) << "\n"; for (auto op : ops) { cout << op.type << " " << op.num << " " << op.let << "\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 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 | /* * Opis: Główny nagłówek */ #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif const int A = 26; struct Game { int n, m; vector<string> mapa; array<vector<int>, A> let_in_row; array<vector<int>, A> let_in_col; vector<int> diff_in_row; vector<int> diff_in_col; vector<pair<int, int> > ready; Game(int _n, int _m, vector<string> &_mapa) { n = _n; m = _m; swap(mapa, _mapa); diff_in_col.resize(m); diff_in_row.resize(n); REP(i, A) { let_in_col[i].resize(m); let_in_row[i].resize(n); } REP(i, n) { REP(j, m) { int ind = mapa[i][j] - 'A'; let_in_row[ind][i]++; if (let_in_row[ind][i] == 1) { diff_in_row[i]++; } let_in_col[ind][j]++; if (let_in_col[ind][j] == 1) { diff_in_col[j]++; } } } REP(i, n) { if (diff_in_row[i] == 1) { ready.push_back({0, i}); } } REP(i, m) { if (diff_in_col[i] == 1) { ready.push_back({1, i}); } } } char remove(int num, bool col) { char val = 'A'; if (col) { REP(i, n) { if (mapa[i][num] == ' ') { continue; } val = mapa[i][num]; int ind = mapa[i][num] - 'A'; mapa[i][num] = ' '; let_in_row[ind][i]--; if (let_in_row[ind][i] == 0) { diff_in_row[i]--; if (diff_in_row[i] == 1) { ready.push_back({0, i}); } } } } else { REP(j, m) { if (mapa[num][j] == ' ') { continue; } val = mapa[num][j]; int ind = mapa[num][j] - 'A'; mapa[num][j] = ' '; let_in_col[ind][j]--; if (let_in_col[ind][j] == 0) { diff_in_col[j]--; if (diff_in_col[j] == 1) { ready.push_back({1, j}); } } } } return val; } bool finished() { return ready.empty(); } }; struct Op { char type; int num; char let; }; int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector<string> mapa(n); REP(i, n) { cin >> mapa[i]; } Game game(n, m, mapa); vector<Op> ops; while (!game.finished()) { pair<int, int> curr = game.ready.back(); game.ready.pop_back(); Op op{curr.first ? 'K' : 'R', curr.second+1, game.remove(curr.second, curr.first)}; ops.push_back(op); } reverse(ops.begin(), ops.end()); cout << ssize(ops) << "\n"; for (auto op : ops) { cout << op.type << " " << op.num << " " << op.let << "\n"; } } |