Advertisement
asdfg0998

cwdcwe

Nov 15th, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. vector<vector<char>> pushAndFall(vector<vector<char>>& board) {
  2.     int rows = board.size();
  3.     int cols = board[0].size();
  4.  
  5.     // Step 1: Push boxes to the right
  6.     for (int i = 0; i < rows; i++) {
  7.         for (int j = cols - 1; j >= 0; j--) {
  8.             if (board[i][j] == '#') {
  9.                 int k = j;
  10.                 while (k + 1 < cols && board[i][k + 1] == '-') {
  11.                     swap(board[i][k], board[i][k + 1]);
  12.                     k++;
  13.                 }
  14.             }
  15.         }
  16.     }
  17.  
  18.     // Step 2: Push boxes down
  19.     for (int j = 0; j < cols; j++) {
  20.         for (int i = rows - 1; i >= 0; i--) {
  21.             if (board[i][j] == '#') {
  22.                 int k = i;
  23.                 while (k + 1 < rows && board[k + 1][j] == '-') {
  24.                     swap(board[k][j], board[k + 1][j]);
  25.                     k++;
  26.                 }
  27.             }
  28.         }
  29.     }
  30.  
  31.     return board;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement