#include <iostream>
#include <vector>
#include <utility>
#include <queue>
#include <stack>
#include <array>
using namespace std;
void input(int & n, int & m, vector<vector<char>> & board) {
cin >> n >> m;
board.resize(n, vector<char>(m));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> board[i][j];
board[i][j] -= 'A';
}
}
}
void make_rows(int const n, int const m, vector<vector<char>> const & board, vector<pair<int,vector<int>>> & rows) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(rows[i].second[board[i][j]]++ == 0) {
rows[i].first++;
}
}
}
}
void make_columns(int const n, int const m, vector<vector<char>> const & board, vector<pair<int,vector<int>>> & columns) {
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(columns[i].second[board[j][i]]++ == 0) {
columns[i].first++;
}
}
}
}
void make_result(int const n, int const m, vector<vector<char>> & board, vector<pair<int,vector<int>>> & rows, vector<pair<int,vector<int>>> & columns, stack<array<int,3>> & result) {
queue<array<int,2>> to_process;
for(int i = 0; i < n; i++) {
if(rows[i].first == 1) {
to_process.push({1, i});
}
}
for(int i = 0; i < m; i++) {
if(columns[i].first == 1) {
to_process.push({-1, i});
}
}
while(!to_process.empty()) {
int cur_type = to_process.front()[0];
int cur_ind = to_process.front()[1];
to_process.pop();
if(cur_type == 1 && rows[cur_ind].first == 1) {
char cur_color = -1;
for(int i = 0; i < m; i++) {
if(board[cur_ind][i] != -1) {
cur_color = board[cur_ind][i];
board[cur_ind][i] = -1;
if(--columns[i].second[cur_color] == 0) {
if(--columns[i].first == 1) {
to_process.push({-1, i});
}
}
}
}
result.push({cur_type, cur_ind, cur_color});
} else if(cur_type == -1 && columns[cur_ind].first == 1) {
char cur_color = -1;
for(int i = 0; i < n; i++) {
if(board[i][cur_ind] != -1) {
cur_color = board[i][cur_ind];
board[i][cur_ind] = -1;
if(--rows[i].second[cur_color] == 0) {
if(--rows[i].first == 1) {
to_process.push({1, i});
}
}
}
}
result.push({cur_type, cur_ind, cur_color});
}
}
}
void solve() {
int n, m;
vector<vector<char>> board;
input(n, m, board);
vector<pair<int,vector<int>>> rows(n, {0, vector<int>('Z'-'A'+1, 0)});
make_rows(n, m, board, rows);
vector<pair<int,vector<int>>> columns(m, {0, vector<int>('Z'-'A'+1, 0)});
make_columns(n, m, board, columns);
stack<array<int,3>> result;
make_result(n, m, board, rows, columns, result);
cout << result.size() << "\n";
while(!result.empty()) {
cout << (result.top()[0] == 1 ? 'R' : 'K') << " " << result.top()[1] + 1 << " " << (char)(result.top()[2] + 'A') << "\n";
result.pop();
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
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 | #include <iostream> #include <vector> #include <utility> #include <queue> #include <stack> #include <array> using namespace std; void input(int & n, int & m, vector<vector<char>> & board) { cin >> n >> m; board.resize(n, vector<char>(m)); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { cin >> board[i][j]; board[i][j] -= 'A'; } } } void make_rows(int const n, int const m, vector<vector<char>> const & board, vector<pair<int,vector<int>>> & rows) { for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(rows[i].second[board[i][j]]++ == 0) { rows[i].first++; } } } } void make_columns(int const n, int const m, vector<vector<char>> const & board, vector<pair<int,vector<int>>> & columns) { for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if(columns[i].second[board[j][i]]++ == 0) { columns[i].first++; } } } } void make_result(int const n, int const m, vector<vector<char>> & board, vector<pair<int,vector<int>>> & rows, vector<pair<int,vector<int>>> & columns, stack<array<int,3>> & result) { queue<array<int,2>> to_process; for(int i = 0; i < n; i++) { if(rows[i].first == 1) { to_process.push({1, i}); } } for(int i = 0; i < m; i++) { if(columns[i].first == 1) { to_process.push({-1, i}); } } while(!to_process.empty()) { int cur_type = to_process.front()[0]; int cur_ind = to_process.front()[1]; to_process.pop(); if(cur_type == 1 && rows[cur_ind].first == 1) { char cur_color = -1; for(int i = 0; i < m; i++) { if(board[cur_ind][i] != -1) { cur_color = board[cur_ind][i]; board[cur_ind][i] = -1; if(--columns[i].second[cur_color] == 0) { if(--columns[i].first == 1) { to_process.push({-1, i}); } } } } result.push({cur_type, cur_ind, cur_color}); } else if(cur_type == -1 && columns[cur_ind].first == 1) { char cur_color = -1; for(int i = 0; i < n; i++) { if(board[i][cur_ind] != -1) { cur_color = board[i][cur_ind]; board[i][cur_ind] = -1; if(--rows[i].second[cur_color] == 0) { if(--rows[i].first == 1) { to_process.push({1, i}); } } } } result.push({cur_type, cur_ind, cur_color}); } } } void solve() { int n, m; vector<vector<char>> board; input(n, m, board); vector<pair<int,vector<int>>> rows(n, {0, vector<int>('Z'-'A'+1, 0)}); make_rows(n, m, board, rows); vector<pair<int,vector<int>>> columns(m, {0, vector<int>('Z'-'A'+1, 0)}); make_columns(n, m, board, columns); stack<array<int,3>> result; make_result(n, m, board, rows, columns, result); cout << result.size() << "\n"; while(!result.empty()) { cout << (result.top()[0] == 1 ? 'R' : 'K') << " " << result.top()[1] + 1 << " " << (char)(result.top()[2] + 'A') << "\n"; result.pop(); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |
English