Advertisement
asdfg0998

cwE

Nov 15th, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. void pushBoxes(vector<vector<char>>& board) {
  2.     int rows = board.size();
  3.     int cols = board[0].size();
  4.  
  5.     // Step 1: Push the boxes to the right
  6.     for (int i = 0; i < rows; ++i) {
  7.         int lastEmpty = cols - 1;  // The last empty position in the row
  8.         // Traverse the row from right to left
  9.         for (int j = cols - 1; j >= 0; --j) {
  10.             if (board[i][j] == '#') {  // Found a box
  11.                 board[i][j] = '-';  // Empty the original position of the box
  12.                 board[i][lastEmpty] = '#';  // Move the box to the rightmost empty position
  13.                 lastEmpty--;  // Update the last empty position
  14.             }
  15.         }
  16.     }
  17.  
  18.     // Step 2: Push the boxes down
  19.     for (int j = 0; j < cols; ++j) {
  20.         int lastEmpty = rows - 1;  // The last empty position in the column
  21.         // Traverse the column from bottom to top
  22.         for (int i = rows - 1; i >= 0; --i) {
  23.             if (board[i][j] == '#') {  // Found a box
  24.                 board[i][j] = '-';  // Empty the original position of the box
  25.                 board[lastEmpty][j] = '#';  // Move the box to the lowest available position
  26.                 lastEmpty--;  // Update the last empty position
  27.             }
  28.         }
  29.     }
  30. }
  31.  
  32. void printBoard(const vector<vector<char>>& board) {
  33.     for (const auto& row : board) {
  34.         for (char cell : row) {
  35.             cout << cell << ' ';
  36.         }
  37.         cout << endl;
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement