HTML & JS
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pi pair<int, int>
#define F first
#define S second
#define all(c) (c).begin(), (c).end()

int tc;

signed main()
{
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    cin >> tc;
    while (tc--)
    {
        int n, m;
        cin >> n >> m;
        vector<string> a(n);
        for (auto &x : a)
            cin >> x;

        for (int j = 0; j < m; j++)
        {

            // for each col, start from bottom row

            int place = n - 1, curr_rock_count = 0;
            vi temp;
            for (int i = n - 1; i >= 0; i--)
            {
                if (a[i][j] == 'o')
                {
                    temp.push_back(curr_rock_count);
                    curr_rock_count = 0;
                }
                else if (a[i][j] == '*')
                    curr_rock_count++;
            }

            temp.push_back(curr_rock_count);

            int idx = 0;

            for (int i = n - 1; i >= 0; i--)
            {
                if (idx >= temp.size())
                    break;
                if (a[i][j] == 'o')
                {
                    idx++;
                }
                else
                {
                    if (temp[idx])
                    {
                        a[i][j] = '*';
                        temp[idx]--;
                    }
                    else
                        a[i][j] = '.';
                }
            }
        }

        for (auto x : a)
        {
            cout << x << '\n';
        }
    }

    return 0;
}

Generated URL:

Output:

No Output
Report Bug