Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vector<vector<char>> pushAndFall(vector<vector<char>>& board) {
- int rows = board.size();
- int cols = board[0].size();
- // Step 1: Push boxes to the right
- for (int i = 0; i < rows; i++) {
- for (int j = cols - 1; j >= 0; j--) {
- if (board[i][j] == '#') {
- int k = j;
- while (k + 1 < cols && board[i][k + 1] == '-') {
- swap(board[i][k], board[i][k + 1]);
- k++;
- }
- }
- }
- }
- // Step 2: Push boxes down
- for (int j = 0; j < cols; j++) {
- for (int i = rows - 1; i >= 0; i--) {
- if (board[i][j] == '#') {
- int k = i;
- while (k + 1 < rows && board[k + 1][j] == '-') {
- swap(board[k][j], board[k + 1][j]);
- k++;
- }
- }
- }
- }
- return board;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement