#include <bits/stdc++.h> using namespace std; int x, y; vector<vector<int>> field, dp1, dp2; vector<string> moves; set<int> dne; bool isGood(vector<int>& tmp, int cn, bool pos){ if(dne.find(cn + x * (int)(!pos)) != dne.end()) return 0; int cnt = 0; for(int i = 0; i<tmp.size(); i++){ if(tmp[i]>0) cnt++; } return (cnt<2); } char getColor(int cn, bool pos){ if(pos){ for(int i = 0; i<y; i++){ if(field[cn][i]!=-1) return (char)(field[cn][i] + 'A'); } } else{ for(int i = 0; i<x; i++){ if(field[i][cn]!=-1) return (char)(field[i][cn] + 'A'); } } return 'A'; } void removeEdge(int cn, bool pos){ dne.insert(cn + x * (int)(!pos)); if(pos){ for(int i = 0; i<y; i++){ //cout<<"upd: "<<cn<<" x "<<i<<endl; if(field[cn][i]!=-1) dp2[i][field[cn][i]]--; field[cn][i] = -1; } } else{ for(int i = 0; i<x; i++){ //cout<<"upd: "<<i<<" x "<<cn<<endl; if(field[i][cn] != -1) dp1[i][field[i][cn]]--; field[i][cn] = -1; } } } int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin>>y>>x; field.assign(x, {}); dp1.assign(x, {}); dp2.assign(y, {}); for(int i = 0; i<x; i++) dp1[i].assign(30, 0); for(int i = 0; i<y; i++) dp2[i].assign(30, 0); for(int i = 0; i<x; i++) field[i].assign(y, 0); char a; for(int i = 0; i<y; i++){ for(int j = 0; j<x; j++){ cin>>a; int val = ((int)a-(int)'A'); field[j][i] = val; dp1[j][val]++; dp2[i][val]++; } } int done = 0; while(done<x+y){ //cout<<done<<endl; for(int i = 0; i<x; i++){ if(isGood(dp1[i], i, 1)){ moves.push_back("K "+ to_string(i+1)+ " "); moves.back().push_back(getColor(i, 1)); removeEdge(i, 1); done++; } } for(int i = 0; i<y; i++){ if(isGood(dp2[i], i, 0)){ moves.push_back("R "+ to_string(i+1)+ " "); moves.back().push_back(getColor(i, 0)); removeEdge(i, 0); done++; } } } reverse(moves.begin(), moves.end()); cout<<moves.size()<<"\n"; for(string move : moves){ cout<<move<<"\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 | #include <bits/stdc++.h> using namespace std; int x, y; vector<vector<int>> field, dp1, dp2; vector<string> moves; set<int> dne; bool isGood(vector<int>& tmp, int cn, bool pos){ if(dne.find(cn + x * (int)(!pos)) != dne.end()) return 0; int cnt = 0; for(int i = 0; i<tmp.size(); i++){ if(tmp[i]>0) cnt++; } return (cnt<2); } char getColor(int cn, bool pos){ if(pos){ for(int i = 0; i<y; i++){ if(field[cn][i]!=-1) return (char)(field[cn][i] + 'A'); } } else{ for(int i = 0; i<x; i++){ if(field[i][cn]!=-1) return (char)(field[i][cn] + 'A'); } } return 'A'; } void removeEdge(int cn, bool pos){ dne.insert(cn + x * (int)(!pos)); if(pos){ for(int i = 0; i<y; i++){ //cout<<"upd: "<<cn<<" x "<<i<<endl; if(field[cn][i]!=-1) dp2[i][field[cn][i]]--; field[cn][i] = -1; } } else{ for(int i = 0; i<x; i++){ //cout<<"upd: "<<i<<" x "<<cn<<endl; if(field[i][cn] != -1) dp1[i][field[i][cn]]--; field[i][cn] = -1; } } } int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin>>y>>x; field.assign(x, {}); dp1.assign(x, {}); dp2.assign(y, {}); for(int i = 0; i<x; i++) dp1[i].assign(30, 0); for(int i = 0; i<y; i++) dp2[i].assign(30, 0); for(int i = 0; i<x; i++) field[i].assign(y, 0); char a; for(int i = 0; i<y; i++){ for(int j = 0; j<x; j++){ cin>>a; int val = ((int)a-(int)'A'); field[j][i] = val; dp1[j][val]++; dp2[i][val]++; } } int done = 0; while(done<x+y){ //cout<<done<<endl; for(int i = 0; i<x; i++){ if(isGood(dp1[i], i, 1)){ moves.push_back("K "+ to_string(i+1)+ " "); moves.back().push_back(getColor(i, 1)); removeEdge(i, 1); done++; } } for(int i = 0; i<y; i++){ if(isGood(dp2[i], i, 0)){ moves.push_back("R "+ to_string(i+1)+ " "); moves.back().push_back(getColor(i, 0)); removeEdge(i, 0); done++; } } } reverse(moves.begin(), moves.end()); cout<<moves.size()<<"\n"; for(string move : moves){ cout<<move<<"\n"; } return 0; } |