Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void pushBoxes(vector<vector<char>>& board) {
- int rows = board.size();
- int cols = board[0].size();
- // Step 1: Push the boxes to the right
- for (int i = 0; i < rows; ++i) {
- int lastEmpty = cols - 1; // The last empty position in the row
- // Traverse the row from right to left
- for (int j = cols - 1; j >= 0; --j) {
- if (board[i][j] == '#') { // Found a box
- board[i][j] = '-'; // Empty the original position of the box
- board[i][lastEmpty] = '#'; // Move the box to the rightmost empty position
- lastEmpty--; // Update the last empty position
- }
- }
- }
- // Step 2: Push the boxes down
- for (int j = 0; j < cols; ++j) {
- int lastEmpty = rows - 1; // The last empty position in the column
- // Traverse the column from bottom to top
- for (int i = rows - 1; i >= 0; --i) {
- if (board[i][j] == '#') { // Found a box
- board[i][j] = '-'; // Empty the original position of the box
- board[lastEmpty][j] = '#'; // Move the box to the lowest available position
- lastEmpty--; // Update the last empty position
- }
- }
- }
- }
- void printBoard(const vector<vector<char>>& board) {
- for (const auto& row : board) {
- for (char cell : row) {
- cout << cell << ' ';
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement