#include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> // macros #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d", &n); #define SC2(n, m) scanf("%d%d", &n, &m); #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; const int NMAX = 2005; int n, m, k; char t[NMAX][NMAX]; vector<string> ans; mi rows[NMAX], cols[NMAX]; queue<int> row_still, col_still; char extract(const mi& mapa) { if (mapa.empty()) return 'A'; if (mapa.size() > 1) return '!'; return (char) mapa.begin()->xx; } int main() { SC2(n,m); REP(i, n) { scanf("%s", t[i]); /* int still = 1; FORE(j, 1, m-1) if (t[i][0] != t[i][j]) still = 0; if (still) row_still.push(i);*/ } /* REP(j, m) { int still = 1; FORE(i, 1, n-1) if (t[0][j] != t[i][j]) still = 0; if (still) col_still.push(i); } */ REP(i, n) REP(j, m) { int color = t[i][j]; rows[i][color]++; cols[j][color]++; } REP(i, n) if (rows[i].size() == 1) { row_still.push(i); prd("push row %d\n", i+1); } REP(j, m) if (cols[j].size() == 1) { col_still.push(j); prd("push col %d\n", j+1); } dbg REP(i,n) { REP(j, m) printf("%c", t[i][j]); prd("\n"); } int cells = n * m; while (cells) { if (!col_still.empty()) { int j = col_still.front(); col_still.pop(); string order ="K " + to_string(j+1) + " " + extract(cols[j]); ans.pb(order); prd("%s\n", order.c_str()); REP(i,n) if (t[i][j]) { cells--; int color = t[i][j]; t[i][j] = 0; rows[i][color]--; if (rows[i][color] == 0) { rows[i].erase(color); if (rows[i].size() == 1) { row_still.push(i); prd("push row %d\n", i+1); } } } } if (!row_still.empty()) { int i = row_still.front(); row_still.pop(); string order = "R " + to_string(i+1) + " " + extract(rows[i]); ans.pb(order); prd("%s\n", order.c_str()); REP(j,m) if (t[i][j]) { cells--; char color = t[i][j]; t[i][j] = 0; cols[j][color]--; if (cols[j][color] == 0) { cols[j].erase(color); if (cols[j].size() == 1) { col_still.push(j); prd("push col %d\n", j+1); } } } } } reverse(ALL(ans)); printf("%d\n", (int) ans.size()); REP(i, ans.size()) printf("%s\n", ans[i].c_str()); 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | #include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> // macros #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d", &n); #define SC2(n, m) scanf("%d%d", &n, &m); #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; const int NMAX = 2005; int n, m, k; char t[NMAX][NMAX]; vector<string> ans; mi rows[NMAX], cols[NMAX]; queue<int> row_still, col_still; char extract(const mi& mapa) { if (mapa.empty()) return 'A'; if (mapa.size() > 1) return '!'; return (char) mapa.begin()->xx; } int main() { SC2(n,m); REP(i, n) { scanf("%s", t[i]); /* int still = 1; FORE(j, 1, m-1) if (t[i][0] != t[i][j]) still = 0; if (still) row_still.push(i);*/ } /* REP(j, m) { int still = 1; FORE(i, 1, n-1) if (t[0][j] != t[i][j]) still = 0; if (still) col_still.push(i); } */ REP(i, n) REP(j, m) { int color = t[i][j]; rows[i][color]++; cols[j][color]++; } REP(i, n) if (rows[i].size() == 1) { row_still.push(i); prd("push row %d\n", i+1); } REP(j, m) if (cols[j].size() == 1) { col_still.push(j); prd("push col %d\n", j+1); } dbg REP(i,n) { REP(j, m) printf("%c", t[i][j]); prd("\n"); } int cells = n * m; while (cells) { if (!col_still.empty()) { int j = col_still.front(); col_still.pop(); string order ="K " + to_string(j+1) + " " + extract(cols[j]); ans.pb(order); prd("%s\n", order.c_str()); REP(i,n) if (t[i][j]) { cells--; int color = t[i][j]; t[i][j] = 0; rows[i][color]--; if (rows[i][color] == 0) { rows[i].erase(color); if (rows[i].size() == 1) { row_still.push(i); prd("push row %d\n", i+1); } } } } if (!row_still.empty()) { int i = row_still.front(); row_still.pop(); string order = "R " + to_string(i+1) + " " + extract(rows[i]); ans.pb(order); prd("%s\n", order.c_str()); REP(j,m) if (t[i][j]) { cells--; char color = t[i][j]; t[i][j] = 0; cols[j][color]--; if (cols[j][color] == 0) { cols[j].erase(color); if (cols[j].size() == 1) { col_still.push(j); prd("push col %d\n", j+1); } } } } } reverse(ALL(ans)); printf("%d\n", (int) ans.size()); REP(i, ans.size()) printf("%s\n", ans[i].c_str()); return 0; } |