#include<bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<string>grid(n); for(auto &x: grid)cin >> x; vector<map<char,int>>cnt_col(m); vector<map<char,int>>cnt_row(n); for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ cnt_col[j][grid[i][j]]++; cnt_row[i][grid[i][j]]++; } } vector<pair<char, pair<int, char>>>ans; queue<pair<char, pair<int, char>>>q; map<pair<char, int>, bool>vis; for(int i = 0; i < m; i++){ if(cnt_col[i].size() == 1){ for(auto x : cnt_col[i])q.push({'K', {i, x.first}}); } } for(int i = 0; i < n; i++){ if(cnt_row[i].size() == 1){ for(auto x : cnt_row[i])q.push({'R', {i, x.first}}); } } while(!q.empty()){ auto x = q.front(); q.pop(); if(vis[{x.first, x.second.first}])continue; vis[{x.first, x.second.first}] = true; ans.push_back({x.first, {x.second.first + 1, x.second.second}}); if(x.first == 'K'){ int j = x.second.first; for(int i = 0; i < n; i++){ if(grid[i][j] == 'x')continue; cnt_row[i][grid[i][j]]--; if(cnt_row[i][grid[i][j]] == 0){ cnt_row[i].erase(cnt_row[i].find(grid[i][j])); } if(cnt_row[i].size() == 1){ for(auto v : cnt_row[i])q.push({'R', {i, v.first}}); } grid[i][j] = 'x'; } } if(x.first == 'R'){ int i = x.second.first; for(int j = 0; j < m; j++){ if(grid[i][j] == 'x')continue; cnt_col[j][grid[i][j]]--; if(cnt_col[j][grid[i][j]] == 0){ cnt_col[j].erase(cnt_col[j].find(grid[i][j])); } if(cnt_col[j].size() == 1){ for(auto v : cnt_col[j])q.push({'K', {j, v.first}}); } grid[i][j] = 'x'; } } } cout << ans.size() << '\n'; reverse(ans.begin(), ans.end()); for(auto x : ans){ cout << x.first << ' ' << x.second.first << ' ' << x.second.second << '\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 | #include<bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<string>grid(n); for(auto &x: grid)cin >> x; vector<map<char,int>>cnt_col(m); vector<map<char,int>>cnt_row(n); for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ cnt_col[j][grid[i][j]]++; cnt_row[i][grid[i][j]]++; } } vector<pair<char, pair<int, char>>>ans; queue<pair<char, pair<int, char>>>q; map<pair<char, int>, bool>vis; for(int i = 0; i < m; i++){ if(cnt_col[i].size() == 1){ for(auto x : cnt_col[i])q.push({'K', {i, x.first}}); } } for(int i = 0; i < n; i++){ if(cnt_row[i].size() == 1){ for(auto x : cnt_row[i])q.push({'R', {i, x.first}}); } } while(!q.empty()){ auto x = q.front(); q.pop(); if(vis[{x.first, x.second.first}])continue; vis[{x.first, x.second.first}] = true; ans.push_back({x.first, {x.second.first + 1, x.second.second}}); if(x.first == 'K'){ int j = x.second.first; for(int i = 0; i < n; i++){ if(grid[i][j] == 'x')continue; cnt_row[i][grid[i][j]]--; if(cnt_row[i][grid[i][j]] == 0){ cnt_row[i].erase(cnt_row[i].find(grid[i][j])); } if(cnt_row[i].size() == 1){ for(auto v : cnt_row[i])q.push({'R', {i, v.first}}); } grid[i][j] = 'x'; } } if(x.first == 'R'){ int i = x.second.first; for(int j = 0; j < m; j++){ if(grid[i][j] == 'x')continue; cnt_col[j][grid[i][j]]--; if(cnt_col[j][grid[i][j]] == 0){ cnt_col[j].erase(cnt_col[j].find(grid[i][j])); } if(cnt_col[j].size() == 1){ for(auto v : cnt_col[j])q.push({'K', {j, v.first}}); } grid[i][j] = 'x'; } } } cout << ans.size() << '\n'; reverse(ans.begin(), ans.end()); for(auto x : ans){ cout << x.first << ' ' << x.second.first << ' ' << x.second.second << '\n'; } return 0; } |