#include <bits/stdc++.h> #define LL_INF 1000000000000000007 typedef long long ll; typedef std::string str; typedef std::pair<ll, ll> llPair; template <typename T> using vec = std::vector<T>; using namespace std; // n - rzedy, m - kolumny int n, m; vec<vec<char>> img; struct Info { char color { 'A' }; int waiting { 0 }; bool ok { false }; }; vec<Info> cols; vec<Info> rows; int main() { ios_base::sync_with_stdio(false); std::cout.tie(nullptr); cin.tie(nullptr); cin>>n>>m; img = vec<vec<char>>(n, vec<char>(m)); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) cin>>img[i][j]; } cols = vec<Info>(m); // K rows = vec<Info>(n); // R for(int i = 0; i < n; i++) { int lind = m; for(int j = 0; j < m; j++) { if(cols[j].color != img[i][j] || !cols[j].ok) { rows[i].color = img[i][j]; lind = j + 1; break; } } for(int j = lind; j < m; j++) { if(rows[i].color != img[i][j]) { cols[j].color = img[i][j]; cols[j].ok = true; } } } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { rows[i].waiting += (img[i][j] == rows[i].color && img[i][j] != cols[j].color); } } for(int j = 0; j < m; j++) { for(int i = 0; i < n; i++) { cols[j].waiting += (img[i][j] != rows[i].color && img[i][j] == cols[j].color); } } // for(auto [col, w] : cols) { // std::cout<<"("<<col<<","<<w<<") "; // } // std::cout<<"\n"; // for(auto [col, w] : rows) { // std::cout<<"("<<col<<","<<w<<") "; // } // std::cout<<"\n"; // t = 1 -> kolumna // t = 2 -> rzad struct QEntry { int type; int id; }; queue<QEntry> q; for(int i = 0; i < n; i++) if(rows[i].waiting == 0) q.push({2, i}); for(int i = 0; i < m; i++) if(cols[i].waiting == 0) q.push({1, i}); std::cout<<n + m<<"\n"; while(!q.empty()) { auto [type, id] = q.front(); q.pop(); if(type == 1) { std::cout<<"K "<<id + 1<<" "<<cols[id].color<<"\n"; for(int i = 0; i < n; i++) { if(img[i][id] != cols[id].color) { rows[i].waiting--; if(rows[i].waiting == 0) q.push({2, i}); } } } else { std::cout<<"R "<<id + 1<<" "<<rows[id].color<<"\n"; for(int i = 0; i < m; i++) { if(img[id][i] != rows[id].color) { cols[i].waiting--; if(cols[i].waiting == 0) q.push({1, i}); } } } } std::cout.flush(); }
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 | #include <bits/stdc++.h> #define LL_INF 1000000000000000007 typedef long long ll; typedef std::string str; typedef std::pair<ll, ll> llPair; template <typename T> using vec = std::vector<T>; using namespace std; // n - rzedy, m - kolumny int n, m; vec<vec<char>> img; struct Info { char color { 'A' }; int waiting { 0 }; bool ok { false }; }; vec<Info> cols; vec<Info> rows; int main() { ios_base::sync_with_stdio(false); std::cout.tie(nullptr); cin.tie(nullptr); cin>>n>>m; img = vec<vec<char>>(n, vec<char>(m)); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) cin>>img[i][j]; } cols = vec<Info>(m); // K rows = vec<Info>(n); // R for(int i = 0; i < n; i++) { int lind = m; for(int j = 0; j < m; j++) { if(cols[j].color != img[i][j] || !cols[j].ok) { rows[i].color = img[i][j]; lind = j + 1; break; } } for(int j = lind; j < m; j++) { if(rows[i].color != img[i][j]) { cols[j].color = img[i][j]; cols[j].ok = true; } } } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { rows[i].waiting += (img[i][j] == rows[i].color && img[i][j] != cols[j].color); } } for(int j = 0; j < m; j++) { for(int i = 0; i < n; i++) { cols[j].waiting += (img[i][j] != rows[i].color && img[i][j] == cols[j].color); } } // for(auto [col, w] : cols) { // std::cout<<"("<<col<<","<<w<<") "; // } // std::cout<<"\n"; // for(auto [col, w] : rows) { // std::cout<<"("<<col<<","<<w<<") "; // } // std::cout<<"\n"; // t = 1 -> kolumna // t = 2 -> rzad struct QEntry { int type; int id; }; queue<QEntry> q; for(int i = 0; i < n; i++) if(rows[i].waiting == 0) q.push({2, i}); for(int i = 0; i < m; i++) if(cols[i].waiting == 0) q.push({1, i}); std::cout<<n + m<<"\n"; while(!q.empty()) { auto [type, id] = q.front(); q.pop(); if(type == 1) { std::cout<<"K "<<id + 1<<" "<<cols[id].color<<"\n"; for(int i = 0; i < n; i++) { if(img[i][id] != cols[id].color) { rows[i].waiting--; if(rows[i].waiting == 0) q.push({2, i}); } } } else { std::cout<<"R "<<id + 1<<" "<<rows[id].color<<"\n"; for(int i = 0; i < m; i++) { if(img[id][i] != rows[id].color) { cols[i].waiting--; if(cols[i].waiting == 0) q.push({1, i}); } } } } std::cout.flush(); } |