#include <bits/stdc++.h> using namespace std; #define st first #define nd second typedef long long ll; typedef long double ld; typedef __int128 int128; typedef vector<int> vi; typedef pair<int,int> pi; typedef pair<double,double> pd; typedef pair<ll,ll> pl; const int max_n = 2007; const int alf = 30; int arr[max_n][max_n]; int val_c[max_n][alf]; int val_r[max_n][alf]; int diff_c[max_n]; int diff_r[max_n]; bool odw_c[max_n]; bool odw_r[max_n]; //0 - r, 1 - c //st - num, nd - color queue<pair<bool,pi>> q; vector<pair<bool,pi>> ans; int check_val(bool b, int x){ //cout << "check val\n"; int rep = 1; //wsm losowe moze dzialac if(!b){ for(int j = 1; j < alf; j++){ if(val_r[x][j] >= 1){ rep = j; break; } } } else{ for(int j = 1; j < alf; j++){ if(val_c[x][j] >= 1){ rep = j; break; } } } return rep; } int main(){ ios::sync_with_stdio(0); cin.tie(0); int n,m; cin >> n >> m; for(int i = 1; i <= n; i++){ string s; cin >> s; for(int j = 1; j <= m; j++){ int val = int(s[j-1]-'A'+1); arr[i][j] = val; val_c[j][val]++; if(val_c[j][val] == 1) diff_c[j]++; val_r[i][val]++; if(val_r[i][val] == 1) diff_r[i]++; } } //cout << "wczytane\n"; for(int i = 1; i <= n; i++){ if(diff_r[i] == 1){ odw_r[i] = 1; int rep = check_val(0,i); q.push({0,{i,rep}}); } } for(int i = 1; i <= m; i++){ if(diff_c[i] == 1){ odw_c[i] = 1; int rep = check_val(1,i); q.push({1,{i,rep}}); } } //cout << "dodane na kolejke\n"; while(!q.empty()){ ans.push_back(q.front()); bool b = q.front().st; int num = q.front().nd.st; int color = q.front().nd.nd; q.pop(); int end = (b ? n:m); for(int i = 1; i <= end; i++){ int r = (b ? i : num); int c = (b ? num : i); val_r[r][color]--; if(val_r[r][color] == 0) diff_r[r]--; if(diff_r[r] == 1 && !odw_r[r]){ odw_r[r] = 1; int rep = check_val(0,r); q.push({0,{r,rep}}); } val_c[c][color]--; if(val_c[c][color] == 0) diff_c[c]--; if(diff_c[c] == 1 && !odw_c[c]){ odw_c[c] = 1; int rep = check_val(1,c); q.push({1,{c,rep}}); } } } reverse(ans.begin(),ans.end()); cout << ans.size() << '\n'; for(auto x:ans){ if(!x.st) cout << "R "; else cout << "K "; cout << x.nd.st << ' '; cout << char(x.nd.nd+'A'-1) << '\n'; } return 0; } //g++ -O3 -static -Wall .cpp -std=c++17
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 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second typedef long long ll; typedef long double ld; typedef __int128 int128; typedef vector<int> vi; typedef pair<int,int> pi; typedef pair<double,double> pd; typedef pair<ll,ll> pl; const int max_n = 2007; const int alf = 30; int arr[max_n][max_n]; int val_c[max_n][alf]; int val_r[max_n][alf]; int diff_c[max_n]; int diff_r[max_n]; bool odw_c[max_n]; bool odw_r[max_n]; //0 - r, 1 - c //st - num, nd - color queue<pair<bool,pi>> q; vector<pair<bool,pi>> ans; int check_val(bool b, int x){ //cout << "check val\n"; int rep = 1; //wsm losowe moze dzialac if(!b){ for(int j = 1; j < alf; j++){ if(val_r[x][j] >= 1){ rep = j; break; } } } else{ for(int j = 1; j < alf; j++){ if(val_c[x][j] >= 1){ rep = j; break; } } } return rep; } int main(){ ios::sync_with_stdio(0); cin.tie(0); int n,m; cin >> n >> m; for(int i = 1; i <= n; i++){ string s; cin >> s; for(int j = 1; j <= m; j++){ int val = int(s[j-1]-'A'+1); arr[i][j] = val; val_c[j][val]++; if(val_c[j][val] == 1) diff_c[j]++; val_r[i][val]++; if(val_r[i][val] == 1) diff_r[i]++; } } //cout << "wczytane\n"; for(int i = 1; i <= n; i++){ if(diff_r[i] == 1){ odw_r[i] = 1; int rep = check_val(0,i); q.push({0,{i,rep}}); } } for(int i = 1; i <= m; i++){ if(diff_c[i] == 1){ odw_c[i] = 1; int rep = check_val(1,i); q.push({1,{i,rep}}); } } //cout << "dodane na kolejke\n"; while(!q.empty()){ ans.push_back(q.front()); bool b = q.front().st; int num = q.front().nd.st; int color = q.front().nd.nd; q.pop(); int end = (b ? n:m); for(int i = 1; i <= end; i++){ int r = (b ? i : num); int c = (b ? num : i); val_r[r][color]--; if(val_r[r][color] == 0) diff_r[r]--; if(diff_r[r] == 1 && !odw_r[r]){ odw_r[r] = 1; int rep = check_val(0,r); q.push({0,{r,rep}}); } val_c[c][color]--; if(val_c[c][color] == 0) diff_c[c]--; if(diff_c[c] == 1 && !odw_c[c]){ odw_c[c] = 1; int rep = check_val(1,c); q.push({1,{c,rep}}); } } } reverse(ans.begin(),ans.end()); cout << ans.size() << '\n'; for(auto x:ans){ if(!x.st) cout << "R "; else cout << "K "; cout << x.nd.st << ' '; cout << char(x.nd.nd+'A'-1) << '\n'; } return 0; } //g++ -O3 -static -Wall .cpp -std=c++17 |