#include <bits/stdc++.h> using namespace std; struct ruch { char dir, col; int val; }; struct fullRC { int rc; // 1 - row, 2 - column int which; }; vector <ruch> result; int board[2005][2005]; int rows[2005][30]; int cols[2005][30]; stack <fullRC> stos; int n, m; const int ACTIVE = 28; // active void checkActive() { for (int i = 1; i <= n; i++) { int how_many_not_zero = 0; for (int j = 0; j <= 'Z' - 'A'; j++) { if (rows[i][j] != 0) how_many_not_zero++; } if (how_many_not_zero <= 1 && rows[i][ACTIVE] == 0) { rows[i][ACTIVE] = 1; stos.push({1, i}); } } for (int i = 1; i <= m; i++) { int how_many_not_zero = 0; for (int j = 0; j <= 'Z' - 'A'; j++) { if (cols[i][j] != 0) how_many_not_zero++; } if (how_many_not_zero <= 1 && cols[i][ACTIVE] == 0) { cols[i][ACTIVE] = 1; stos.push({2,i}); } } } void clearRow(int r) { for (int i = 1; i <= m; i++) { int c = board[r][i]; if (c != 0) { cols[i][c - 'A']--; board[r][i] = 0; } } } void clearCol(int r) { for (int i = 1; i <= n; i++) { int c = board[i][r]; if (c != 0) { rows[i][c - 'A']--; board[i][r] = 0; } } } int getRow(int r) { for (int i = 1; i <= m; i++) { int c = board[r][i]; if (c != 0) return c; } return 0; } int getCol(int r) { for (int i = 1; i <= n; i++) { int c = board[i][r]; if (c != 0) return c; } return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { string S; cin >> S; for (int j = 1; j <= m; j++) { board[i][j] = (int)S[j-1]; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int which = board[i][j] - 'A'; rows[i][which]++; cols[j][which]++; } } checkActive(); while (!stos.empty()) { auto x = stos.top(); stos.pop(); int colour = 0; if (x.rc == 1) { colour = getRow(x.which); if (colour == 0) break; clearRow(x.which); result.push_back({'R', (char)colour, x.which}); } else { colour = getCol(x.which); if (colour == 0) break; clearCol(x.which); result.push_back({'K', (char)colour, x.which}); } checkActive(); } cout << result.size() << "\n"; for (int i = result.size()-1; i >=0; i--) { cout << result[i].dir << " " << result[i].val << " " << result[i].col << "\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 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 | #include <bits/stdc++.h> using namespace std; struct ruch { char dir, col; int val; }; struct fullRC { int rc; // 1 - row, 2 - column int which; }; vector <ruch> result; int board[2005][2005]; int rows[2005][30]; int cols[2005][30]; stack <fullRC> stos; int n, m; const int ACTIVE = 28; // active void checkActive() { for (int i = 1; i <= n; i++) { int how_many_not_zero = 0; for (int j = 0; j <= 'Z' - 'A'; j++) { if (rows[i][j] != 0) how_many_not_zero++; } if (how_many_not_zero <= 1 && rows[i][ACTIVE] == 0) { rows[i][ACTIVE] = 1; stos.push({1, i}); } } for (int i = 1; i <= m; i++) { int how_many_not_zero = 0; for (int j = 0; j <= 'Z' - 'A'; j++) { if (cols[i][j] != 0) how_many_not_zero++; } if (how_many_not_zero <= 1 && cols[i][ACTIVE] == 0) { cols[i][ACTIVE] = 1; stos.push({2,i}); } } } void clearRow(int r) { for (int i = 1; i <= m; i++) { int c = board[r][i]; if (c != 0) { cols[i][c - 'A']--; board[r][i] = 0; } } } void clearCol(int r) { for (int i = 1; i <= n; i++) { int c = board[i][r]; if (c != 0) { rows[i][c - 'A']--; board[i][r] = 0; } } } int getRow(int r) { for (int i = 1; i <= m; i++) { int c = board[r][i]; if (c != 0) return c; } return 0; } int getCol(int r) { for (int i = 1; i <= n; i++) { int c = board[i][r]; if (c != 0) return c; } return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { string S; cin >> S; for (int j = 1; j <= m; j++) { board[i][j] = (int)S[j-1]; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int which = board[i][j] - 'A'; rows[i][which]++; cols[j][which]++; } } checkActive(); while (!stos.empty()) { auto x = stos.top(); stos.pop(); int colour = 0; if (x.rc == 1) { colour = getRow(x.which); if (colour == 0) break; clearRow(x.which); result.push_back({'R', (char)colour, x.which}); } else { colour = getCol(x.which); if (colour == 0) break; clearCol(x.which); result.push_back({'K', (char)colour, x.which}); } checkActive(); } cout << result.size() << "\n"; for (int i = result.size()-1; i >=0; i--) { cout << result[i].dir << " " << result[i].val << " " << result[i].col << "\n"; } return 0; } |