#include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; int n, m, k; void shift(char c, vector<string>& v) { if (c == 'L') { for (int i = 0; i < n; i++) { v[i].erase(remove(v[i].begin(), v[i].end(), '.'), v[i].end()); int len = v[i].length(); for (int j = 0; j < m - len; j++) v[i] += '.'; } return; } if (c == 'P') { for (int i = 0; i < n; i++) { v[i].erase(remove(v[i].begin(), v[i].end(), '.'), v[i].end()); int len = v[i].length(); v[i].insert(0, m - len, '.'); } return; } if (c == 'G') { for (int j = 0; j < m; j++) { string s = ""; for (int i = 0; i < n; i++) { s += v[i][j]; s.erase(remove(s.begin(), s.end(), '.'), s.end()); int len = s.length(); for (int j = 0; j < n - len; j++) s += '.'; } for (int i = 0; i < n; i++) { v[i][j] = s[i]; } } return; } if (c == 'D') { for (int j = 0; j < m; j++) { string s = ""; for (int i = 0; i < n; i++) { s += v[i][j]; s.erase(remove(s.begin(), s.end(), '.'), s.end()); int len = s.length(); s.insert(0, n - len, '.'); } for (int i = 0; i < n; i++) { v[n-i-1][j] = s[n-i-1]; } } return; } } void removeFromString(string& s, string p, string r = ""){ std::string::size_type i = s.find(p); while (i != std::string::npos) { s.erase(i, p.length()); if(r != "") s.insert(i,r); i = s.find(p, i); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; vector<string> v; string s; char c; for (int i = 0; i < n; i++) { s = ""; for (int j = 0; j < m; j++) { cin >> c; s += c; } v.push_back(s); } cin >> k; cin >> s; string prev; for(int i = 0; i < s.length() - 1; i++){ if(s[i] == 'P'){ for(int j = i+1; j < s.length(); j++){ if(s[j] == 'L'){ //s.erase(i,1); break; } if(s[j] == 'P'){ s.erase(j,1); break; } } } } for(int i = 0; i < s.length() - 1; i++){ if(s[i] == 'D'){ for(int j = i+1; j < s.length(); j++){ if(s[j] == 'G'){ //s.erase(i,1); break; } if(s[j] == 'D'){ s.erase(j,1); break; } } } } for(int i = 0; i < s.length() - 1; i++){ if(s[i] == 'G'){ for(int j = i+1; j < s.length(); j++){ if(s[j] == 'D'){ //s.erase(i,1); break; } if(s[j] == 'G'){ s.erase(j,1); break; } } } } for(int i = 0; i < s.length() - 1; i++){ if(s[i] == 'L'){ for(int j = i+1; j < s.length(); j++){ if(s[j] == 'P'){ //s.erase(i,1); break; } if(s[j] == 'L'){ s.erase(j,1); break; } } } } while(true){ prev = s; removeFromString(s,"LL","L"); removeFromString(s,"PP","P"); removeFromString(s,"DD","D"); removeFromString(s,"GG","G"); removeFromString(s,"LP","P"); removeFromString(s,"PL","L"); removeFromString(s,"GD","D"); removeFromString(s,"DG","G"); removeFromString(s,"LDLD","LD"); removeFromString(s,"PDPD","PD"); removeFromString(s,"LGLG","LG"); removeFromString(s,"PGPG","PG"); // // removeFromString(s,"GLDP","DP"); // removeFromString(s,"DPGL","GL"); // removeFromString(s,"GPDL","DL"); // removeFromString(s,"DLGP","GP"); // removeFromString(s,"PDLG","LG"); //G nadpisuje D, L nadpisuje P if(s == prev) break; } // // cout<<endl<<s<<endl; for (int i = 0; i < s.length(); i++) shift(s[i], v); for (int i = 0; i < n; i++) cout << v[i] << '\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 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 186 187 188 189 190 191 192 | #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; int n, m, k; void shift(char c, vector<string>& v) { if (c == 'L') { for (int i = 0; i < n; i++) { v[i].erase(remove(v[i].begin(), v[i].end(), '.'), v[i].end()); int len = v[i].length(); for (int j = 0; j < m - len; j++) v[i] += '.'; } return; } if (c == 'P') { for (int i = 0; i < n; i++) { v[i].erase(remove(v[i].begin(), v[i].end(), '.'), v[i].end()); int len = v[i].length(); v[i].insert(0, m - len, '.'); } return; } if (c == 'G') { for (int j = 0; j < m; j++) { string s = ""; for (int i = 0; i < n; i++) { s += v[i][j]; s.erase(remove(s.begin(), s.end(), '.'), s.end()); int len = s.length(); for (int j = 0; j < n - len; j++) s += '.'; } for (int i = 0; i < n; i++) { v[i][j] = s[i]; } } return; } if (c == 'D') { for (int j = 0; j < m; j++) { string s = ""; for (int i = 0; i < n; i++) { s += v[i][j]; s.erase(remove(s.begin(), s.end(), '.'), s.end()); int len = s.length(); s.insert(0, n - len, '.'); } for (int i = 0; i < n; i++) { v[n-i-1][j] = s[n-i-1]; } } return; } } void removeFromString(string& s, string p, string r = ""){ std::string::size_type i = s.find(p); while (i != std::string::npos) { s.erase(i, p.length()); if(r != "") s.insert(i,r); i = s.find(p, i); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; vector<string> v; string s; char c; for (int i = 0; i < n; i++) { s = ""; for (int j = 0; j < m; j++) { cin >> c; s += c; } v.push_back(s); } cin >> k; cin >> s; string prev; for(int i = 0; i < s.length() - 1; i++){ if(s[i] == 'P'){ for(int j = i+1; j < s.length(); j++){ if(s[j] == 'L'){ //s.erase(i,1); break; } if(s[j] == 'P'){ s.erase(j,1); break; } } } } for(int i = 0; i < s.length() - 1; i++){ if(s[i] == 'D'){ for(int j = i+1; j < s.length(); j++){ if(s[j] == 'G'){ //s.erase(i,1); break; } if(s[j] == 'D'){ s.erase(j,1); break; } } } } for(int i = 0; i < s.length() - 1; i++){ if(s[i] == 'G'){ for(int j = i+1; j < s.length(); j++){ if(s[j] == 'D'){ //s.erase(i,1); break; } if(s[j] == 'G'){ s.erase(j,1); break; } } } } for(int i = 0; i < s.length() - 1; i++){ if(s[i] == 'L'){ for(int j = i+1; j < s.length(); j++){ if(s[j] == 'P'){ //s.erase(i,1); break; } if(s[j] == 'L'){ s.erase(j,1); break; } } } } while(true){ prev = s; removeFromString(s,"LL","L"); removeFromString(s,"PP","P"); removeFromString(s,"DD","D"); removeFromString(s,"GG","G"); removeFromString(s,"LP","P"); removeFromString(s,"PL","L"); removeFromString(s,"GD","D"); removeFromString(s,"DG","G"); removeFromString(s,"LDLD","LD"); removeFromString(s,"PDPD","PD"); removeFromString(s,"LGLG","LG"); removeFromString(s,"PGPG","PG"); // // removeFromString(s,"GLDP","DP"); // removeFromString(s,"DPGL","GL"); // removeFromString(s,"GPDL","DL"); // removeFromString(s,"DLGP","GP"); // removeFromString(s,"PDLG","LG"); //G nadpisuje D, L nadpisuje P if(s == prev) break; } // // cout<<endl<<s<<endl; for (int i = 0; i < s.length(); i++) shift(s[i], v); for (int i = 0; i < n; i++) cout << v[i] << '\n'; return 0; } |