#include <bits/stdc++.h> #include <ranges> using namespace std; #define loop(i, a, b) for(int i = a; i <= b; i++) #define loop_rev(i, a, b) for(int i = a; i >= b; i--) #define all(x) x.begin(), x.end() #define sz(x) int(x.size()) #define eb emplace_back #define pb push_back struct Event { char type; int index; char letter; }; int main() { cin.tie(0)->sync_with_stdio(false); int n, m; cin >> n >> m; auto resize_vec = [&]<typename T>(vector<vector<T>>& vec, T val = T()) { vec.resize(n+2, vector<T>(m+2, val)); }; vector<vector<char>> ma; vector<vector<int>> col_l, col_r, row_l, row_r; resize_vec(ma, '#'); resize_vec(col_l), resize_vec(col_r); resize_vec(row_l), resize_vec(row_r); loop(i, 1, n) loop(j, 1, m) cin >> ma[i][j]; vector<int> row_cnt(n+1); vector<int> col_cnt(m+1); loop(i, 1, n) { loop(j, 1, m) { if(ma[i][j] != ma[i][j-1]) row_cnt[i]++; if(ma[i][j] != ma[i-1][j]) col_cnt[j]++; } } loop(i, 1, n) { loop(j, 1, m) { col_l[i][j] = j-1; col_r[i][j] = j+1; row_l[i][j] = i-1; row_r[i][j] = i+1; } } vector<Event> res; bool success = 1; while(success) { success = 0; loop(i, 1, n) { if(row_cnt[i] == 1) { loop(j, 1, m) { if(ma[i][j] != '#') { res.eb('R', i, ma[i][j]); break; } } loop(j, 1, m) { int im = row_l[i][j], ip = row_r[i][j]; if(ma[im][j] != ma[i][j] && ma[i][j] != ma[ip][j]) col_cnt[j] -= 1; if(ma[im][j] == ma[ip][j] && ma[im][j] != '#' && ma[i][j] != ma[im][j]) col_cnt[j] -= 1; row_r[im][j] = row_r[i][j]; row_l[ip][j] = row_l[i][j]; ma[i][j] = '#'; } row_cnt[i] = 0, success = 1; break; } } if(success) continue; loop(j, 1, m) { if(col_cnt[j] == 1) { loop(i, 1, n) { if(ma[i][j] != '#') { res.eb('K', j, ma[i][j]); break; } } loop(i, 1, n) { int jm = col_l[i][j], jp = col_r[i][j]; if(ma[i][jm] != ma[i][j] && ma[i][j] != ma[i][jp]) row_cnt[i] -= 1; if(ma[i][jm] == ma[i][jp] && ma[i][jm] != '#' && ma[i][j] != ma[i][jm]) row_cnt[i] -= 1; col_r[i][jm] = col_r[i][j]; col_l[i][jp] = col_l[i][j]; ma[i][j] = '#'; } col_cnt[j] = 0, success = 1; break; } } } cout << sz(res) << '\n'; ranges::reverse(res); for(auto const& r : res) { cout << r.type << ' ' << r.index << ' ' << r.letter << '\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 | #include <bits/stdc++.h> #include <ranges> using namespace std; #define loop(i, a, b) for(int i = a; i <= b; i++) #define loop_rev(i, a, b) for(int i = a; i >= b; i--) #define all(x) x.begin(), x.end() #define sz(x) int(x.size()) #define eb emplace_back #define pb push_back struct Event { char type; int index; char letter; }; int main() { cin.tie(0)->sync_with_stdio(false); int n, m; cin >> n >> m; auto resize_vec = [&]<typename T>(vector<vector<T>>& vec, T val = T()) { vec.resize(n+2, vector<T>(m+2, val)); }; vector<vector<char>> ma; vector<vector<int>> col_l, col_r, row_l, row_r; resize_vec(ma, '#'); resize_vec(col_l), resize_vec(col_r); resize_vec(row_l), resize_vec(row_r); loop(i, 1, n) loop(j, 1, m) cin >> ma[i][j]; vector<int> row_cnt(n+1); vector<int> col_cnt(m+1); loop(i, 1, n) { loop(j, 1, m) { if(ma[i][j] != ma[i][j-1]) row_cnt[i]++; if(ma[i][j] != ma[i-1][j]) col_cnt[j]++; } } loop(i, 1, n) { loop(j, 1, m) { col_l[i][j] = j-1; col_r[i][j] = j+1; row_l[i][j] = i-1; row_r[i][j] = i+1; } } vector<Event> res; bool success = 1; while(success) { success = 0; loop(i, 1, n) { if(row_cnt[i] == 1) { loop(j, 1, m) { if(ma[i][j] != '#') { res.eb('R', i, ma[i][j]); break; } } loop(j, 1, m) { int im = row_l[i][j], ip = row_r[i][j]; if(ma[im][j] != ma[i][j] && ma[i][j] != ma[ip][j]) col_cnt[j] -= 1; if(ma[im][j] == ma[ip][j] && ma[im][j] != '#' && ma[i][j] != ma[im][j]) col_cnt[j] -= 1; row_r[im][j] = row_r[i][j]; row_l[ip][j] = row_l[i][j]; ma[i][j] = '#'; } row_cnt[i] = 0, success = 1; break; } } if(success) continue; loop(j, 1, m) { if(col_cnt[j] == 1) { loop(i, 1, n) { if(ma[i][j] != '#') { res.eb('K', j, ma[i][j]); break; } } loop(i, 1, n) { int jm = col_l[i][j], jp = col_r[i][j]; if(ma[i][jm] != ma[i][j] && ma[i][j] != ma[i][jp]) row_cnt[i] -= 1; if(ma[i][jm] == ma[i][jp] && ma[i][jm] != '#' && ma[i][j] != ma[i][jm]) row_cnt[i] -= 1; col_r[i][jm] = col_r[i][j]; col_l[i][jp] = col_l[i][j]; ma[i][j] = '#'; } col_cnt[j] = 0, success = 1; break; } } } cout << sz(res) << '\n'; ranges::reverse(res); for(auto const& r : res) { cout << r.type << ' ' << r.index << ' ' << r.letter << '\n'; } } |