I don’t know exactly what data structure you get the wall in, but assuming it is:
std::vector<std::vector<char>> wall;
To be able to perform melting using std::sort
, it is best to have columns in the outer vector and rows in the inner vectors.
// melting bricks
for (int col = 0; col < wall.size(); ++col) //column of bricks
{
for (int row = 0; row < wall[0].size(); ++row) //row of bricks
{
if (wall[col][row] == 'D') wall[col][row] = 0;
}
}
// applying gravity i.e. sorting
for (int col = 0; col < wall.size(); ++col) //column of bricks
{
std::sort(wall[column].begin(), wall[column].end());
}
I haven’t tested this so you may need to debug.
To find the largest square to fit in, one way is to rotate the wall and melt for each rotation, then searching for largest square, perhaps by using a while loop.
Also, it’s important to organize your code into classes e.g. Wall, Furnace, etc.
6
solved Rotate Matrix and Delete Characters and Find largest Square [closed]