#include <bits/stdc++.h> using namespace std; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() #define ssize(x) int(x.size()) #define pb push_back #define fi first #define se second #define ld long double typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair <ld, ld> pd; using LL=long long; #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 MAXN = 2007; int grid[MAXN][MAXN]; bool valid[MAXN][MAXN]; bool done_row[MAXN], done_col[MAXN]; int cnt_col[MAXN], cnt_row[MAXN]; int letter_col[MAXN][26], letter_row[MAXN][26]; struct Result { char who; int nr, col; Result(char cwho, int cnr, int ccol) { this->who = cwho; this->nr = cnr; this->col = ccol; } }; void solve() { int N, M; cin >> N >> M; REP(i, N) REP(j, M) { valid[i][j] = true; char letter; cin >> letter; int id = int(letter - 'A'); grid[i][j] = id; letter_row[i][id]++; cnt_row[i] += (letter_row[i][id] == 1); letter_col[j][id]++; cnt_col[j] += (letter_col[j][id] == 1); } vector<Result> res; queue< pii > rows, cols; REP(i, N) { if (cnt_row[i] == 1) { done_row[i] = true; int which; REP(j, 26) if(letter_row[i][j] > 0){ which = j; break; } rows.push({i, which}); } } REP(i, M) { if (cnt_col[i] == 1) { done_col[i] = true; int which; REP(j, 26) if(letter_col[i][j] > 0){ which = j; break; } cols.push({i, which}); } } while(!rows.empty() || !cols.empty()) { while(!rows.empty()) { auto &[row, color] = rows.front(); rows.pop(); // bool ok = false; // if (!ok) continue; res.pb(Result('R', row, color)); REP(j, M) { if (!valid[row][j]) continue; else valid[row][j] = false; letter_col[j][color]--; cnt_col[j] -= (letter_col[j][color] == 0); if (cnt_col[j] == 1 && !done_col[j]) { done_col[j] = true; int which; REP(k, 26) if(letter_col[j][k] > 0){ which = k; break; } cols.push({j, which}); } } } while(!cols.empty()) { auto &[col, color] = cols.front(); cols.pop(); res.pb(Result('K', col, color)); REP(j, N) { if (!valid[j][col]) continue; else valid[j][col] = false; letter_row[j][color]--; cnt_row[j] -= (letter_row[j][color] == 0); if (cnt_row[j] == 1 && !done_row[j]) { done_row[j] = true; int which; REP(k, 26) if(letter_row[j][k] > 0){ which = k; break; } rows.push({j, which}); } } } } cout << res.size() << '\n'; reverse(all(res)); for (auto result : res) { cout << result.who << ' ' << result.nr + 1 << ' ' << char(result.col + 'A') << '\n'; } } signed main() { cin.tie(0)->sync_with_stdio(0); int t = 1; // cin >> t; while(t--) { solve(); } }
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | #include <bits/stdc++.h> using namespace std; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() #define ssize(x) int(x.size()) #define pb push_back #define fi first #define se second #define ld long double typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair <ld, ld> pd; using LL=long long; #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 MAXN = 2007; int grid[MAXN][MAXN]; bool valid[MAXN][MAXN]; bool done_row[MAXN], done_col[MAXN]; int cnt_col[MAXN], cnt_row[MAXN]; int letter_col[MAXN][26], letter_row[MAXN][26]; struct Result { char who; int nr, col; Result(char cwho, int cnr, int ccol) { this->who = cwho; this->nr = cnr; this->col = ccol; } }; void solve() { int N, M; cin >> N >> M; REP(i, N) REP(j, M) { valid[i][j] = true; char letter; cin >> letter; int id = int(letter - 'A'); grid[i][j] = id; letter_row[i][id]++; cnt_row[i] += (letter_row[i][id] == 1); letter_col[j][id]++; cnt_col[j] += (letter_col[j][id] == 1); } vector<Result> res; queue< pii > rows, cols; REP(i, N) { if (cnt_row[i] == 1) { done_row[i] = true; int which; REP(j, 26) if(letter_row[i][j] > 0){ which = j; break; } rows.push({i, which}); } } REP(i, M) { if (cnt_col[i] == 1) { done_col[i] = true; int which; REP(j, 26) if(letter_col[i][j] > 0){ which = j; break; } cols.push({i, which}); } } while(!rows.empty() || !cols.empty()) { while(!rows.empty()) { auto &[row, color] = rows.front(); rows.pop(); // bool ok = false; // if (!ok) continue; res.pb(Result('R', row, color)); REP(j, M) { if (!valid[row][j]) continue; else valid[row][j] = false; letter_col[j][color]--; cnt_col[j] -= (letter_col[j][color] == 0); if (cnt_col[j] == 1 && !done_col[j]) { done_col[j] = true; int which; REP(k, 26) if(letter_col[j][k] > 0){ which = k; break; } cols.push({j, which}); } } } while(!cols.empty()) { auto &[col, color] = cols.front(); cols.pop(); res.pb(Result('K', col, color)); REP(j, N) { if (!valid[j][col]) continue; else valid[j][col] = false; letter_row[j][color]--; cnt_row[j] -= (letter_row[j][color] == 0); if (cnt_row[j] == 1 && !done_row[j]) { done_row[j] = true; int which; REP(k, 26) if(letter_row[j][k] > 0){ which = k; break; } rows.push({j, which}); } } } } cout << res.size() << '\n'; reverse(all(res)); for (auto result : res) { cout << result.who << ' ' << result.nr + 1 << ' ' << char(result.col + 'A') << '\n'; } } signed main() { cin.tie(0)->sync_with_stdio(0); int t = 1; // cin >> t; while(t--) { solve(); } } |