#include <bits/stdc++.h> #define iamspeed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define st first #define nd second using namespace std; int n, m, q, typ[500005], optyp[500005]; pair<int, pair<int, int>> t[505][505]; vector<pair<int, int>> VV; vector<pair<pair<int, int>, int> > zmn; string s, op; char popGD, popLP; pair<int, int> mp[25][503][503]; void symulacja(int ch){ vector<pair<int, pair<int, int> > > V; if(!V.empty()) V.clear(); if(ch == 'D'){ for(int j = 1 ; j <= m ; j++){ for(int i = 1 ; i <= n ; i++){ if(t[i][j].st) V.push_back(t[i][j]); } for(int i = n ; i > 0 ; i--){ if(!V.empty()){ t[i][j] = V.back(); V.pop_back(); } else{ t[i][j] = {0, {0, 0}}; } } } } if(ch == 'G'){ for(int j = 1 ; j <= m ; j++){ for(int i = n ; i > 0 ; i--){ if(t[i][j].st) V.push_back(t[i][j]); } for(int i = 1 ; i <= n ; i++){ if(!V.empty()){ t[i][j] = V.back(); V.pop_back(); } else{ t[i][j] = {0, {0, 0}}; } } } } if(ch == 'P'){ for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= m ; j++){ if(t[i][j].st) V.push_back(t[i][j]); } for(int j = m ; j > 0 ; j--){ if(!V.empty()){ t[i][j] = V.back(); V.pop_back(); } else{ t[i][j] = {0, {0, 0}}; } } } } if(ch == 'L'){ for(int i = 1 ; i <= n ; i++){ for(int j = m ; j > 0 ; j--){ if(t[i][j].st) V.push_back(t[i][j]); } for(int j = 1 ; j <= m ; j++){ if(!V.empty()){ t[i][j] = V.back(); V.pop_back(); } else{ t[i][j] = {0, {0, 0}}; } } } } } int main(){ iamspeed; cin >> n >> m; for(int i = 1 ; i <= n ; i++){ cin >> s; for(int j = 1 ; j <= m ; j++){ if(s[j - 1] == '.') t[i][j] = {0, {0, 0}}; else if(s[j - 1] == 'B') t[i][j] = {1, {0, 0}}; else t[i][j] = {2, {0, 0}}; } } cin >> q >> s; s += 'X'; for(int i = 0 ; i < q ; i++){ if(s[i] == 'D' || s[i] == 'G') typ[i] = 1; else typ[i] = 2; } op += s[0]; optyp[0] = typ[0]; for(int i = 1 ; i < q ; i++){ int x = op.size(); if(optyp[x - 1] == typ[i] && op[x - 1] != s[i]){ op.pop_back(); if(x <= 2 || op[x - 3] != s[i]) op += s[i]; } else if(optyp[x - 1] != typ[i]){ if(x == 1 || op[x - 2] != s[i]){ op += s[i]; optyp[x] = typ[i]; } } } int k = 0; while(k < min((int)op.size(), 2)){ symulacja(op[k++]); } for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= m ; j++){ t[i][j].nd = {i, j}; } } while(k < min((int)op.size(), 6)){ symulacja(op[k++]); } for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= m ; j++){ if(t[i][j].st){ VV.push_back({i,j}); mp[0][i][j] = t[i][j].nd; } } } int maxl = 0; for(int l = 1 ; pow(2, l) <= ((int)op.size() - 6) / 4 ; l++){ maxl = l; for(int i = 0 ; i < VV.size() ; i++){ mp[l][VV[i].st][VV[i].nd] = mp[l - 1][mp[l - 1][VV[i].st][VV[i].nd].st][mp[l - 1][VV[i].st][VV[i].nd].nd]; } } int ile_skokow = ((int)op.size() - 6) / 4; while(ile_skokow > 0 && maxl >= 0){ if(pow(2, maxl) <= ile_skokow){ ile_skokow -= pow(2, maxl); if(!zmn.empty()) zmn.clear(); for(int i = 0 ; i < VV.size() ; i++){ zmn.push_back({VV[i], t[mp[maxl][VV[i].st][VV[i].nd].st][mp[maxl][VV[i].st][VV[i].nd].nd].st}); } while(!zmn.empty()){ t[zmn.back().st.st][zmn.back().st.nd] = {zmn.back().nd, {0, 0}}; zmn.pop_back(); } } maxl--; } for(int i = 6 + ((int)(((int)op.size() - 6) / 4)) * 4 ; i < op.size() ; i++){ symulacja(op[i]); } for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= m ; j++){ if(t[i][j].st == 0) cout << "."; else if(t[i][j].st == 1) cout << "B"; else cout << "C"; } cout << endl; } }
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 | #include <bits/stdc++.h> #define iamspeed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define st first #define nd second using namespace std; int n, m, q, typ[500005], optyp[500005]; pair<int, pair<int, int>> t[505][505]; vector<pair<int, int>> VV; vector<pair<pair<int, int>, int> > zmn; string s, op; char popGD, popLP; pair<int, int> mp[25][503][503]; void symulacja(int ch){ vector<pair<int, pair<int, int> > > V; if(!V.empty()) V.clear(); if(ch == 'D'){ for(int j = 1 ; j <= m ; j++){ for(int i = 1 ; i <= n ; i++){ if(t[i][j].st) V.push_back(t[i][j]); } for(int i = n ; i > 0 ; i--){ if(!V.empty()){ t[i][j] = V.back(); V.pop_back(); } else{ t[i][j] = {0, {0, 0}}; } } } } if(ch == 'G'){ for(int j = 1 ; j <= m ; j++){ for(int i = n ; i > 0 ; i--){ if(t[i][j].st) V.push_back(t[i][j]); } for(int i = 1 ; i <= n ; i++){ if(!V.empty()){ t[i][j] = V.back(); V.pop_back(); } else{ t[i][j] = {0, {0, 0}}; } } } } if(ch == 'P'){ for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= m ; j++){ if(t[i][j].st) V.push_back(t[i][j]); } for(int j = m ; j > 0 ; j--){ if(!V.empty()){ t[i][j] = V.back(); V.pop_back(); } else{ t[i][j] = {0, {0, 0}}; } } } } if(ch == 'L'){ for(int i = 1 ; i <= n ; i++){ for(int j = m ; j > 0 ; j--){ if(t[i][j].st) V.push_back(t[i][j]); } for(int j = 1 ; j <= m ; j++){ if(!V.empty()){ t[i][j] = V.back(); V.pop_back(); } else{ t[i][j] = {0, {0, 0}}; } } } } } int main(){ iamspeed; cin >> n >> m; for(int i = 1 ; i <= n ; i++){ cin >> s; for(int j = 1 ; j <= m ; j++){ if(s[j - 1] == '.') t[i][j] = {0, {0, 0}}; else if(s[j - 1] == 'B') t[i][j] = {1, {0, 0}}; else t[i][j] = {2, {0, 0}}; } } cin >> q >> s; s += 'X'; for(int i = 0 ; i < q ; i++){ if(s[i] == 'D' || s[i] == 'G') typ[i] = 1; else typ[i] = 2; } op += s[0]; optyp[0] = typ[0]; for(int i = 1 ; i < q ; i++){ int x = op.size(); if(optyp[x - 1] == typ[i] && op[x - 1] != s[i]){ op.pop_back(); if(x <= 2 || op[x - 3] != s[i]) op += s[i]; } else if(optyp[x - 1] != typ[i]){ if(x == 1 || op[x - 2] != s[i]){ op += s[i]; optyp[x] = typ[i]; } } } int k = 0; while(k < min((int)op.size(), 2)){ symulacja(op[k++]); } for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= m ; j++){ t[i][j].nd = {i, j}; } } while(k < min((int)op.size(), 6)){ symulacja(op[k++]); } for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= m ; j++){ if(t[i][j].st){ VV.push_back({i,j}); mp[0][i][j] = t[i][j].nd; } } } int maxl = 0; for(int l = 1 ; pow(2, l) <= ((int)op.size() - 6) / 4 ; l++){ maxl = l; for(int i = 0 ; i < VV.size() ; i++){ mp[l][VV[i].st][VV[i].nd] = mp[l - 1][mp[l - 1][VV[i].st][VV[i].nd].st][mp[l - 1][VV[i].st][VV[i].nd].nd]; } } int ile_skokow = ((int)op.size() - 6) / 4; while(ile_skokow > 0 && maxl >= 0){ if(pow(2, maxl) <= ile_skokow){ ile_skokow -= pow(2, maxl); if(!zmn.empty()) zmn.clear(); for(int i = 0 ; i < VV.size() ; i++){ zmn.push_back({VV[i], t[mp[maxl][VV[i].st][VV[i].nd].st][mp[maxl][VV[i].st][VV[i].nd].nd].st}); } while(!zmn.empty()){ t[zmn.back().st.st][zmn.back().st.nd] = {zmn.back().nd, {0, 0}}; zmn.pop_back(); } } maxl--; } for(int i = 6 + ((int)(((int)op.size() - 6) / 4)) * 4 ; i < op.size() ; i++){ symulacja(op[i]); } for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= m ; j++){ if(t[i][j].st == 0) cout << "."; else if(t[i][j].st == 1) cout << "B"; else cout << "C"; } cout << endl; } } |