#include <algorithm> #include <cassert> #include <deque> #include <iostream> #include <limits> #include <unordered_set> #include <utility> #include <vector> using Board = std::vector<std::string>; template <typename Collection> void printCollection(const Collection& collection) { for (int i = 0; i < collection.size(); i++) { std::cout << collection[i]; if (i < collection.size() - 1) { std::cout << " "; } } std::cout << "\n"; } void printBoard(const Board& board) { for (const auto& row : board) { for (const auto tile : row) { std::cout << tile; } std::cout << "\n"; } } // rows in range <1;500> // cols in range <1;500> // moves string size in range <1;300000> std::pair<Board, std::string> getInput() { int rows; int cols; std::cin >> rows; std::cin >> cols; Board b(rows, std::string(cols, '.')); for (int i = 0; i < rows; ++i) { std::cin >> b[i]; } std::cin >> rows; std::string moves_values; std::cin >> moves_values; return std::make_pair(b, moves_values); // std::vector<int> v(n); // for (int i = 0; i < n; i++) { // std::cin >> v[i]; // } // return v; // return {{'.', '.', '.', '.', '.'}, // {'.', 'B', '.', 'C', '.'}, // {'.', '.', 'C', '.', '.'}, // {'.', '.', '.', 'B', '.'}}; } std::pair<int, int> countFullRowsAndCols(Board& board) { const int rows = board.size(); const int cols = board.size() > 0 ? board[0].size() : 0; std::unordered_set<int> unfull_rows; std::unordered_set<int> unfull_cols; for (int row = 0; row < board.size(); ++row) { for (int col = 0; col < board[row].size(); ++col) { if (board[row][col] == '.') { unfull_rows.insert(row); unfull_cols.insert(col); } } } return std::make_pair(rows - unfull_rows.size(), cols - unfull_cols.size()); } void moveColUp(Board& board, const int col, const int full_cols, const char previous_left_right_move) { // check values for each column starting from the top int empty_idx{0}; for (int current_row = 0; current_row < board.size(); current_row++) { char& current_tile = board[current_row][col]; if (current_tile != '.') { std::swap(current_tile, board[empty_idx][col]); ++empty_idx; } } } void moveColDown(Board& board, const int col, const int full_cols, const char previous_left_right_move) { // check values for each column starting from the bottom int empty_idx{static_cast<int>(board.size()) - 1}; for (int current_row = board.size() - 1; current_row >= 0; current_row--) { char& current_tile = board[current_row][col]; if (current_tile != '.') { std::swap(current_tile, board[empty_idx][col]); --empty_idx; } } } void moveUpDown(Board& board, const char move_dir, const int full_cols, const char previous_left_right_move) { const int cols = board.size() > 0 ? board[0].size() : 0; for (int col = 0; col < cols; ++col) { if (((previous_left_right_move == 'L' && col < full_cols) || (previous_left_right_move == 'P' && col >= cols - full_cols))) { continue; } switch (move_dir) { case 'G': moveColUp(board, col, full_cols, previous_left_right_move); break; case 'D': moveColDown(board, col, full_cols, previous_left_right_move); break; } } } void moveRowLeft(Board& board, const int row, const int full_rows, const char previous_up_down_move) { // check values for each row starting from the left int empty_idx{0}; for (int current_col = 0; current_col < board[row].size(); current_col++) { char& current_tile = board[row][current_col]; if (current_tile != '.') { std::swap(current_tile, board[row][empty_idx]); ++empty_idx; } } } void moveRowRight(Board& board, const int row, const int full_rows, const char previous_up_down_move) { // check values for each row starting from the right int empty_idx{static_cast<int>(board[row].size()) - 1}; for (int current_col = board[row].size() - 1; current_col >= 0; current_col--) { char& current_tile = board[row][current_col]; if (current_tile != '.') { std::swap(current_tile, board[row][empty_idx]); --empty_idx; } } } void moveLeftRight(Board& board, const char move_dir, const int full_rows, const char previous_up_down_move) { for (int row = 0; row < board.size(); ++row) { if (((previous_up_down_move == 'G' && row < full_rows) || (previous_up_down_move == 'D' && row >= board.size() - full_rows))) { continue; } switch (move_dir) { case 'L': moveRowLeft(board, row, full_rows, previous_up_down_move); break; case 'P': moveRowRight(board, row, full_rows, previous_up_down_move); break; } } } void move(Board& board, const char move_dir) { static char previous_left_right_move = ' '; static char previous_up_down_move = ' '; if (previous_up_down_move != ' ' && previous_left_right_move != ' ') { static std::pair<int, int> rows_cols = countFullRowsAndCols(board); switch (move_dir) { case 'G': case 'D': moveUpDown(board, move_dir, rows_cols.second, previous_left_right_move); previous_up_down_move = move_dir; break; case 'L': case 'P': moveLeftRight(board, move_dir, rows_cols.first, previous_up_down_move); previous_left_right_move = move_dir; break; } } else { switch (move_dir) { case 'G': case 'D': moveUpDown(board, move_dir, 0, previous_left_right_move); previous_up_down_move = move_dir; break; case 'L': case 'P': moveLeftRight(board, move_dir, 0, previous_up_down_move); previous_left_right_move = move_dir; break; } } } std::string sanitizeMoves(const std::string& moves) { std::string result; auto isOpposite = [](const char element, const char next) { switch (element) { case 'G': return next == 'D'; case 'D': return next == 'G'; case 'L': return next == 'P'; case 'P': return next == 'L'; } return false; }; char previous_up_down = ' '; char previous_left_right = ' '; for (int i = 0; i < moves.size(); ++i) { if (i < moves.size() - 1 && moves[i] == moves[i + 1]) { continue; } if (i < moves.size() - 2 && (isOpposite(moves[i], moves[i + 1]) && moves[i] == moves[i + 2])) { ++i; continue; } if (moves[i] == previous_up_down || moves[i] == previous_left_right) { continue; } result += moves[i]; if (moves[i] == 'G' || moves[i] == 'D') { previous_up_down = moves[i]; } else if (moves[i] == 'L' || moves[i] == 'P') { previous_left_right = moves[i]; } } return result; } int main() { // input auto [board, moves] = getInput(); const auto sanitized = sanitizeMoves(moves); const auto sanitized2 = sanitizeMoves(sanitized); for (int i = 0; i < sanitized2.size(); ++i) { move(board, sanitized2[i]); } // output printBoard(board); }
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | #include <algorithm> #include <cassert> #include <deque> #include <iostream> #include <limits> #include <unordered_set> #include <utility> #include <vector> using Board = std::vector<std::string>; template <typename Collection> void printCollection(const Collection& collection) { for (int i = 0; i < collection.size(); i++) { std::cout << collection[i]; if (i < collection.size() - 1) { std::cout << " "; } } std::cout << "\n"; } void printBoard(const Board& board) { for (const auto& row : board) { for (const auto tile : row) { std::cout << tile; } std::cout << "\n"; } } // rows in range <1;500> // cols in range <1;500> // moves string size in range <1;300000> std::pair<Board, std::string> getInput() { int rows; int cols; std::cin >> rows; std::cin >> cols; Board b(rows, std::string(cols, '.')); for (int i = 0; i < rows; ++i) { std::cin >> b[i]; } std::cin >> rows; std::string moves_values; std::cin >> moves_values; return std::make_pair(b, moves_values); // std::vector<int> v(n); // for (int i = 0; i < n; i++) { // std::cin >> v[i]; // } // return v; // return {{'.', '.', '.', '.', '.'}, // {'.', 'B', '.', 'C', '.'}, // {'.', '.', 'C', '.', '.'}, // {'.', '.', '.', 'B', '.'}}; } std::pair<int, int> countFullRowsAndCols(Board& board) { const int rows = board.size(); const int cols = board.size() > 0 ? board[0].size() : 0; std::unordered_set<int> unfull_rows; std::unordered_set<int> unfull_cols; for (int row = 0; row < board.size(); ++row) { for (int col = 0; col < board[row].size(); ++col) { if (board[row][col] == '.') { unfull_rows.insert(row); unfull_cols.insert(col); } } } return std::make_pair(rows - unfull_rows.size(), cols - unfull_cols.size()); } void moveColUp(Board& board, const int col, const int full_cols, const char previous_left_right_move) { // check values for each column starting from the top int empty_idx{0}; for (int current_row = 0; current_row < board.size(); current_row++) { char& current_tile = board[current_row][col]; if (current_tile != '.') { std::swap(current_tile, board[empty_idx][col]); ++empty_idx; } } } void moveColDown(Board& board, const int col, const int full_cols, const char previous_left_right_move) { // check values for each column starting from the bottom int empty_idx{static_cast<int>(board.size()) - 1}; for (int current_row = board.size() - 1; current_row >= 0; current_row--) { char& current_tile = board[current_row][col]; if (current_tile != '.') { std::swap(current_tile, board[empty_idx][col]); --empty_idx; } } } void moveUpDown(Board& board, const char move_dir, const int full_cols, const char previous_left_right_move) { const int cols = board.size() > 0 ? board[0].size() : 0; for (int col = 0; col < cols; ++col) { if (((previous_left_right_move == 'L' && col < full_cols) || (previous_left_right_move == 'P' && col >= cols - full_cols))) { continue; } switch (move_dir) { case 'G': moveColUp(board, col, full_cols, previous_left_right_move); break; case 'D': moveColDown(board, col, full_cols, previous_left_right_move); break; } } } void moveRowLeft(Board& board, const int row, const int full_rows, const char previous_up_down_move) { // check values for each row starting from the left int empty_idx{0}; for (int current_col = 0; current_col < board[row].size(); current_col++) { char& current_tile = board[row][current_col]; if (current_tile != '.') { std::swap(current_tile, board[row][empty_idx]); ++empty_idx; } } } void moveRowRight(Board& board, const int row, const int full_rows, const char previous_up_down_move) { // check values for each row starting from the right int empty_idx{static_cast<int>(board[row].size()) - 1}; for (int current_col = board[row].size() - 1; current_col >= 0; current_col--) { char& current_tile = board[row][current_col]; if (current_tile != '.') { std::swap(current_tile, board[row][empty_idx]); --empty_idx; } } } void moveLeftRight(Board& board, const char move_dir, const int full_rows, const char previous_up_down_move) { for (int row = 0; row < board.size(); ++row) { if (((previous_up_down_move == 'G' && row < full_rows) || (previous_up_down_move == 'D' && row >= board.size() - full_rows))) { continue; } switch (move_dir) { case 'L': moveRowLeft(board, row, full_rows, previous_up_down_move); break; case 'P': moveRowRight(board, row, full_rows, previous_up_down_move); break; } } } void move(Board& board, const char move_dir) { static char previous_left_right_move = ' '; static char previous_up_down_move = ' '; if (previous_up_down_move != ' ' && previous_left_right_move != ' ') { static std::pair<int, int> rows_cols = countFullRowsAndCols(board); switch (move_dir) { case 'G': case 'D': moveUpDown(board, move_dir, rows_cols.second, previous_left_right_move); previous_up_down_move = move_dir; break; case 'L': case 'P': moveLeftRight(board, move_dir, rows_cols.first, previous_up_down_move); previous_left_right_move = move_dir; break; } } else { switch (move_dir) { case 'G': case 'D': moveUpDown(board, move_dir, 0, previous_left_right_move); previous_up_down_move = move_dir; break; case 'L': case 'P': moveLeftRight(board, move_dir, 0, previous_up_down_move); previous_left_right_move = move_dir; break; } } } std::string sanitizeMoves(const std::string& moves) { std::string result; auto isOpposite = [](const char element, const char next) { switch (element) { case 'G': return next == 'D'; case 'D': return next == 'G'; case 'L': return next == 'P'; case 'P': return next == 'L'; } return false; }; char previous_up_down = ' '; char previous_left_right = ' '; for (int i = 0; i < moves.size(); ++i) { if (i < moves.size() - 1 && moves[i] == moves[i + 1]) { continue; } if (i < moves.size() - 2 && (isOpposite(moves[i], moves[i + 1]) && moves[i] == moves[i + 2])) { ++i; continue; } if (moves[i] == previous_up_down || moves[i] == previous_left_right) { continue; } result += moves[i]; if (moves[i] == 'G' || moves[i] == 'D') { previous_up_down = moves[i]; } else if (moves[i] == 'L' || moves[i] == 'P') { previous_left_right = moves[i]; } } return result; } int main() { // input auto [board, moves] = getInput(); const auto sanitized = sanitizeMoves(moves); const auto sanitized2 = sanitizeMoves(sanitized); for (int i = 0; i < sanitized2.size(); ++i) { move(board, sanitized2[i]); } // output printBoard(board); } |