1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>

namespace {
    using std::cin;
    using std::cout;
    using std::string;
    using std::sort;
    using std::max;
    using std::get;
    using tile_t = std::tuple<size_t, size_t, char>;
    using tile_vec_t = std::vector<tile_t>;
    using print_vec_t = std::vector<char>;
    using occupy_vec_t = std::vector<size_t>;

    constexpr char N = '.';
    constexpr char B = 'B';
    constexpr char C = 'C';
    constexpr size_t X = 0;
    constexpr size_t Y = 1;
    constexpr size_t T = 2;
    constexpr char G = 'G';
    constexpr char D = 'D';
    constexpr char L = 'L';
    constexpr char P = 'P';

    bool comp_X(tile_t const & a, tile_t const & b) {
        return get<X>(a) < get<X>(b);
    }

    bool comp_Y(tile_t const & a, tile_t const & b) {
        return get<Y>(a) < get<Y>(b);
    }

    void process_move(
            tile_vec_t & tiles, char dir,
            occupy_vec_t & occupation,
            size_t n, size_t m
    ) {
        static char prev_dir = N;

        if (dir != prev_dir) {

            for (auto & o : occupation) o = 0;

            if (dir == G || dir == D) {
                sort(tiles.begin(), tiles.end(), comp_X);
            } else if (dir == L || dir == P) {
                sort(tiles.begin(), tiles.end(), comp_Y);
            }

            for (size_t k = 0; k < tiles.size(); ++k) {
                if (dir == G)
                    get<X>(tiles[k]) = occupation[get<Y>(tiles[k])]++;
                if (dir == D)
                    get<X>(tiles[tiles.size() - 1 - k]) = n - 1
                        - occupation[get<Y>(tiles[tiles.size() - 1 - k])]++;
                if (dir == L)
                    get<Y>(tiles[k]) = occupation[get<X>(tiles[k])]++;
                if (dir == P)
                    get<Y>(tiles[tiles.size() - 1 - k]) = m - 1
                        - occupation[get<X>(tiles[tiles.size() - 1 - k])]++;
            }

            prev_dir = dir;
        }
    }
}

int main() {
    size_t n, m;
    tile_vec_t tiles = {};
    size_t k_moves;
    string moves, line;

    // Input data
    cin >> n;
    cin >> m;
    for (size_t w = 0; w < n; ++w) {
        cin >> line;
        for (size_t k = 0; k < m; ++k) {
            if (line[k] == B || line[k] == C)
                tiles.emplace_back(w, k, line[k]);
        }
    }
    cin >> k_moves;
    cin >> moves;

    // Process data
    occupy_vec_t occupation(max(n, m), 0);
    for (auto const & z : moves)
        process_move(tiles, z, occupation, n, m);

    // Print results
    print_vec_t printout(n * m, N);
    for (auto const & t : tiles)
        printout[get<X>(t) * m + get<Y>(t)] = get<T>(t);
    for (size_t w = 0; w < n; ++w) {
        for (size_t k = 0; k < m; ++k)
            cout << printout[w * m + k];
        cout << '\n';
    }

    return 0;
}