#include <iostream> #include <algorithm> #include <vector> #include <stack> #include <string> using namespace std; #define MAX 509 #define DB if(0) #define PR if(0) #define RND if (0) string s[MAX], comp[MAX]; int horCount = 0; int verCount = 0; stack<char> st; int n, m; bool isVertical(char val) { return val == 'G' || val == 'D'; } bool isHorizontal(char val) { return val == 'L' || val == 'P'; } void push(char val) { if (st.size() >= 2) { int top = st.top(); st.pop(); int sec = st.top(); st.push(top); if (val == sec) return; } if (isVertical(val)) verCount++; if (isHorizontal(val)) horCount++; st.push(val); } void pop() { char val = st.top(); if (isVertical(val)) verCount--; if (isHorizontal(val)) horCount--; st.pop(); } void moveVertical(bool down) { int start = down ? n - 1 : 0; int end = down ? -1 : n; int step = down ? -1 : 1; for (int i=0; i<m; i++) { int cur = start; for (int j=start; j * step < end * step; j += step) { if (s[j][i] != '.') { int pom = s[j][i]; s[j][i] = '.'; s[cur][i] = pom; cur += step; } } } } void moveHorizontal(bool right) { int start = right ? m - 1 : 0; int end = right ? -1 : m; int step = right ? -1 : 1; for (int i=0; i<n; i++) { int cur = start; for (int j=start; j * step < end * step; j += step) { if (s[i][j] != '.') { int pom = s[i][j]; s[i][j] = '.'; s[i][cur] = pom; cur += step; } } } } void print() { for (int i=0; i<n; i++) cout << s[i] << "\n"; cout << "\n"; } void save() { for (int i=0; i<n; i++) comp[i] = s[i]; } bool compare() { // print(); for (int i=0; i<n; i++) if (comp[i] != s[i]) { // cout << comp[i] << " != " << s[i] << "\n"; return false; } return true; } int main() { cin >> n >> m; for (int i=0; i<n; i++) { cin >> s[i]; } int movesCount; cin >> movesCount; string moves; cin >> moves; for (char move : moves) { // push(move); if (st.empty()) push(move); else { char top = st.top(); if (top == move) continue; if ((isHorizontal(move) && isHorizontal(top)) || (isVertical(move) && isVertical(top))) { pop(); if (isVertical(move) && !verCount) push(move); if (isHorizontal(move) && !horCount) push(move); } else { push(move); } } } // print(); stack<char> finalMoves; while (st.size()) { finalMoves.push(st.top()); // cout << st.top(); st.pop(); } // cout << "\n"; int saveAfter = 3, round = 0, cleared = false; while (finalMoves.size()) { char val = finalMoves.top(); finalMoves.pop(); // cout << "apply move " << val << "\n"; if (isHorizontal(val)) { moveHorizontal(val == 'P'); } else if (isVertical(val)) { moveVertical(val == 'D'); } if (!saveAfter) { save(); round = 0; // cout << "Saved:\n"; // print(); } else if (saveAfter < 0 && !cleared) { if (compare()) { while (st.size() > round) { for (int i=0; i<round; i++) st.pop(); } cleared = true; // cout <<"ELO MORDO! " << round << " " << st.size() << "\n"; // return 0; } } round++; saveAfter--; // print(); } print(); }
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 | #include <iostream> #include <algorithm> #include <vector> #include <stack> #include <string> using namespace std; #define MAX 509 #define DB if(0) #define PR if(0) #define RND if (0) string s[MAX], comp[MAX]; int horCount = 0; int verCount = 0; stack<char> st; int n, m; bool isVertical(char val) { return val == 'G' || val == 'D'; } bool isHorizontal(char val) { return val == 'L' || val == 'P'; } void push(char val) { if (st.size() >= 2) { int top = st.top(); st.pop(); int sec = st.top(); st.push(top); if (val == sec) return; } if (isVertical(val)) verCount++; if (isHorizontal(val)) horCount++; st.push(val); } void pop() { char val = st.top(); if (isVertical(val)) verCount--; if (isHorizontal(val)) horCount--; st.pop(); } void moveVertical(bool down) { int start = down ? n - 1 : 0; int end = down ? -1 : n; int step = down ? -1 : 1; for (int i=0; i<m; i++) { int cur = start; for (int j=start; j * step < end * step; j += step) { if (s[j][i] != '.') { int pom = s[j][i]; s[j][i] = '.'; s[cur][i] = pom; cur += step; } } } } void moveHorizontal(bool right) { int start = right ? m - 1 : 0; int end = right ? -1 : m; int step = right ? -1 : 1; for (int i=0; i<n; i++) { int cur = start; for (int j=start; j * step < end * step; j += step) { if (s[i][j] != '.') { int pom = s[i][j]; s[i][j] = '.'; s[i][cur] = pom; cur += step; } } } } void print() { for (int i=0; i<n; i++) cout << s[i] << "\n"; cout << "\n"; } void save() { for (int i=0; i<n; i++) comp[i] = s[i]; } bool compare() { // print(); for (int i=0; i<n; i++) if (comp[i] != s[i]) { // cout << comp[i] << " != " << s[i] << "\n"; return false; } return true; } int main() { cin >> n >> m; for (int i=0; i<n; i++) { cin >> s[i]; } int movesCount; cin >> movesCount; string moves; cin >> moves; for (char move : moves) { // push(move); if (st.empty()) push(move); else { char top = st.top(); if (top == move) continue; if ((isHorizontal(move) && isHorizontal(top)) || (isVertical(move) && isVertical(top))) { pop(); if (isVertical(move) && !verCount) push(move); if (isHorizontal(move) && !horCount) push(move); } else { push(move); } } } // print(); stack<char> finalMoves; while (st.size()) { finalMoves.push(st.top()); // cout << st.top(); st.pop(); } // cout << "\n"; int saveAfter = 3, round = 0, cleared = false; while (finalMoves.size()) { char val = finalMoves.top(); finalMoves.pop(); // cout << "apply move " << val << "\n"; if (isHorizontal(val)) { moveHorizontal(val == 'P'); } else if (isVertical(val)) { moveVertical(val == 'D'); } if (!saveAfter) { save(); round = 0; // cout << "Saved:\n"; // print(); } else if (saveAfter < 0 && !cleared) { if (compare()) { while (st.size() > round) { for (int i=0; i<round; i++) st.pop(); } cleared = true; // cout <<"ELO MORDO! " << round << " " << st.size() << "\n"; // return 0; } } round++; saveAfter--; // print(); } print(); } |