#include<bits/stdc++.h> using namespace std; int row[2005][26]; int col[2005][26]; int diff_row[2005]; int diff_col[2005]; char tab[2005][2005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int a, b; cin>>a>>b; for(int x = 1; x <= a; x++) for(int y = 1; y <= b; y++) { char c; cin>>c; tab[x][y] = c; row[x][c - 'A']++; if(row[x][c - 'A'] == 1) diff_row[x]++; col[y][c - 'A']++; if(col[y][c - 'A'] == 1) diff_col[y]++; } queue<pair<char, int>> q; for(int x = 1; x <= a; x++) if(diff_row[x] == 1) q.push(make_pair('R', x)); for(int x = 1; x <= b; x++) if(diff_col[x] == 1) q.push(make_pair('K', x)); vector<pair<char, pair<int, char>>> out; while(!q.empty()) { auto p = q.front(); q.pop(); //cout<<"XD "<<p.first<<" "<<p.second<<'\n'; if(p.first == 'R') { int lit = 0; for(int x = 0; x < 26; x++) if(row[p.second][x]) lit = x; out.push_back({p.first, {p.second, 'A' + lit}}); for(int y = 1; y <= b; y++) { if(tab[p.second][y] != '0') if(--col[y][tab[p.second][y] - 'A'] == 0) if(--diff_col[y] == 1) q.push({'K', y}); } } else { int lit = 0; for(int x = 0; x < 26; x++) if(col[p.second][x]) lit = x; out.push_back({p.first, {p.second, 'A' + lit}}); for(int y = 1; y <= a; y++) { if(tab[y][p.second] != '0') if(--row[y][tab[y][p.second] - 'A'] == 0) if(--diff_row[y] == 1) q.push({'R', y}); } } } reverse(out.begin(), out.end()); cout<<out.size()<<'\n'; for(auto p : out) cout<<p.first<<" "<<p.second.first<<" "<<p.second.second<<'\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 | #include<bits/stdc++.h> using namespace std; int row[2005][26]; int col[2005][26]; int diff_row[2005]; int diff_col[2005]; char tab[2005][2005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int a, b; cin>>a>>b; for(int x = 1; x <= a; x++) for(int y = 1; y <= b; y++) { char c; cin>>c; tab[x][y] = c; row[x][c - 'A']++; if(row[x][c - 'A'] == 1) diff_row[x]++; col[y][c - 'A']++; if(col[y][c - 'A'] == 1) diff_col[y]++; } queue<pair<char, int>> q; for(int x = 1; x <= a; x++) if(diff_row[x] == 1) q.push(make_pair('R', x)); for(int x = 1; x <= b; x++) if(diff_col[x] == 1) q.push(make_pair('K', x)); vector<pair<char, pair<int, char>>> out; while(!q.empty()) { auto p = q.front(); q.pop(); //cout<<"XD "<<p.first<<" "<<p.second<<'\n'; if(p.first == 'R') { int lit = 0; for(int x = 0; x < 26; x++) if(row[p.second][x]) lit = x; out.push_back({p.first, {p.second, 'A' + lit}}); for(int y = 1; y <= b; y++) { if(tab[p.second][y] != '0') if(--col[y][tab[p.second][y] - 'A'] == 0) if(--diff_col[y] == 1) q.push({'K', y}); } } else { int lit = 0; for(int x = 0; x < 26; x++) if(col[p.second][x]) lit = x; out.push_back({p.first, {p.second, 'A' + lit}}); for(int y = 1; y <= a; y++) { if(tab[y][p.second] != '0') if(--row[y][tab[y][p.second] - 'A'] == 0) if(--diff_row[y] == 1) q.push({'R', y}); } } } reverse(out.begin(), out.end()); cout<<out.size()<<'\n'; for(auto p : out) cout<<p.first<<" "<<p.second.first<<" "<<p.second.second<<'\n'; return 0; } |