#include<bits/stdc++.h> using namespace std; const int n_max = 2*1e3+7; int kolumnu[n_max][30]; int wiersze[n_max][30]; int used[2][n_max]; string s_tab[n_max]; vector<tuple<int, int, int>> ans; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int nr_a = (int) 'A'; int n, m; cin >> n >> m; for(int i = 0; i < n; i++){ string s; cin >> s; s_tab[i] = s; for(int j = 0; j < m; j++){ int nr = (int)s[j] - nr_a; kolumnu[j][nr]++; wiersze[i][nr]++; } } int colored = 0; for(int t = 0; t < n + m; t++){ // Przejście po wierszach for(int i = 0; i < n; i++){ if(used[0][i] == 1){ continue; } int first = -1; int second = -1; for(int l = 0; l < 30; l++){ if(first == -1 and wiersze[i][l] != 0){ first = l; continue; } if(first != -1 and wiersze[i][l] != 0){ second = l; break; } } if(first == -1){ used[0][i] = 1; colored++; continue; } if(second == -1){ for(int j = 0; j < m; j++){ int nr = (int) s_tab[i][j] - nr_a; kolumnu[j][nr]--; } colored++; used[0][i] = 1; ans.push_back({0, i, first}); } } // Przejście po kolumnach for(int i = 0; i < m; i++){ if(used[1][i] == 1){ continue; } int first = -1; int second = -1; for(int l = 0; l < 30; l++){ if(first == -1 and kolumnu[i][l] != 0){ first = l; continue; } if(first != -1 and kolumnu[i][l] != 0){ second = l; break; } } if(first == -1){ used[1][i] = 1; colored++; continue; } if(second == -1){ for(int j = 0; j < n; j++){ int nr = (int) s_tab[j][i] - nr_a; wiersze[j][nr]--; } colored++; used[1][i] = 1; ans.push_back({1, i, first}); } } } cout << ans.size() << '\n'; reverse(ans.begin(), ans.end()); for(auto [type, nr, color]: ans){ if(type == 0){ cout << "R "; }else{ cout << "K "; } cout << nr + 1 << ' ' << (char) (color+nr_a) << '\n'; } 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 | #include<bits/stdc++.h> using namespace std; const int n_max = 2*1e3+7; int kolumnu[n_max][30]; int wiersze[n_max][30]; int used[2][n_max]; string s_tab[n_max]; vector<tuple<int, int, int>> ans; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int nr_a = (int) 'A'; int n, m; cin >> n >> m; for(int i = 0; i < n; i++){ string s; cin >> s; s_tab[i] = s; for(int j = 0; j < m; j++){ int nr = (int)s[j] - nr_a; kolumnu[j][nr]++; wiersze[i][nr]++; } } int colored = 0; for(int t = 0; t < n + m; t++){ // Przejście po wierszach for(int i = 0; i < n; i++){ if(used[0][i] == 1){ continue; } int first = -1; int second = -1; for(int l = 0; l < 30; l++){ if(first == -1 and wiersze[i][l] != 0){ first = l; continue; } if(first != -1 and wiersze[i][l] != 0){ second = l; break; } } if(first == -1){ used[0][i] = 1; colored++; continue; } if(second == -1){ for(int j = 0; j < m; j++){ int nr = (int) s_tab[i][j] - nr_a; kolumnu[j][nr]--; } colored++; used[0][i] = 1; ans.push_back({0, i, first}); } } // Przejście po kolumnach for(int i = 0; i < m; i++){ if(used[1][i] == 1){ continue; } int first = -1; int second = -1; for(int l = 0; l < 30; l++){ if(first == -1 and kolumnu[i][l] != 0){ first = l; continue; } if(first != -1 and kolumnu[i][l] != 0){ second = l; break; } } if(first == -1){ used[1][i] = 1; colored++; continue; } if(second == -1){ for(int j = 0; j < n; j++){ int nr = (int) s_tab[j][i] - nr_a; wiersze[j][nr]--; } colored++; used[1][i] = 1; ans.push_back({1, i, first}); } } } cout << ans.size() << '\n'; reverse(ans.begin(), ans.end()); for(auto [type, nr, color]: ans){ if(type == 0){ cout << "R "; }else{ cout << "K "; } cout << nr + 1 << ' ' << (char) (color+nr_a) << '\n'; } return 0; } |