#include <iostream> #include <vector> #include <algorithm> #include <map> #include <cmath> #include <unordered_map> #include <string> #include <queue> #include <deque> #include <set> #include <cstdlib> #include <ctime> #include<chrono> #include<thread> #include<iomanip> #include<fstream> #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second using namespace std; typedef struct ans { char orient; int num; char element; }; int main() { int n, m; cin >> n >> m; map <char, int> hor[n], ver[m]; bool verhor = false; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { char a; cin >> a; hor[i][a]++; ver[j][a]++; if(i == n - 1 && ver[j].size() == 1) verhor = true; } } bool flag = true; vector <ans> answer; while(flag) { flag = false; if(verhor) { for(int j = 0; j < m; j++) { if(ver[j].size() == 1) { auto it = ver[j].begin(); answer.pb({'K', j + 1, it->first}); for(int i = 0; i < n; i++) { if(hor[i].count(it->first)) hor[i][it->first]--; if(hor[i][it->first] == 0) hor[i].erase(it->first); } ver[j].clear(); flag = true; } } verhor = !verhor; } else { for(int i = 0; i < n; i++) { if(hor[i].size() == 1) { auto it = hor[i].begin(); answer.pb({'R', i + 1, it->first}); for(int j = 0; j < m; j++) { if(ver[j].count(it->first)) ver[j][it->first]--; if(ver[j][it->first] == 0) ver[j].erase(it->first); } hor[i].clear(); flag = true; } } verhor = !verhor; } } reverse(answer.begin(), answer.end()); cout << answer.size() << endl; for(auto i : answer) { cout << i.orient << " " << i.num << " " << i.element << endl; } 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 | #include <iostream> #include <vector> #include <algorithm> #include <map> #include <cmath> #include <unordered_map> #include <string> #include <queue> #include <deque> #include <set> #include <cstdlib> #include <ctime> #include<chrono> #include<thread> #include<iomanip> #include<fstream> #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second using namespace std; typedef struct ans { char orient; int num; char element; }; int main() { int n, m; cin >> n >> m; map <char, int> hor[n], ver[m]; bool verhor = false; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { char a; cin >> a; hor[i][a]++; ver[j][a]++; if(i == n - 1 && ver[j].size() == 1) verhor = true; } } bool flag = true; vector <ans> answer; while(flag) { flag = false; if(verhor) { for(int j = 0; j < m; j++) { if(ver[j].size() == 1) { auto it = ver[j].begin(); answer.pb({'K', j + 1, it->first}); for(int i = 0; i < n; i++) { if(hor[i].count(it->first)) hor[i][it->first]--; if(hor[i][it->first] == 0) hor[i].erase(it->first); } ver[j].clear(); flag = true; } } verhor = !verhor; } else { for(int i = 0; i < n; i++) { if(hor[i].size() == 1) { auto it = hor[i].begin(); answer.pb({'R', i + 1, it->first}); for(int j = 0; j < m; j++) { if(ver[j].count(it->first)) ver[j][it->first]--; if(ver[j][it->first] == 0) ver[j].erase(it->first); } hor[i].clear(); flag = true; } } verhor = !verhor; } } reverse(answer.begin(), answer.end()); cout << answer.size() << endl; for(auto i : answer) { cout << i.orient << " " << i.num << " " << i.element << endl; } return 0; } |