#include <bits/stdc++.h> using namespace std; #define int long long #define ALL(x) (x).begin(), (x).end() #define FOR(i, a, b) for (int i = a; i <= b; i++) #define FORD(i, a, b) for (int i = a; i >= b; i--) #define REP(i, n) for (int i = 0; i < n; i++) #define VI vector<int> #define PII pair<int, int> #define SZ(x) (int)((x).size()) #define PB push_back #define MP make_pair #define st first #define nd second template <class C> void mini(C &_a4, C _b4) { _a4 = min(_a4, _b4); } template <class C> void maxi(C &_a4, C _b4) { _a4 = max(_a4, _b4); } template <class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; } template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',') cerr << *sdbg++; cerr << '=' << h << ','; _dbg(sdbg + 1, a...); } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "["; for (auto &vv : V) os << vv << ","; os << "]"; return os; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr \ if (0) \ cout #endif template <class C> ostream &operator<<(ostream &os, const set<C> &s) { os << "{"; for (auto a : s) { os << a << ", "; } os << "}"; return os; } // struct Info{ // int num_r, num_k; // }; // make this easier to work with by making it an array typedef array<int, 26> Info; enum QueryType { ROW, COL }; enum ColorActionType { COLOR_R, COLOR_K }; struct ColorAction { QueryType query_type; int color_idx; int idx; }; int char2color(char c) { return c - 'A'; } optional<int> only_non_zero_color(const Info &info) { int col = -1; REP(i, 26) { if (info[i] > 0) { if(col != -1) return {}; col = i; } } if(col == -1) col = 0; return col; } int32_t main() { int n, m; cin >> n >> m; vector<string> board(n); vector<vector<bool>> done(n, vector<bool>(m)); vector<vector<bool>> done_seq(2, vector<bool>(max(n, m))); REP(i, n) { cin >> board[i]; } vector<vector<Info>> info = { vector<Info>(n), vector<Info>(m)}; REP(i, n) { REP(j, m) { int num = char2color(board[i][j]); info[ROW][i][num]++; info[COL][j][num]++; } } vector<ColorAction> singular_colors; vector<PII> t_and_sz = { {ROW, n}, {COL, m} }; for(auto p: t_and_sz){ REP(i, p.nd){ auto maybe_non_zero_color = only_non_zero_color(info[p.st][i]); if(maybe_non_zero_color.has_value()){ singular_colors.push_back({(QueryType)p.st, maybe_non_zero_color.value(), i}); done_seq[p.st][i] = true; } } } REP(_, SZ(singular_colors)) { ColorAction a = singular_colors[_]; int hidden_i = a.idx; int hidden_j = 0; int hidden_j_limit = a.query_type == ROW ? m : n; QueryType opposite_type = a.query_type == ROW ? COL : ROW; int *i, *j; if(a.query_type == ROW){ i = &hidden_i; j = &hidden_j; } else { i = &hidden_j; j = &hidden_i; } for(hidden_j = 0; hidden_j < hidden_j_limit; hidden_j++){ if(done[*i][*j] || done_seq[opposite_type][hidden_j]) continue; done[*i][*j] = true; int num = char2color(board[*i][*j]); info[opposite_type][hidden_j][num]--; auto maybe_non_zero_color = only_non_zero_color(info[opposite_type][hidden_j]); if(maybe_non_zero_color.has_value()){ singular_colors.push_back({opposite_type, maybe_non_zero_color.value(), hidden_j}); done_seq[opposite_type][hidden_j] = true; } } } reverse(ALL(singular_colors)); cout << SZ(singular_colors) << "\n"; for(auto a: singular_colors){ char query_identifier = a.query_type == ROW ? 'R' : 'K'; char color = a.color_idx + 'A'; cout << query_identifier << " " << a.idx + 1 << " " << color << "\n"; } }
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | #include <bits/stdc++.h> using namespace std; #define int long long #define ALL(x) (x).begin(), (x).end() #define FOR(i, a, b) for (int i = a; i <= b; i++) #define FORD(i, a, b) for (int i = a; i >= b; i--) #define REP(i, n) for (int i = 0; i < n; i++) #define VI vector<int> #define PII pair<int, int> #define SZ(x) (int)((x).size()) #define PB push_back #define MP make_pair #define st first #define nd second template <class C> void mini(C &_a4, C _b4) { _a4 = min(_a4, _b4); } template <class C> void maxi(C &_a4, C _b4) { _a4 = max(_a4, _b4); } template <class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; } template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',') cerr << *sdbg++; cerr << '=' << h << ','; _dbg(sdbg + 1, a...); } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "["; for (auto &vv : V) os << vv << ","; os << "]"; return os; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr \ if (0) \ cout #endif template <class C> ostream &operator<<(ostream &os, const set<C> &s) { os << "{"; for (auto a : s) { os << a << ", "; } os << "}"; return os; } // struct Info{ // int num_r, num_k; // }; // make this easier to work with by making it an array typedef array<int, 26> Info; enum QueryType { ROW, COL }; enum ColorActionType { COLOR_R, COLOR_K }; struct ColorAction { QueryType query_type; int color_idx; int idx; }; int char2color(char c) { return c - 'A'; } optional<int> only_non_zero_color(const Info &info) { int col = -1; REP(i, 26) { if (info[i] > 0) { if(col != -1) return {}; col = i; } } if(col == -1) col = 0; return col; } int32_t main() { int n, m; cin >> n >> m; vector<string> board(n); vector<vector<bool>> done(n, vector<bool>(m)); vector<vector<bool>> done_seq(2, vector<bool>(max(n, m))); REP(i, n) { cin >> board[i]; } vector<vector<Info>> info = { vector<Info>(n), vector<Info>(m)}; REP(i, n) { REP(j, m) { int num = char2color(board[i][j]); info[ROW][i][num]++; info[COL][j][num]++; } } vector<ColorAction> singular_colors; vector<PII> t_and_sz = { {ROW, n}, {COL, m} }; for(auto p: t_and_sz){ REP(i, p.nd){ auto maybe_non_zero_color = only_non_zero_color(info[p.st][i]); if(maybe_non_zero_color.has_value()){ singular_colors.push_back({(QueryType)p.st, maybe_non_zero_color.value(), i}); done_seq[p.st][i] = true; } } } REP(_, SZ(singular_colors)) { ColorAction a = singular_colors[_]; int hidden_i = a.idx; int hidden_j = 0; int hidden_j_limit = a.query_type == ROW ? m : n; QueryType opposite_type = a.query_type == ROW ? COL : ROW; int *i, *j; if(a.query_type == ROW){ i = &hidden_i; j = &hidden_j; } else { i = &hidden_j; j = &hidden_i; } for(hidden_j = 0; hidden_j < hidden_j_limit; hidden_j++){ if(done[*i][*j] || done_seq[opposite_type][hidden_j]) continue; done[*i][*j] = true; int num = char2color(board[*i][*j]); info[opposite_type][hidden_j][num]--; auto maybe_non_zero_color = only_non_zero_color(info[opposite_type][hidden_j]); if(maybe_non_zero_color.has_value()){ singular_colors.push_back({opposite_type, maybe_non_zero_color.value(), hidden_j}); done_seq[opposite_type][hidden_j] = true; } } } reverse(ALL(singular_colors)); cout << SZ(singular_colors) << "\n"; for(auto a: singular_colors){ char query_identifier = a.query_type == ROW ? 'R' : 'K'; char color = a.color_idx + 'A'; cout << query_identifier << " " << a.idx + 1 << " " << color << "\n"; } } |