#include <bits/stdc++.h> using namespace std; typedef long long ll; struct Move{ char type; int num; char color; }; const int N = 2002; char tab[N][N]; int color[2][N][26]; //0 - row, 1 - collumn int howManyLeft[2][N]; bool isDone[N][N]; vector<Move> ans; queue<pair<int, bool>> q; char colorCheck(bool type, int idx){ for(int i = 0 ; i < 26 ; i++) if(color[type][idx][i] != 0) return i + 'A'; return 'A'; } int solve(int n, int m){ int r = 0; for(int i = 1 ; i <= n ; i++){ if(howManyLeft[0][i] == 1){ q.push({i, 0}); r++; howManyLeft[0][i] = 0; } } for(int i = 1 ; i <= m ; i++){ if(howManyLeft[1][i] == 1){ q.push({i, 1}); r++; howManyLeft[1][i] = 0; } } bool type; int idx, Size; char colorType; while(!q.empty()){ type = q.front().second; idx = q.front().first; q.pop(); colorType = colorCheck(type, idx); if(!type){ Size = m; ans.push_back({'R', idx, colorType}); } else{ Size = n; ans.push_back({'K', idx, colorType}); } /*for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= m ; j++){ printf("%d", isDone[i][j]); } printf("\n"); } printf("\n");*/ for(int i = 1 ; i <= Size ; i++){ if((!type && !isDone[idx][i]) || (type && !isDone[i][idx])){ if(!type) isDone[idx][i] = true; else isDone[i][idx] = true; color[!type][i][colorType - 'A']--; if(color[!type][i][colorType - 'A'] == 0){ howManyLeft[!type][i]--; if(howManyLeft[!type][i] == 1){ howManyLeft[!type][i] = 0; q.push({i, !type}); r++; } } } } } return r; } int main(){ int n, m; char c; scanf("%d %d\n", &n, &m); for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= m ; j++){ scanf("%c", &tab[i][j]); color[0][i][tab[i][j] - 'A']++; if(color[0][i][tab[i][j] - 'A'] == 1) howManyLeft[0][i]++; color[1][j][tab[i][j] - 'A']++; if(color[1][j][tab[i][j] - 'A'] == 1) howManyLeft[1][j]++; } scanf("\n"); } printf("%d\n", solve(n, m)); for(int i = ans.size() - 1 ; i >= 0 ; i--){ printf("%c %d %c\n", ans[i].type, ans[i].num, ans[i].color); } }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; struct Move{ char type; int num; char color; }; const int N = 2002; char tab[N][N]; int color[2][N][26]; //0 - row, 1 - collumn int howManyLeft[2][N]; bool isDone[N][N]; vector<Move> ans; queue<pair<int, bool>> q; char colorCheck(bool type, int idx){ for(int i = 0 ; i < 26 ; i++) if(color[type][idx][i] != 0) return i + 'A'; return 'A'; } int solve(int n, int m){ int r = 0; for(int i = 1 ; i <= n ; i++){ if(howManyLeft[0][i] == 1){ q.push({i, 0}); r++; howManyLeft[0][i] = 0; } } for(int i = 1 ; i <= m ; i++){ if(howManyLeft[1][i] == 1){ q.push({i, 1}); r++; howManyLeft[1][i] = 0; } } bool type; int idx, Size; char colorType; while(!q.empty()){ type = q.front().second; idx = q.front().first; q.pop(); colorType = colorCheck(type, idx); if(!type){ Size = m; ans.push_back({'R', idx, colorType}); } else{ Size = n; ans.push_back({'K', idx, colorType}); } /*for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= m ; j++){ printf("%d", isDone[i][j]); } printf("\n"); } printf("\n");*/ for(int i = 1 ; i <= Size ; i++){ if((!type && !isDone[idx][i]) || (type && !isDone[i][idx])){ if(!type) isDone[idx][i] = true; else isDone[i][idx] = true; color[!type][i][colorType - 'A']--; if(color[!type][i][colorType - 'A'] == 0){ howManyLeft[!type][i]--; if(howManyLeft[!type][i] == 1){ howManyLeft[!type][i] = 0; q.push({i, !type}); r++; } } } } } return r; } int main(){ int n, m; char c; scanf("%d %d\n", &n, &m); for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= m ; j++){ scanf("%c", &tab[i][j]); color[0][i][tab[i][j] - 'A']++; if(color[0][i][tab[i][j] - 'A'] == 1) howManyLeft[0][i]++; color[1][j][tab[i][j] - 'A']++; if(color[1][j][tab[i][j] - 'A'] == 1) howManyLeft[1][j]++; } scanf("\n"); } printf("%d\n", solve(n, m)); for(int i = ans.size() - 1 ; i >= 0 ; i--){ printf("%c %d %c\n", ans[i].type, ans[i].num, ans[i].color); } } |