#include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator<<(auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator<<(auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << __LINE__ << ": [" #x "]: ", dump(x) #else #define debug(...) 0 #endif int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, m; cin>>n>>m; vector<string> tab(n); rep(i, n)cin>>tab[i]; vector<int> rowCnt(n, 0); vector<int> colCnt(m, 0); vector<int> prevRow(n, 0); vector<int> nextRow(n, 0); vector<int> prevCol(m, 0); vector<int> nextCol(m, 0); vector<bool> colActive(m, true); vector<bool> rowActive(n, true); prevRow[0] = -1; for(int i = 1; i < n; i++){ prevRow[i] = i-1; } nextRow[n-1] = -1; for(int i= 0; i < n-1; i++){ nextRow[i] = i+1; } prevCol[0] = -1; for(int i = 1; i < m; i++){ prevCol[i] = i-1; } nextCol[m-1] = -1; for(int i= 0; i < m-1; i++){ nextCol[i] = i+1; } rep(i, n){ rep(j, m-1){ if(tab[i][j] != tab[i][j+1])rowCnt[i]++; } } rep(j, m){ rep(i, n-1){ if(tab[i][j] != tab[i+1][j])colCnt[j]++; } } debug(colCnt); debug(rowCnt); queue<int> q; vector<pair<int, char>> ans; rep(i, n){ if(rowCnt[i] == 0)q.push((i+1)); } rep(i, m){ if(colCnt[i] == 0)q.push(-(i+1)); } while(!q.empty()){ int v = q.front(); q.pop(); debug(v); if(v > 0){ int row = v-1; char c = 0; int a = prevRow[row]; int b = nextRow[row]; rep(j, m){ if(colActive[j])c = tab[row][j]; if(colCnt[j] != 0){ if(a != -1 && tab[a][j] != tab[row][j]){ colCnt[j]--; } if(b != -1 && tab[b][j] != tab[row][j]){ colCnt[j]--; } if(a != -1 && b != -1 && tab[a][j] != tab[b][j])colCnt[j]++; if(colCnt[j] == 0){ q.push(-(j+1)); } } } if(a != -1)nextRow[a] = b; if(b != -1)prevRow[b] = a; if(c != 0)ans.push_back({v, c}); rowActive[row] = false; }else{ int col = -v - 1; char c = 0; int a = prevCol[col]; int b = nextCol[col]; rep(i, n){ if(rowActive[i])c = tab[i][col]; if(rowCnt[i] != 0){ if(a != -1 && tab[i][a] != tab[i][col]){ rowCnt[i]--; } if(b != -1 && tab[i][b] != tab[i][col]){ rowCnt[i]--; } if(a != -1 && b != -1 && tab[i][a] != tab[i][b])rowCnt[i]++; if(rowCnt[i] == 0){ q.push(i + 1); } } } if(a != -1)nextCol[a] = b; if(b != -1)prevCol[b] = a; if(c != 0)ans.push_back({v, c}); colActive[col] = false; } } reverse(all(ans)); cout<<sz(ans)<<"\n"; for(auto p : ans){ if(p.st > 0){ cout<<"R "<<p.st<<" "<<p.nd<<"\n"; }else{ cout<<"K "<<(-p.st)<<" "<<p.nd<<"\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 142 143 144 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator<<(auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator<<(auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << __LINE__ << ": [" #x "]: ", dump(x) #else #define debug(...) 0 #endif int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, m; cin>>n>>m; vector<string> tab(n); rep(i, n)cin>>tab[i]; vector<int> rowCnt(n, 0); vector<int> colCnt(m, 0); vector<int> prevRow(n, 0); vector<int> nextRow(n, 0); vector<int> prevCol(m, 0); vector<int> nextCol(m, 0); vector<bool> colActive(m, true); vector<bool> rowActive(n, true); prevRow[0] = -1; for(int i = 1; i < n; i++){ prevRow[i] = i-1; } nextRow[n-1] = -1; for(int i= 0; i < n-1; i++){ nextRow[i] = i+1; } prevCol[0] = -1; for(int i = 1; i < m; i++){ prevCol[i] = i-1; } nextCol[m-1] = -1; for(int i= 0; i < m-1; i++){ nextCol[i] = i+1; } rep(i, n){ rep(j, m-1){ if(tab[i][j] != tab[i][j+1])rowCnt[i]++; } } rep(j, m){ rep(i, n-1){ if(tab[i][j] != tab[i+1][j])colCnt[j]++; } } debug(colCnt); debug(rowCnt); queue<int> q; vector<pair<int, char>> ans; rep(i, n){ if(rowCnt[i] == 0)q.push((i+1)); } rep(i, m){ if(colCnt[i] == 0)q.push(-(i+1)); } while(!q.empty()){ int v = q.front(); q.pop(); debug(v); if(v > 0){ int row = v-1; char c = 0; int a = prevRow[row]; int b = nextRow[row]; rep(j, m){ if(colActive[j])c = tab[row][j]; if(colCnt[j] != 0){ if(a != -1 && tab[a][j] != tab[row][j]){ colCnt[j]--; } if(b != -1 && tab[b][j] != tab[row][j]){ colCnt[j]--; } if(a != -1 && b != -1 && tab[a][j] != tab[b][j])colCnt[j]++; if(colCnt[j] == 0){ q.push(-(j+1)); } } } if(a != -1)nextRow[a] = b; if(b != -1)prevRow[b] = a; if(c != 0)ans.push_back({v, c}); rowActive[row] = false; }else{ int col = -v - 1; char c = 0; int a = prevCol[col]; int b = nextCol[col]; rep(i, n){ if(rowActive[i])c = tab[i][col]; if(rowCnt[i] != 0){ if(a != -1 && tab[i][a] != tab[i][col]){ rowCnt[i]--; } if(b != -1 && tab[i][b] != tab[i][col]){ rowCnt[i]--; } if(a != -1 && b != -1 && tab[i][a] != tab[i][b])rowCnt[i]++; if(rowCnt[i] == 0){ q.push(i + 1); } } } if(a != -1)nextCol[a] = b; if(b != -1)prevCol[b] = a; if(c != 0)ans.push_back({v, c}); colActive[col] = false; } } reverse(all(ans)); cout<<sz(ans)<<"\n"; for(auto p : ans){ if(p.st > 0){ cout<<"R "<<p.st<<" "<<p.nd<<"\n"; }else{ cout<<"K "<<(-p.st)<<" "<<p.nd<<"\n"; } } return 0; } |