Untitled
unknown
c_cpp
9 months ago
1.1 kB
7
Indexable
vector<vector<char>> applyGravity(const vector<vector<char>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();
vector<vector<char>> result = matrix;
int maxDrop = rows; // Start with the maximum possible drop
// Step 1: Determine how far the entire F block can fall
for (int col = 0; col < cols; ++col) {
int obstacleRow = rows; // Position of the nearest obstacle below
for (int row = rows - 1; row >= 0; --row) {
if (result[row][col] == '#') {
obstacleRow = row; // Update nearest obstacle position
} else if (result[row][col] == 'F') {
maxDrop = min(maxDrop, obstacleRow - row - 1); // How far this F can fall
}
}
}
// Step 2: Move the F block down by maxDrop
for (int row = rows - 1; row >= 0; --row) {
for (int col = 0; col < cols; ++col) {
if (result[row][col] == 'F') {
result[row][col] = '_'; // Clear old position
result[row + maxDrop][col] = 'F'; // Move down
}
}
}
return result;
}Editor is loading...
Leave a Comment