#include <bits/stdc++.h> using namespace std; struct segment { vector<int> colors; int cnt = 0; bool visited = false; }; struct shoot { bool row; int idx, color; }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; vector<vector<int>> board(n, vector<int>(m)); vector<segment> cols(m), rows(n); for(int i = 0; i < n; i++) rows[i].colors.resize(26, 0); for(int i = 0; i < m; i++) cols[i].colors.resize(26, 0); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { char chr; cin >> chr; chr -= 'A'; board[i][j] = chr; cols[j].colors[chr]++; if(cols[j].colors[chr] == 1) cols[j].cnt++; rows[i].colors[chr]++; if(rows[i].colors[chr] == 1) rows[i].cnt++; } } queue<pair<bool, int>> q; for(int i = 0; i < m; i++) { if(cols[i].cnt == 1) { q.push({0, i}); cols[i].visited = true; } } for(int i = 0; i < n; i++) { if(rows[i].cnt == 1) { q.push({1, i}); rows[i].visited = true; } } vector<shoot> result; while(q.size()) { bool isRow = q.front().first; int idx = q.front().second; int color; for(int i = 0; i < 26; i++) if((isRow ? rows[idx] : cols[idx]).colors[i] > 0) color = i; result.push_back({isRow, idx, color}); q.pop(); if(isRow) { for(int i = 0; i < m; i++) { if(!cols[i].visited) { cols[i].colors[board[idx][i]]--; if(cols[i].colors[board[idx][i]] == 0) cols[i].cnt--; if(cols[i].cnt == 1) { q.push({0, i}); cols[i].visited = true; } } } } else { for(int i = 0; i < n; i++) { if(!rows[i].visited) { rows[i].colors[board[i][idx]]--; if(rows[i].colors[board[i][idx]] == 0) rows[i].cnt--; if(rows[i].cnt == 1) { q.push({1, i}); rows[i].visited = true; } } } } } cout << result.size() << '\n'; for(int i = result.size() - 1; i >= 0; i--) { cout << (result[i].row ? "R " : "K ") << result[i].idx + 1 << ' ' << char(result[i].color + 'A') << '\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 | #include <bits/stdc++.h> using namespace std; struct segment { vector<int> colors; int cnt = 0; bool visited = false; }; struct shoot { bool row; int idx, color; }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; vector<vector<int>> board(n, vector<int>(m)); vector<segment> cols(m), rows(n); for(int i = 0; i < n; i++) rows[i].colors.resize(26, 0); for(int i = 0; i < m; i++) cols[i].colors.resize(26, 0); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { char chr; cin >> chr; chr -= 'A'; board[i][j] = chr; cols[j].colors[chr]++; if(cols[j].colors[chr] == 1) cols[j].cnt++; rows[i].colors[chr]++; if(rows[i].colors[chr] == 1) rows[i].cnt++; } } queue<pair<bool, int>> q; for(int i = 0; i < m; i++) { if(cols[i].cnt == 1) { q.push({0, i}); cols[i].visited = true; } } for(int i = 0; i < n; i++) { if(rows[i].cnt == 1) { q.push({1, i}); rows[i].visited = true; } } vector<shoot> result; while(q.size()) { bool isRow = q.front().first; int idx = q.front().second; int color; for(int i = 0; i < 26; i++) if((isRow ? rows[idx] : cols[idx]).colors[i] > 0) color = i; result.push_back({isRow, idx, color}); q.pop(); if(isRow) { for(int i = 0; i < m; i++) { if(!cols[i].visited) { cols[i].colors[board[idx][i]]--; if(cols[i].colors[board[idx][i]] == 0) cols[i].cnt--; if(cols[i].cnt == 1) { q.push({0, i}); cols[i].visited = true; } } } } else { for(int i = 0; i < n; i++) { if(!rows[i].visited) { rows[i].colors[board[i][idx]]--; if(rows[i].colors[board[i][idx]] == 0) rows[i].cnt--; if(rows[i].cnt == 1) { q.push({1, i}); rows[i].visited = true; } } } } } cout << result.size() << '\n'; for(int i = result.size() - 1; i >= 0; i--) { cout << (result[i].row ? "R " : "K ") << result[i].idx + 1 << ' ' << char(result[i].color + 'A') << '\n'; } } |