#include <bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define sn second typedef long long ll; typedef vector<int> VI; typedef vector<char> VC; typedef pair<int, int> PI; const int ALPHA_L = 'Z' - 'A' + 1; const int ROW = 0; const int COL = 1; int toi(char a) { return a - 'A'; } char toch(int a) { return (char)(((char)a + 'A')); } struct Stripe { int col; int type; int i; Stripe(char col, int type, int i) : col(col), type(type), i(i) {} }; void print(Stripe& s) { cout << (s.type == ROW ? 'R' : 'K') << " " << s.i + 1 << " " << toch(s.col) << endl; } int first_nonzero(const VI& vec) { for (int i = 0; i < vec.size(); ++i) { if (vec[i] != 0) { return i; } } return 0; } int main() { int n, m; cin >> n >> m; vector<VC> A(n, VC(m)); vector<VI> cnt[2]; cnt[ROW] = vector<VI>(n, VI(ALPHA_L, 0)); cnt[COL] = vector<VI>(m, VI(ALPHA_L, 0)); for (int r = 0; r < n; r++) { for (int c = 0; c < m; c++) { char col; cin >> col; A[r][c] = col; cnt[ROW][r][toi(col)]++; cnt[COL][c][toi(col)]++; } } vector<Stripe> S; vector<bool> visited[2]; visited[ROW] = vector<bool>(n, false); visited[COL] = vector<bool>(m, false); VI colors[2]; colors[ROW] = VI(n, 0); colors[COL] = VI(m, 0); queue<Stripe> Q; // int unpainted[2]; // unpainted[ROW] = m; // unpainted[COL] = n; // for (int c = 0; c < m; c++) { // for (int col = 0; col < ALPHA_L; col++) { // if (cnt[COL][c][col] > 0) colors[COL][c]++; // } // } for (int r = 0; r < n; r++) { for (int col = 0; col < ALPHA_L; col++) { if (cnt[ROW][r][col] > 0) colors[ROW][r]++; } if (colors[ROW][r] == 1) { int col = first_nonzero(cnt[ROW][r]); visited[ROW][r] = true; Q.push(Stripe(col, ROW, r)); } } for (int c = 0; c < m; c++) { for (int col = 0; col < ALPHA_L; col++) { if (cnt[COL][c][col] > 0) colors[COL][c]++; } if (colors[COL][c] == 1) { int col = first_nonzero(cnt[COL][c]); visited[COL][c] = true; Q.push(Stripe(col, COL, c)); } } // for (int c = 0; c < m; c++) { // cout<<colors[COL][c]<<" | "; // for (int col = 0; col < ALPHA_L; col++) { // cout<<cnt[COL][c][col]<<" "; // } // cout<<endl; // } vector<Stripe> R; while (!Q.empty()) { Stripe s = Q.front(); // print(s); Q.pop(); if (s.type == ROW && colors[ROW][s.i] == 0) continue; if (s.type == COL && colors[COL][s.i] == 0) continue; if (s.type == ROW) { // int r = s.i; for (int c = 0; c < m; c++) { cnt[COL][c][s.col]--; if (cnt[COL][c][s.col] == 0) { colors[COL][c]--; if (colors[COL][c] == 1 && !visited[COL][c]) { int col = first_nonzero(cnt[COL][c]); visited[COL][c] = true; Q.push(Stripe(col, COL, c)); } } } } else { // int c = s.i; for (int r = 0; r < n; r++) { cnt[ROW][r][s.col]--; if (cnt[ROW][r][s.col] == 0) { colors[ROW][r]--; if (colors[ROW][r] == 1 && !visited[ROW][r]) { int col = first_nonzero(cnt[ROW][r]); visited[ROW][r] = true; Q.push(Stripe(col, ROW, r)); } } } } R.push_back(s); } // cout << endl; cout << R.size() << endl; for (int i = R.size() - 1; i >= 0; i--) { Stripe s = R[i]; print(s); } 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 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 | #include <bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define sn second typedef long long ll; typedef vector<int> VI; typedef vector<char> VC; typedef pair<int, int> PI; const int ALPHA_L = 'Z' - 'A' + 1; const int ROW = 0; const int COL = 1; int toi(char a) { return a - 'A'; } char toch(int a) { return (char)(((char)a + 'A')); } struct Stripe { int col; int type; int i; Stripe(char col, int type, int i) : col(col), type(type), i(i) {} }; void print(Stripe& s) { cout << (s.type == ROW ? 'R' : 'K') << " " << s.i + 1 << " " << toch(s.col) << endl; } int first_nonzero(const VI& vec) { for (int i = 0; i < vec.size(); ++i) { if (vec[i] != 0) { return i; } } return 0; } int main() { int n, m; cin >> n >> m; vector<VC> A(n, VC(m)); vector<VI> cnt[2]; cnt[ROW] = vector<VI>(n, VI(ALPHA_L, 0)); cnt[COL] = vector<VI>(m, VI(ALPHA_L, 0)); for (int r = 0; r < n; r++) { for (int c = 0; c < m; c++) { char col; cin >> col; A[r][c] = col; cnt[ROW][r][toi(col)]++; cnt[COL][c][toi(col)]++; } } vector<Stripe> S; vector<bool> visited[2]; visited[ROW] = vector<bool>(n, false); visited[COL] = vector<bool>(m, false); VI colors[2]; colors[ROW] = VI(n, 0); colors[COL] = VI(m, 0); queue<Stripe> Q; // int unpainted[2]; // unpainted[ROW] = m; // unpainted[COL] = n; // for (int c = 0; c < m; c++) { // for (int col = 0; col < ALPHA_L; col++) { // if (cnt[COL][c][col] > 0) colors[COL][c]++; // } // } for (int r = 0; r < n; r++) { for (int col = 0; col < ALPHA_L; col++) { if (cnt[ROW][r][col] > 0) colors[ROW][r]++; } if (colors[ROW][r] == 1) { int col = first_nonzero(cnt[ROW][r]); visited[ROW][r] = true; Q.push(Stripe(col, ROW, r)); } } for (int c = 0; c < m; c++) { for (int col = 0; col < ALPHA_L; col++) { if (cnt[COL][c][col] > 0) colors[COL][c]++; } if (colors[COL][c] == 1) { int col = first_nonzero(cnt[COL][c]); visited[COL][c] = true; Q.push(Stripe(col, COL, c)); } } // for (int c = 0; c < m; c++) { // cout<<colors[COL][c]<<" | "; // for (int col = 0; col < ALPHA_L; col++) { // cout<<cnt[COL][c][col]<<" "; // } // cout<<endl; // } vector<Stripe> R; while (!Q.empty()) { Stripe s = Q.front(); // print(s); Q.pop(); if (s.type == ROW && colors[ROW][s.i] == 0) continue; if (s.type == COL && colors[COL][s.i] == 0) continue; if (s.type == ROW) { // int r = s.i; for (int c = 0; c < m; c++) { cnt[COL][c][s.col]--; if (cnt[COL][c][s.col] == 0) { colors[COL][c]--; if (colors[COL][c] == 1 && !visited[COL][c]) { int col = first_nonzero(cnt[COL][c]); visited[COL][c] = true; Q.push(Stripe(col, COL, c)); } } } } else { // int c = s.i; for (int r = 0; r < n; r++) { cnt[ROW][r][s.col]--; if (cnt[ROW][r][s.col] == 0) { colors[ROW][r]--; if (colors[ROW][r] == 1 && !visited[ROW][r]) { int col = first_nonzero(cnt[ROW][r]); visited[ROW][r] = true; Q.push(Stripe(col, ROW, r)); } } } } R.push_back(s); } // cout << endl; cout << R.size() << endl; for (int i = R.size() - 1; i >= 0; i--) { Stripe s = R[i]; print(s); } return 0; } |