Problem:
Problem F:
Mysterious Maze
A robot in a two-dimensional maze again. The maze has an entrance and an exit this time, though.
Just as in the previous problem, the maze is made up of
H
×
W
grid cells, its upper side faces north, and each cell is either empty or wall. Unlike the previous, on the other hand, one of the empty cells is connected to the entrance and another to the exit.
The robot is rather complex - there is some control, but not full. It is associated with a controller that has two buttons, namely
forward
and
turn
. The forward button moves the robot forward to the next cell, if possible. The robot can not move into a wall nor outside the maze. The turn button turns the robot as
programmed
. Here the program is a finite sequence of
N
commands, each of which is either 'L' (indicating a left turn) or 'R' (a right turn). The first turn follows the first command; the second turn follows the second command; similar for the following turns. The turn button stops working once the commands are exhausted; the forward button still works in such a case though. The robot always turns by 90 degrees at once.
The robot is initially put on the entrance cell, directed to the north. Your mission is to determine whether it can reach the exit cell if controlled properly.
Input
The input is a sequence of datasets. Each dataset is formatted as follows.
H W N
s
1
...
s
N
c
1,1
c
1,2
...
c
1,
W
...
c
H
,1
c
H
,2
...
c
H
,
W
The first line of a dataset contains three integers
H
,
W
and
N
(1 ≤
H
,
W
≤ 1,000, 1 ≤
N
≤ 1,000,000).
The second line contains a program of
N
commands.
Each of the following
H
lines contains exactly
W
characters. Each of these characters represents a cell of the maze. "
.
" indicates empty, "
#
" indicates a wall, "
S
" indicates an entrance, and "
G
" indicates an exit. There is exactly one entrance cell and one exit cell.
The end of input is indicated by a line with three zeros.
Output
For each dataset, output whether the robot can reach the exit in a line: "
Yes
" if it can or "
No
" otherwise (without quotes).
Sample Input
2 2 1
L
G.
#S
2 2 2
RR
G.
.S
3 3 6
LLLLLL
G#.
...
.#S
0 0 0
Output for the Sample Input
Yes
No
Yes

Submitted solutions:

code_snippet: #include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include <queue>
//#define debug 1
using namespace std;

const int maxn = 1010;
const int inf = (1<<30);

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

struct node
{
    int x, y;
    node(int i = 0, int j = 0)
    {
        x = i, y = j;
    }
};

char s[1000010];
int g[maxn][maxn], dis[maxn][maxn], cost[1000010][4], dir[1000010];
bool vis[maxn][maxn];
int W,H,N;
queue<node>Q;

void init()
{
    memset(dis, -1, sizeof(dis));
    memset(g, 0, sizeof(g));
    memset(vis, 0, sizeof(vis));

    dir[0] = 0;
    for(int i = 1; i <= N; ++ i)
        if(s[i] == 'L') dir[i] = (dir[i-1]+3)%4;
        else dir[i] = (dir[i-1]+1)%4;

    for(int i = N; i >= 0; -- i)
        for(int j = 0; j < 4; ++ j)
        {
            if(dir[i] == j) cost[i][j] = 0;
            else
            {
                if(i == N) cost[i][j] = inf;
                else cost[i][j] = cost[i+1][j] +1;
            }
        }
    for(int i = 0; i < 4; ++ i) cost[N+1][i] = inf;

    while(!Q.empty()) Q.pop();
}

bool solve(int sx, int sy, int ex, int ey)
{
    Q.push(node(sx, sy));
    dis[sx][sy] = 0;
    while(!Q.empty())
    {
        node u = Q.front();
        Q.pop();
        int now = dis[u.x][u.y];
        for(int i = 0; i < 4; ++ i)
        {
            int nx = u.x+dx[i], ny = u.y+dy[i];
            if(g[nx][ny] && cost[now][i] < inf)
            {
                if(nx == ex && ny == ey) return true;
                int tem = now+cost[now][i];
                if(dis[nx][ny] == -1 || tem < dis[nx][ny])
                {
                    dis[nx][ny] = tem;
                    Q.push(node(nx, ny));
                }
            }
        }
    }
    return false;
}

void show()
{
    for(int i = 0; i <= W+1; ++ i)
    {
        for(int j = 0; j <= H+1; ++ j)
            printf("%d ", g[i][j]);
        puts("");
    }
}

int main()
{
    while(scanf("%d %d %d", &W, &H, &N) ==3 && W+H+N)
    {
        scanf("%s", s+1);
        init();
        int sx, sy, ex, ey;
        for(int i = 1; i <= W; ++ i)
        {
            scanf("%s", s+1);
            for(int j = 1; j <= H; ++ j)
            {
                if(s[j] != '#') g[i][j] = 1;
                if(s[j] == 'S') sx = i, sy = j;
                if(s[j] == 'G') ex = i, ey = j;
            }
        }
       // show();
        if(solve(sx, sy, ex, ey)) puts("Yes");
        else puts("No");
    }
    return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 14136
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
const int inf = 1 << 28;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int h, w, n; cin >> h >> w >> n;

    while(h) {
        string t; cin >> t;
        vector<vector<int>> nx(n + 1, vector(4, inf));
        for(int i = n - 1; i >= 0; i --) {
            if(t[i] == 'R') {
                for(int j = 0; j < 4; j ++) {
                    nx[i][(j + 1) % 4] = nx[i + 1][j]; 
                }
                nx[i][1] = i + 1;
            } else {
                for(int j = 0; j < 4; j ++) {
                    nx[i][j] = nx[i + 1][(j + 1) % 4]; 
                }
                nx[i][3] = i + 1;
            }
        }
        vector<string> s(h);
        int sy, sx, gy, gx;
        rep(i, h) {
            cin >> s[i];
            rep(j, w) {
                if(s[i][j] == 'S') {
                    sy = i;
                    sx = j;
                    s[i][j] = '.';
                }
                if(s[i][j] == 'G') {
                    gy = i;
                    gx = j;
                    s[i][j] = '.';
                }
            }
        }
        
        vector dis(h, vector(w, vector(4, inf)));
        // using P = tuple<int, int, int, int>;
        // priority_queue<P, vector<P>, greater<>> ms;
        vector<vector<tuple<int, int, int>>> v(n + 1);
        // ms.push({0, sy, sx, 0});
        v[0].push_back({sy, sx, 0});
        dis[sy][sx][0] = 0;
        for(int phase = 0; phase <= n; phase ++) {
            while(!v[phase].empty()) {
                auto [i, j, state] = v[phase].back();
                v[phase].pop_back();
                if(i == gy and j == gx) continue;
                if(phase > dis[i][j][state]) continue;
                {
                    int y = i + dy[state];
                    int x = j + dx[state];
                    if(x >= 0 and y >= 0 and x < w and y < h and s[y][x] == '.' and dis[y][x][state] > phase) {
                        dis[y][x][state] = phase;
                        v[phase].push_back({y, x, state});
                    }
                }
                for(int k = 1; k <= 3; k ++) {
                    int nxstate = (state + k) & 3;
                    int cost = nx[phase][k];
                    if(cost == inf) continue;
                    if(dis[i][j][nxstate] > cost) {
                        dis[i][j][nxstate] = cost;
                        v[cost].push_back({i, j, nxstate});
                    }
                }
            }
        }
            
        int ans = inf;
        rep(i, 4) ans = min(ans, dis[gy][gx][i]);
        cout << (ans == inf ? "No" : "Yes") << endl;
        cin >> h >> w >> n;
    }
    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 92592
final_rank: 19

code_snippet: // AOJ #2325 Mysterious Maze
// 2025.3.14

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    while(true){
        int H,W; ll NN;
        cin >> H >> W >> NN;
        if(H == 0) break;
        string prog; cin >> prog;
        vector<string> g(H);
        for(int i=0;i<H;i++) cin >> g[i];
        int sx, sy, gx, gy;
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                if(g[i][j]=='S'){ sx = i; sy = j; }
                if(g[i][j]=='G'){ gx = i; gy = j; }
            }
        }

        vector<vector<int>> Nn(H,vector<int>(W)), Ss(H,vector<int>(W));
        vector<vector<int>> Ww(H,vector<int>(W)), Ee(H,vector<int>(W));
        for(int j=0;j<W;j++){
            for(int i=0;i<H;){
                if(g[i][j]=='#'){ i++; continue; }
                int st = i;
                while(i<H && g[i][j]!='#') i++;
                int ed = i-1;
                for(int k=st; k<=ed; k++){
                    Nn[k][j] = st;
                    Ss[k][j] = ed;
                }
            }
        }
        for(int i=0;i<H;i++){
            for(int j=0;j<W;){
                if(g[i][j]=='#'){ j++; continue; }
                int st = j;
                while(j<W && g[i][j]!='#') j++;
                int ed = j-1;
                for(int k=st; k<=ed; k++){
                    Ww[i][k] = st;
                    Ee[i][k] = ed;
                }
            }
        }

        vector<vector<pii>> dpV(W), dpH(H);
        dpV[sy].push_back({sx,sx});

        int t=0, d=0;

        auto mergeInt = [&](vector<pii> &v){
            if(v.empty()) return;
            sort(v.begin(), v.end());
            vector<pii> nv;
            pii cur = v[0];
            for(size_t i=1;i<v.size();i++){
                if(v[i].first <= cur.second+1)
                    cur.second = max(cur.second, v[i].second);
                else { nv.push_back(cur); cur = v[i]; }
            }
            nv.push_back(cur);
            v = move(nv);
        };

        auto extendV = [&](){
            vector<vector<pii>> ndp(W);
            for(int j=0;j<W;j++){
                for(auto &p: dpV[j]){
                    int a = p.first, b = p.second;
                    if(d==0){
                        int na = Nn[a][j];
                        ndp[j].push_back({na, b});
                    } else {
                        int nb = Ss[b][j];
                        ndp[j].push_back({a, nb});
                    }
                }
                mergeInt(ndp[j]);
            }
            dpV = move(ndp);
        };
        auto extendH = [&](){
            vector<vector<pii>> ndp(H);
            for(int i=0;i<H;i++){
                for(auto &p: dpH[i]){
                    int a = p.first, b = p.second;
                    if(d==1){
                        int nb = Ee[i][b];
                        ndp[i].push_back({a, nb});
                    } else {
                        int na = Ww[i][a];
                        ndp[i].push_back({na, b});
                    }
                }
                mergeInt(ndp[i]);
            }
            dpH = move(ndp);
        };

        auto chkV = [&](){
            for(auto &p: dpV[gy])
                if(p.first <= gx && gx <= p.second) return true;
            return false;
        };
        auto chkH = [&](){
            for(auto &p: dpH[gx])
                if(p.first <= gy && gy <= p.second) return true;
            return false;
        };

        auto convVtoH = [&](){
            vector<vector<pii>> ndp(H);
            vector<vector<int>> add(H);
            for(int j=0;j<W;j++){
                for(auto &p: dpV[j]){
                    for(int r = p.first; r <= p.second; r++)
                        add[r].push_back(j);
                }
            }
            for(int i=0;i<H;i++){
                if(add[i].empty()) continue;
                sort(add[i].begin(), add[i].end());
                int st = add[i][0], ed = add[i][0];
                vector<pii> seg;
                for(size_t k=1;k<add[i].size();k++){
                    int x = add[i][k];
                    if(x <= ed+1) ed = max(ed, x);
                    else { seg.push_back({st,ed}); st = x; ed = x; }
                }
                seg.push_back({st,ed});
                ndp[i] = move(seg);
            }
            dpH = move(ndp);
        };
        auto convHtoV = [&](){
            vector<vector<pii>> ndp(W);
            vector<vector<int>> add(W);
            for(int i=0;i<H;i++){
                for(auto &p: dpH[i]){
                    for(int j = p.first; j <= p.second; j++)
                        add[j].push_back(i);
                }
            }
            for(int j=0;j<W;j++){
                if(add[j].empty()) continue;
                sort(add[j].begin(), add[j].end());
                int st = add[j][0], ed = add[j][0];
                vector<pii> seg;
                for(size_t k=1;k<add[j].size();k++){
                    int x = add[j][k];
                    if(x <= ed+1) ed = max(ed,x);
                    else { seg.push_back({st,ed}); st = x; ed = x; }
                }
                seg.push_back({st,ed});
                ndp[j] = move(seg);
            }
            dpV = move(ndp);
        };

        bool ok = false;
        while(true){
            if(d==0 || d==2){
                extendV();
                if(chkV()){ ok = true; break; }
            } else {
                extendH();
                if(chkH()){ ok = true; break; }
            }
            if(t >= NN) break;
            char c = prog[t++];
            d = (c=='L' ? (d+3)%4 : (d+1)%4);
            if(d==0 || d==2){
                if(!dpH.empty()){
                    convHtoV();
                    dpH.clear();
                }
            } else {
                if(!dpV.empty()){
                    convVtoH();
                    dpV.clear();
                }
            }
        }
        cout << (ok? "Yes": "No") << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 680
memory_kb: 26568
final_rank: 14

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rrep(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int dx[4] = {-1,0,1,0}, dy[4] = {0,-1,0,1};

int main() {
while(1) {
    int N, M, K;
    cin >> N >> M >> K;
    if (N == 0) return 0;
    string S;
    cin >> S;
    vector<vector<bool>> B(N,vector<bool>(M));
    int SX, SY, GX, GY;
    rep(i,0,N) {
        rep(j,0,M) {
            char C;
            cin >> C;
            if (C == 'S') SX = i, SY = j;
            if (C == 'G') GX = i, GY = j;
            B[i][j] = (C != '#');
        }
    }
    vector<int> Dir(K+1);
    Dir[0] = 0;
    rep(i,0,K) {
        if (S[i] == 'L') Dir[i+1] = (Dir[i] + 1) % 4;
        else Dir[i+1] = (Dir[i] + 3) % 4;
    }
    vector<vector<int>> Next(K+1, vector<int>(4,inf));
    Next[K][Dir[K]] = K;
    rrep(i,0,K) {
        rep(j,0,4) Next[i][j] = Next[i+1][j];
        Next[i][Dir[i]] = i;
    }
    vector DP(N, vector(M, vector<int>(4,inf)));
    priority_queue<pair<int,tuple<int,int,int>>,vector<pair<int,tuple<int,int,int>>>,greater<pair<int,tuple<int,int,int>>>> PQ;
    DP[SX][SY][0] = 0;
    PQ.push({0,{SX,SY,0}});
    while(!PQ.empty()) {
        auto [D,P] = PQ.top();
        PQ.pop();
        auto [X,Y,dir] = P;
        if (D > DP[X][Y][dir]) continue;
        rep(i,0,4) {
            if (chmin(DP[X][Y][i],Next[D][i])) {
                PQ.push({Next[D][i],{X,Y,i}});
            }
        }
        int NX = X + dx[dir], NY = Y + dy[dir];
        if (0 <= NX && NX < N && 0 <= NY && NY < M) {
            if (B[NX][NY] && chmin(DP[NX][NY][dir],D)) {
                PQ.push({D,{NX,NY,dir}});
            }
        }
    }
    int ANS = inf;
    rep(i,0,4) chmin(ANS, DP[GX][GY][i]);
    if (ANS <= K) cout << "Yes" << endl;
    else cout << "No" << endl;
}
}
accuracy: 1
time_ms: 540
memory_kb: 59960
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < (int) v.size(); ++i) {
        os << v[i];
        if (i < (int) v.size() - 1) os << " ";
    }
    return os;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    const vector<int> di = {-1, 0, 1, 0};
    const vector<int> dj = {0, -1, 0, 1};
    constexpr int INF = 1e9;

    while (true) {
        int H, W, N;
        cin >> H >> W >> N;
        if (N == 0) break;
        string s;
        cin >> s;
        vector<int> dir(N+1);
        rep(i,0,N) {
            dir[i+1] = (dir[i] + (s[i] == 'L' ? 1 : 3)) % 4;
        }
        vector<vector<int>> next(N+1, vector<int>(4, -1));
        revrep(i,N,0) {
            rep(k,0,4) next[i][k] = next[i+1][k];
            next[i][dir[i+1]] = i+1;
        }
        vector<string> c(H);
        for (auto& x : c) cin >> x;
        using P = tuple<int, int, int, int>;
        vector<vector<vector<int>>> dist(H, vector<vector<int>>(W, vector<int>(4, INF)));
        priority_queue<P, vector<P>, greater<>> pq;
        rep(i,0,H) rep(j,0,W) {
            if (c[i][j] == 'S') {
                dist[i][j][0] = 0;
                pq.push({0, i, j, 0});
            }
        }
        bool ok = false;
        while (!pq.empty()) {
            auto [d, i, j, k] = pq.top();
            pq.pop();
            if (dist[i][j][k] < d) continue;
            if (c[i][j] == 'G') {
                ok = true;
                break;
            }
            // forward
            int ni = i+di[k], nj = j+dj[k];
            if (0 <= ni && ni < H && 0 <= nj && nj < W && c[ni][nj] != '#' && dist[ni][nj][k] > d) {
                dist[ni][nj][k] = d;
                pq.push({d, ni, nj, k});
            }
            // left
            int nk = (k+1)%4, e = (dir[d]+1)%4;
            if (next[d][e] != -1 && dist[i][j][nk] > next[d][e]) {
                dist[i][j][nk] = next[d][e];
                pq.push({next[d][e], i, j, nk});
            }
            // right
            nk = (k+3)%4, e = (dir[d]+3)%4;
            if (next[d][e] != -1 && dist[i][j][nk] > next[d][e]) {
                dist[i][j][nk] = next[d][e];
                pq.push({next[d][e], i, j, nk});
            }
        }
        cout << (ok ? "Yes" : "No") << endl;
    }
}
accuracy: 1
time_ms: 400
memory_kb: 60920
final_rank: 16

code_snippet: #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb(...) emplace_back(__VA_ARGS__)
#define mp(a, b) make_pair(a, b)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define REP(i, l, r) for (int i = l; i < (r); i++)
#define REP2(i, l, r) for (int i = (int)r - 1; i >= (l); i--)
#define siz(x) (ll) x.size()
template <class T>
using rque = priority_queue<T, vector<T>, greater<T>>;

template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

template <class T>
bool chmax(T &a, const T &b) {
    if (b > a) {
        a = b;
        return 1;
    }
    return 0;
}

template <class T>
void print(vector<T> a) {
    if (a.empty())
        cout << '\n';
    else {
        for (int i = 0; i < a.size(); i++)
            cout << a[i] << (i + 1 == a.size() ? '\n' : ' ');
    }
}

// __int128_t gcd(__int128_t a, __int128_t b) {
//     if (a == 0)
//         return b;
//     if (b == 0)
//         return a;
//     __int128_t cnt = a % b;
//     while (cnt != 0) {
//         a = b;
//         b = cnt;
//         cnt = a % b;
//     }
//     return b;
// }

long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

struct UnionFind {
    vector<int> data;
    int num;

    UnionFind(int sz) {
        data.assign(sz, -1);
        num = sz;
    }

    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y)
            return (false);
        if (data[x] > data[y])
            swap(x, y);
        data[x] += data[y];
        data[y] = x;
        num--;
        return (true);
    }

    int find(int k) {
        if (data[k] < 0)
            return (k);
        return (data[k] = find(data[k]));
    }

    int size(int k) {
        return (-data[find(k)]);
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    int operator[](int k) {
        return find(k);
    }
};

template <int mod>
struct Mod_Int {
    int x;

    Mod_Int() : x(0) {
    }

    Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {
    }

    static int get_mod() {
        return mod;
    }

    Mod_Int &operator+=(const Mod_Int &p) {
        if ((x += p.x) >= mod)
            x -= mod;
        return *this;
    }

    Mod_Int &operator-=(const Mod_Int &p) {
        if ((x += mod - p.x) >= mod)
            x -= mod;
        return *this;
    }

    Mod_Int &operator*=(const Mod_Int &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    Mod_Int &operator/=(const Mod_Int &p) {
        *this *= p.inverse();
        return *this;
    }

    Mod_Int &operator++() {
        return *this += Mod_Int(1);
    }

    Mod_Int operator++(int) {
        Mod_Int tmp = *this;
        ++*this;
        return tmp;
    }

    Mod_Int &operator--() {
        return *this -= Mod_Int(1);
    }

    Mod_Int operator--(int) {
        Mod_Int tmp = *this;
        --*this;
        return tmp;
    }

    Mod_Int operator-() const {
        return Mod_Int(-x);
    }

    Mod_Int operator+(const Mod_Int &p) const {
        return Mod_Int(*this) += p;
    }

    Mod_Int operator-(const Mod_Int &p) const {
        return Mod_Int(*this) -= p;
    }

    Mod_Int operator*(const Mod_Int &p) const {
        return Mod_Int(*this) *= p;
    }

    Mod_Int operator/(const Mod_Int &p) const {
        return Mod_Int(*this) /= p;
    }

    bool operator==(const Mod_Int &p) const {
        return x == p.x;
    }

    bool operator!=(const Mod_Int &p) const {
        return x != p.x;
    }

    Mod_Int inverse() const {
        assert(*this != Mod_Int(0));
        return pow(mod - 2);
    }

    Mod_Int pow(long long k) const {
        Mod_Int now = *this, ret = 1;
        for (; k > 0; k >>= 1, now *= now) {
            if (k & 1)
                ret *= now;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const Mod_Int &p) {
        return os << p.x;
    }

    friend istream &operator>>(istream &is, Mod_Int &p) {
        long long a;
        is >> a;
        p = Mod_Int<mod>(a);
        return is;
    }
};

ll mpow2(ll x, ll n, ll mod) {
    ll ans = 1;
    x %= mod;
    while (n != 0) {
        if (n & 1)
            ans = ans * x % mod;
        x = x * x % mod;
        n = n >> 1;
    }
    ans %= mod;
    return ans;
}

ll modinv2(ll a, ll mod) {
    ll b = mod, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= mod;
    if (u < 0)
        u += mod;
    return u;
}

ll divide_int(ll a, ll b) {
    return (a >= 0 ? a / b : (a - b + 1) / b);
}

constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
// constexpr int mod = 31607;
using mint = Mod_Int<mod>;

mint mpow(mint x, ll n) {
    bool rev = n < 0;
    n = abs(n);
    mint ans = 1;
    while (n != 0) {
        if (n & 1)
            ans *= x;
        x *= x;
        n = n >> 1;
    }
    return (rev ? ans.inverse() : ans);
}

// ----- library -------
// ----- library -------

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout << fixed << setprecision(15);

    while (1) {
        int h, w, n;
        cin >> h >> w >> n;
        if (!h)
            break;
        string com;
        cin >> com;
        vector<string> s(h);
        rep(i, h) cin >> s[i];
        vector<int> ds(n + 1);
        ds[0] = 0;
        rep(i, n) ds[i + 1] = (ds[i] + (com[i] == 'R' ? 1 : 3)) % 4;
        vector<vector<int>> nidx(n + 1, vector<int>(4));
        vector<int> memo(4, 2e9);
        rep2(i, n + 1) {
            memo[ds[i]] = i;
            rep(j, 4) nidx[i][j] = memo[j];
        }
        rque<pair<int, pair<int, int>>> que;
        rep(i, h) rep(j, w) if (s[i][j] == 'S') que.push({0, {i, j}});
        vector<vector<int>> d(h, vector<int>(w, 1e9));
        int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
        while (!que.empty()) {
            auto [nd, nij] = que.top();
            que.pop();
            auto [ni, nj] = nij;
            if (chmin(d[ni][nj], nd)) {
                rep(dir, 4) {
                    int nei = ni + dx[dir], nej = nj + dy[dir];
                    if (nei < 0 || h <= nei || nej < 0 || w <= nej)
                        continue;
                    if (s[nei][nej] == '#')
                        continue;
                    que.push({nidx[nd][dir], {nei, nej}});
                }
            }
        }
        rep(i, h) rep(j, w) if (s[i][j] == 'G') cout << (d[i][j] < 1e8 ? "Yes" : "No") << '\n';
    }
}
accuracy: 1
time_ms: 310
memory_kb: 9804
final_rank: 8

code_snippet: #line 2 "/home/nok0/documents/programming/library/template/header.hpp"
#include <bits/stdc++.h>
#line 3 "/home/nok0/documents/programming/library/template/def_const.hpp"

const int inf = 1000000000;
const long long INF = 1000000000000000000ll;
#line 4 "/home/nok0/documents/programming/library/template/debug.hpp"

namespace viewer {
void view(const long long &e) {
	if(e == INF)
		std::cerr << "INF";
	else if(e == -INF)
		std::cerr << "-INF";
	else
		std::cerr << e;
}

void view(const int &e) {
	if(e == inf)
		std::cerr << "inf";
	else if(e == -inf)
		std::cerr << "-inf";
	else
		std::cerr << e;
}

template <typename T>
void view(const T &e) {
	std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << "(";
	view(p.first);
	std::cerr << ", ";
	view(p.second);
	std::cerr << ")";
}

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ")";
}

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ", ";
	view(std::get<3>(p));
	std::cerr << ")";
}

template <typename T>
void view(const std::set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::unordered_set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::multiset<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::unordered_multiset<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T, std::size_t ary_size>
void view(const std::array<T, ary_size> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	std::cerr << "{\n";
	for(const auto &v : vv) {
		std::cerr << "\t";
		view(v);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	std::cerr << "{\n";
	for(const auto &c : v) {
		std::cerr << "\t(";
		view(c.first);
		std::cerr << ", ";
		view(c.second);
		std::cerr << ")\n";
	}
	std::cerr << "}";
}

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}
}  // namespace viewer

// when compiling : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	viewer::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(0)
#define dump(x)                                      \
	do {                                             \
		std::cerr << __LINE__ << " " << #x << " : "; \
		viewer::view(x);                             \
		std::cerr << '\n';                           \
	} while(0)

#else
#define debug(...) (void(0))
#define dump(x)    (void(0))
#endif
#line 3 "/home/nok0/documents/programming/library/template/def_name.hpp"

#define pb        push_back
#define eb        emplace_back
#define SZ(x)     ((int)(x).size())
#define all(x)    (x).begin(), (x).end()
#define rall(x)   (x).rbegin(), (x).rend()
#define popcnt(x) __builtin_popcountll(x)
template<class T = int>
using V = std::vector<T>;
template<class T = int>
using VV = std::vector<std::vector<T>>;
template<class T>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
using ll = long long;
using ld = long double;
using int128 = __int128_t;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
#line 3 "/home/nok0/documents/programming/library/template/fast_io.hpp"

struct fast_io {
	fast_io() {
		std::ios::sync_with_stdio(false);
		std::cin.tie(nullptr);
		std::cout << std::fixed << std::setprecision(15);
	}
} fast_io_;
#line 3 "/home/nok0/documents/programming/library/template/input.hpp"

template<class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    is >> p.first >> p.second;
    return is;
}
template<class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
std::istream &operator>>(std::istream &is, __int128_t &a) {
    std::string s;
    is >> s;
    __int128_t ret = 0;
    for (int i = 0; i < (int)s.length(); i++)
        if ('0' <= s[i] and s[i] <= '9')
            ret = 10 * ret + s[i] - '0';
    a = ret * (s[0] == '-' ? -1 : 1);
    return is;
}
namespace scanner {
void scan(int &a) { std::cin >> a; }
void scan(long long &a) { std::cin >> a; }
void scan(std::string &a) { std::cin >> a; }
void scan(char &a) { std::cin >> a; }
void scan(char a[]) { std::scanf("%s", a); }
void scan(double &a) { std::cin >> a; }
void scan(long double &a) { std::cin >> a; }
template<class T, class U>
void scan(std::pair<T, U> &p) { std::cin >> p; }
template<class T>
void scan(std::vector<T> &a) { std::cin >> a; }
void INPUT() {}
template<class Head, class... Tail>
void INPUT(Head &head, Tail &...tail) {
    scan(head);
    INPUT(tail...);
}
}  // namespace scanner
#define VEC(type, name, size)     \
    std::vector<type> name(size); \
    scanner::INPUT(name)
#define VVEC(type, name, h, w)                                    \
    std::vector<std::vector<type>> name(h, std::vector<type>(w)); \
    scanner::INPUT(name)
#define INT(...)     \
    int __VA_ARGS__; \
    scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
    long long __VA_ARGS__; \
    scanner::INPUT(__VA_ARGS__)
#define STR(...)             \
    std::string __VA_ARGS__; \
    scanner::INPUT(__VA_ARGS__)
#define CHAR(...)     \
    char __VA_ARGS__; \
    scanner::INPUT(__VA_ARGS__)
#define DOUBLE(...)     \
    double __VA_ARGS__; \
    scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
    long double __VA_ARGS__; \
    scanner::INPUT(__VA_ARGS__)
#line 3 "/home/nok0/documents/programming/library/template/math.hpp"

template <class T, class U>
inline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <class T, class U>
inline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <class T>
T divup(T x, T y) { return (x + y - 1) / y; }
template <class T>
T POW(T a, long long n) {
	T ret = 1;
	while(n) {
		if(n & 1) ret *= a;
		a *= a;
		n >>= 1;
	}
	return ret;
}
long long POW(long long a, long long n, const int mod) {
	long long ret = 1;
	a = (a % mod + mod) % mod;
	while(n) {
		if(n & 1) (ret *= a) %= mod;
		(a *= a) %= mod;
		n >>= 1;
	}
	return ret;
}
template <class T, class F>
T bin_search(T ok, T ng, const F &f) {
	while(abs(ok - ng) > 1) {
		T mid = (ok + ng) >> 1;
		(f(mid) ? ok : ng) = mid;
	}
	return ok;
}
template <class T, class F>
T bin_search(T ok, T ng, const F &f, int loop) {
	for(int i = 0; i < loop; i++) {
		T mid = (ok + ng) / 2;
		(f(mid) ? ok : ng) = mid;
	}
	return ok;
}
#line 3 "/home/nok0/documents/programming/library/template/output.hpp"


template<class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    os << p.first << " " << p.second;
    return os;
}
template<class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {
    for (int i = 0; i < int(a.size()); ++i) {
        if (i) os << " ";
        os << a[i];
    }
    return os;
}
std::ostream &operator<<(std::ostream &dest, __int128_t &value) {
    std::ostream::sentry s(dest);
    if (s) {
        __uint128_t tmp = value < 0 ? -value : value;
        char buffer[128];
        char *d = std::end(buffer);
        do {
            --d;
            *d = "0123456789"[tmp % 10];
            tmp /= 10;
        } while (tmp != 0);
        if (value < 0) {
            --d;
            *d = '-';
        }
        int len = std::end(buffer) - d;
        if (dest.rdbuf()->sputn(d, len) != len) {
            dest.setstate(std::ios_base::badbit);
        }
    }
    return dest;
}
template<class T>
void print(const T a) { std::cout << a << '\n'; }
template<class Head, class... Tail>
void print(Head H, Tail... T) {
    std::cout << H << ' ';
    print(T...);
}
template<class T>
void printel(const T a) { std::cout << a << '\n'; }
template<class T>
void printel(const std::vector<T> &a) {
    for (const auto &v : a)
        std::cout << v << '\n';
}
template<class Head, class... Tail>
void printel(Head H, Tail... T) {
    std::cout << H << '\n';
    printel(T...);
}
void Yes(const bool b = true) { std::cout << (b ? "Yes\n" : "No\n"); }
void No() { std::cout << "No\n"; }
void YES(const bool b = true) { std::cout << (b ? "YES\n" : "NO\n"); }
void NO() { std::cout << "NO\n"; }
#line 2 "/home/nok0/documents/programming/library/template/rep.hpp"

#define foa(v, a)                   for (auto &v : a)
#define repname(a, b, c, d, e, ...) e
#define rep(...)                    repname(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)
#define rep0(x)                     for (int rep_counter = 0; rep_counter < (x); ++rep_counter)
#define rep1(i, x)                  for (int i = 0; i < (x); ++i)
#define rep2(i, l, r)               for (int i = (l); i < (r); ++i)
#define rep3(i, l, r, c)            for (int i = (l); i < (r); i += (c))

#define repsname(a, b, c, ...) c
#define reps(...)              repsname(__VA_ARGS__, reps1, reps0)(__VA_ARGS__)
#define reps0(x)               for (int reps_counter = 1; reps_counter <= (x); ++reps_counter)
#define reps1(i, x)            for (int i = 1; i <= (x); ++i)

#define rrepname(a, b, c, ...) c
#define rrep(...)              rrepname(__VA_ARGS__, rrep1, rrep0)(__VA_ARGS__)
#define rrep0(x)               for (int rrep_counter = (x)-1; rrep_counter >= 0; --rrep_counter)
#define rrep1(i, x)            for (int i = (x)-1; i >= 0; --i)
#line 3 "/home/nok0/documents/programming/library/template/vector.hpp"

template <class T>
int lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }
template <class T>
int ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }
template <class T>
void UNIQUE(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<T> press(std::vector<T> &a) {
	auto res = a;
	UNIQUE(res);
	for(auto &v : a)
		v = lb(res, v);
	return res;
}
#define SORTname(a, b, c, ...) c
#define SORT(...)              SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)
#define SORT0(a)               std::sort((a).begin(), (a).end())
#define SORT1(a, c)            std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })
template <class T>
void ADD(std::vector<T> &a, const T x = 1) {
	for(auto &v : a) v += x;
}
template <class T>
void SUB(std::vector<T> &a, const T x = 1) {
	for(auto &v : a) v -= x;
}
template <class T>
struct cum_vector {
   public:
	cum_vector() = default;
	template <class U>
	cum_vector(const std::vector<U> &vec) : cum((int)vec.size() + 1) {
		for(int i = 0; i < (int)vec.size(); i++)
			cum[i + 1] = cum[i] + vec[i];
	}
	T prod(int l, int r) {
		return cum[r] - cum[l];
	}

   private:
	std::vector<T> cum;
};
std::vector<std::pair<char, int>> rle(const std::string &s) {
	const int n = s.size();
	std::vector<std::pair<char, int>> ret;
	for(int l = 0; l < n;) {
		int r = l + 1;
		for(; r < n and s[l] == s[r]; r++) {}
		ret.emplace_back(s[l], r - l);
		l = r;
	}
	return ret;
}
template <class T>
std::vector<std::pair<T, int>> rle(const std::vector<T> &v) {
	int n = v.size();
	std::vector<std::pair<T, int>> ret;
	for(int l = 0; l < n;) {
		int r = l + 1;
		for(; r < n and v[l] == v[r]; r++) {}
		ret.emplace_back(v[l], r - l);
		l = r;
	}
	return ret;
}
std::vector<int> iota(int n) {
	std::vector<int> p(n);
	std::iota(p.begin(), p.end(), 0);
	return p;
}
#line 11 "/home/nok0/documents/programming/library/template/all"
using namespace std;
#line 2 "e.cpp"

void main_();
int main() {
	while(true) main_();
}

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void main_() {
	INT(h, w, n);
	if(!h) exit(0);
	vector dp(h, vector(w, inf));
	STR(com);
	VEC(string, s, h);
	int sx, sy, gx, gy;
	rep(i, h) {
		rep(j, w) {
			if(s[i][j] == 'S') sx = i, sy = j;
			if(s[i][j] == 'G') gx = i, gy = j;
		}
	}
	dp[sx][sy] = 0;
	pqup<pair<int, pii>> pq;
	pq.push({0, pair(sx, sy)});
	vector<int> dir(n + 1);
	dir[0] = 2;
	VV<> idx(4);
	rep(i, n) {
		if(com[i] == 'L') {
			dir[i + 1] = dir[i] + 1;
			dir[i + 1] %= 4;
		} else {
			dir[i + 1] = dir[i] + 3;
			dir[i + 1] %= 4;
		}
	}
	rep(i, n + 1) idx[dir[i]].pb(i);
	rep(i, 4) idx[i].pb(inf);
	auto valid = [&](int x, int y) {
		return x >= 0 and x < h and y >= 0 and y < w and s[x][y] != '#';
	};
	while(SZ(pq)) {
		auto [d, p] = pq.top();
		pq.pop();
		auto [x, y] = p;
		if(d > dp[x][y]) continue;
		rep(k, 4) {
			int nx = x + dx[k], ny = y + dy[k];
			if(!valid(nx, ny)) continue;
			int nxt_len = idx[k][lb(idx[k], d)];
			if(nxt_len == inf) continue;
			if(chmin(dp[nx][ny], nxt_len)) {
				pq.push({dp[nx][ny], pair(nx, ny)});
			}
		}
	}
	debug(dp);
	Yes(dp[gx][gy] != inf);
}
accuracy: 1
time_ms: 140
memory_kb: 8540
final_rank: 3

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

// 0:↑ 1:→ 2:↓ 3:←
// R:+1 L:-1

int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int h,w,n;
    while(cin >> h >> w >> n,h){
        string op; cin >> op;
        vector<string> s(h);
        int sx,sy,gx,gy;
        for(int i=0;i<h;i++){
            cin >> s[i];
            for(int j=0;j<w;j++){
                if(s[i][j] == 'S'){
                    s[i][j] = '.';
                    sx = i, sy = j;
                }
                if(s[i][j] == 'G'){
                    s[i][j] = '.';
                    gx = i, gy = j;
                }
            }
        }
        vector<int> dir(n+1);
        dir[0] = 0;
        for(int i=0;i<n;i++){
            if(op[i] == 'R'){
                dir[i+1] = dir[i] + 1;
                if(dir[i+1] == 4) dir[i+1] = 0;
            }
            else{
                dir[i+1] = dir[i] - 1;
                if(dir[i+1] == -1) dir[i+1] = 3;
            }
        }
        vector<vector<int>> nex(4,vector<int>(n+1,-1));
        for(int i=n;i>=0;i--){
            if(i+1<=n){
                for(int j=0;j<4;j++){
                    nex[j][i] = nex[j][i+1];
                }
            }
            nex[dir[i]][i] = i;
        }
        vector<vector<int>> dp(4,vector<int>(h*w,1e9));
        auto idx=[&](int x,int y)->int{
            return x*w+y;
        };
        dp[0][idx(sx,sy)] = 0;
        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
        q.push({0,idx(sx,sy)});
        while(q.size()){
            auto p = q.top(); q.pop();
            int dd = p.first;
            int x = p.second / w, y = p.second % w;
            for(int k=0;k<4;k++){
                if(nex[k][dd] == -1) continue;
                if(dp[k][idx(x,y)] > nex[k][dd]){
                    dp[k][idx(x,y)] = nex[k][dd];
                    q.push({nex[k][dd],idx(x,y)});
                }
            }
            {
                int k = dir[dd];
                int nx = x + dx[k];
                int ny = y + dy[k];
                if(0<=nx and nx<h and 0<=ny and ny<w and s[nx][ny] == '.'){
                    if(dp[k][idx(nx,ny)] > nex[k][dd]){
                        dp[k][idx(nx,ny)] = nex[k][dd];
                        q.push({nex[k][dd],idx(nx,ny)});
                    }
                }
            }
        }
        if(dp[dir.back()][idx(gx,gy)] != 1e9){
            cout << "Yes" << endl;
        }
        else{
            cout << "No" << endl;
        }
    }
}
accuracy: 1
time_ms: 300
memory_kb: 23800
final_rank: 10

code_snippet: #pragma GCC tag("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 5050000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-9;
const int mod = 1e9 + 7;
//const int mod = 998244353;

bool in(ll y, ll x, ll h, ll w){
    return 0 <= y && y < h && 0 <= x && x < w;
}

using tu = tuple<int,int,int,int>;

int dp[1010][1010][4];

int main(){
    while(1){
        int h,w,n; cin >> h >> w >> n;
        if(!h) break;
        string com; cin >> com;
        vvl pos(4);
        int now = 0;
        rep(i,n){
            if(com[i] == 'R') now+=3;
            else now++;
            now %= 4;
            pos[now].push_back(i);
        }
        rep(i,4) pos[i].push_back(inf);
        now = 0;
        vvl nxt(n+1,vl(4));
        rep(i,n+1){
            rep(j,4){
                nxt[i][j] = *lower_bound(all(pos[(now+j)%4]),i);
            }
            if(i == n) break;
            if(com[i] == 'R') now+=3;
            else now++;
            now %= 4;
        }
        vs s(h); rep(i,h) cin >> s[i];
        rep(i,h) rep(j,w) rep(k,4) dp[i][j][k] = inf;
        priority_queue<tu,vector<tu>,greater<tu>> pq;
        int gy,gx;
        rep(i,h) rep(j,w){
            if(s[i][j] == 'S'){
                pq.emplace(0,i,j,3);
                dp[i][j][3] = 0;
            }
            if(s[i][j] == 'G') gy = i, gx = j;
        }
        while(!pq.empty()){
            int c,y,x,dir;
            tie(c,y,x,dir) = pq.top(); pq.pop();
            if(dp[y][x][dir] < c) continue;
            rep(i,4){
                int ny = y + dy[i];
                int nx = x + dx[i];
                if(!in(ny,nx,h,w) || s[ny][nx] == '#') continue;
                int nc = (dir==i) ? c : nxt[c][(i-dir+4)%4]+1;
                if(nc > n) continue;
                if(chmin(dp[ny][nx][i], nc)) pq.emplace(nc,ny,nx,i);
            }
        }
        int ans = inf;
        rep(i,4) chmin(ans, dp[gy][gx][i]);
        if(ans <= n) cout << "Yes\n";
        else cout << "No\n";
    }
}
accuracy: 1
time_ms: 410
memory_kb: 22752
final_rank: 13

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <tuple>

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

using namespace std;

const vector<pair<int, int>> dxys{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

bool solve() {
    int h, w, n;
    cin >> h >> w >> n;
    if (h == 0) return false;

    auto nxt = vector(n + 1, vector(4, -1));
    {
        vector<int> ds(n + 1);
        ds[0] = 0;
        for (int i = 1; i <= n; ++i) {
            ds[i] = ds[i - 1];

            char c;
            cin >> c;
            ds[i] = (ds[i] + (c == 'R' ? 1 : 3)) % 4;
        }

        for (int i = n; i >= 0; --i) {
            if (i < n) nxt[i] = nxt[i + 1];
            nxt[i][ds[i]] = i;
        }
    }

    vector<string> ss(h);
    for (auto& s : ss) cin >> s;

    int sx, sy, gx, gy;
    for (int x = 0; x < h; ++x) {
        for (int y = 0; y < w; ++y) {
            if (ss[x][y] == 'S') {
                sx = x, sy = y;
                ss[x][y] = '.';
            } else if (ss[x][y] == 'G') {
                gx = x, gy = y;
                ss[x][y] = '.';
            }
        }
    }

    auto dist = vector(h, vector(w, n + 1));
    MinHeap<tuple<int, int, int>> heap;
    dist[sx][sy] = 0;
    heap.emplace(dist[sx][sy], sx, sy);

    while (!heap.empty()) {
        auto [d, x, y] = heap.top();
        heap.pop();
        if (dist[x][y] < d) continue;

        for (int i = 0; i < 4; ++i) {
            int nd = nxt[d][i];
            if (nd == -1) continue;

            auto [dx, dy] = dxys[i];
            int nx = x + dx,
                ny = y + dy;

            if (nx < 0 || h <= nx ||
                ny < 0 || w <= ny ||
                ss[nx][ny] == '#' ||
                dist[nx][ny] <= nd) continue;

            dist[nx][ny] = nd;
            heap.emplace(nd, nx, ny);
        }
    }

    cout << (dist[gx][gy] <= n ? "Yes" : "No") << "\n";
    return true;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    while (solve()) {}

    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 8520
final_rank: 2

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

struct Node {
	int cost;
	int x, y, d;
	Node(int yy, int xx, int dd, int cc) :y(yy), x(xx), d(dd), cost(cc) {

	}
	bool operator<(const Node&n)const {
		return cost > n.cost;
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	int dir[] = { -1,0,1,0,-1 };
	while (cin >> H >> W >> K, H) {
		string s;
		cin >> s;
		vector<string>field(H);
		for (auto &i : field)cin >> i;
		int sy, sx, gy, gx;
		vector<vector<int>>d(4, vector<int>(s.size(), MOD));
		int cd = 0;
		int cnt = 0;
		for (auto i : s) {
			if (i == 'L')cd += 3;
			else cd++;
			cd %= 4;
			d[cd][cnt] = cnt;
			cnt++;
		}
		for (int i = s.size(); i >= 2; i--) {
			for (int j = 0; j < 4; j++) {
				d[j][i - 2] = min(d[j][i - 2], d[j][i - 1]);
			}
		}
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (field[i][j] == 'S') {
					sy = i, sx = j;
					field[i][j] = '.';
				}
				if (field[i][j] == 'G') {
					gy = i, gx = j;
					field[i][j] = '.';
				}
			}
		}
		priority_queue<Node>PQ;
		vector<vector<vector<int>>>dis(H, vector<vector<int>>(W, vector<int>(4, MOD)));
		dis[sy][sx][0] = 0;
		PQ.push(Node(sy, sx, 0, 0));
		cnt = 0;
		while (!PQ.empty()) {
			auto box = PQ.top();
			PQ.pop();
			if (dis[box.y][box.x][box.d] < box.cost)continue;
			cnt++;
			int ny = box.y + dir[box.d];
			int nx = box.x + dir[box.d + 1];
			if (ny >= 0 && nx >= 0 && ny < H&&nx < W&&field[ny][nx]!='#') {
				if (dis[ny][nx][box.d] > box.cost) {
					dis[ny][nx][box.d] = box.cost;
					PQ.push(Node(ny, nx, box.d, box.cost));
				}
			}
			for (int i = 0; i < 4; i++) {
				auto it = d[i][box.cost];
				if (it == MOD)continue;
				if (dis[box.y][box.x][i] > it) {
					dis[box.y][box.x][i] = it;
					PQ.push(Node(box.y, box.x, i, it));
				}
			}
		}
		int ans = MOD;
		for (auto i : dis[gy][gx])ans = min(ans, i);
		if (ans == MOD)cout << "No" << endl;
		else cout << "Yes" << endl;;
	}
}
accuracy: 1
time_ms: 430
memory_kb: 62928
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
vector<int> dy = {-1, 0, 1, 0};
vector<int> dx = {0, 1, 0, -1};
int main(){
  while (1){
    int H, W, N;
    cin >> H >> W >> N;
    if (H == 0 && W == 0 && N == 0){
      break;
    }
    string s;
    cin >> s;
    vector<vector<char>> c(H + 2, vector<char>(W + 2, '#'));
    for (int i = 1; i <= H; i++){
      for (int j = 1; j <= W; j++){
        cin >> c[i][j];
      }
    }
    vector<int> S(N + 1, 0);
    for (int i = 0; i < N; i++){
      if (s[i] == 'R'){
        S[i + 1] = (S[i] + 1) % 4;
      }
      if (s[i] == 'L'){
        S[i + 1] = (S[i] + 3) % 4;
      }
    }
    vector<vector<int>> next(N + 1, vector<int>(4, -1));
    for (int i = N - 1; i >= 0; i--){
      next[i] = next[i + 1];
      next[i][S[i + 1]] = i + 1;
    }
    int sy, sx;
    for (int i = 1; i <= H; i++){
      for (int j = 1; j <= W; j++){
        if (c[i][j] == 'S'){
          sy = i;
          sx = j;
        }
      }
    }
    c[sy][sx] = '.';
    int gy, gx;
    for (int i = 1; i <= H; i++){
      for (int j = 1; j <= W; j++){
        if (c[i][j] == 'G'){
          gy = i;
          gx = j;
        }
      }
    }
    c[gy][gx] = '.';
    vector<vector<vector<int>>> d(H + 2, vector<vector<int>>(W + 2, vector<int>(4, -1)));
    priority_queue<tuple<int, int, int, int>, vector<tuple<int, int, int, int>>, greater<tuple<int, int, int, int>>> pq;
    pq.push(make_tuple(0, sy, sx, 0));
    while (!pq.empty()){
      int t = get<0>(pq.top());
      int y = get<1>(pq.top());
      int x = get<2>(pq.top());
      int f = get<3>(pq.top());
      pq.pop();
      if (d[y][x][f] == -1){
        d[y][x][f] = t;
        int y2 = y + dy[f];
        int x2 = x + dx[f];
        if (c[y2][x2] == '.' && d[y2][x2][f] == -1){
          pq.push(make_tuple(t, y2, x2, f));
        }
        for (int i = 0; i < 4; i++){
          if (next[t][i] != -1 && d[y][x][i] == -1){
            pq.push(make_tuple(next[t][i], y, x, i));
          }
        }
      }
    }
    bool ok = false;
    for (int i = 0; i < 4; i++){
      if (d[gy][gx][i] != -1){
        ok = true;
      }
    }
    if (ok){
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}
accuracy: 1
time_ms: 1130
memory_kb: 63160
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=1005,INF=1<<30;
vector<int> dh={0,1,0,-1},dw={1,0,-1,0};
int H,W,N;
string S;
int sh,sw,gh,gw;

int dis[MAX][MAX][4];
char T[MAX][MAX];
int nex[MAX][4];

struct dat{
    int h;
    int w;
    int dir;
    int dis;
};

auto compare = [](dat &A,dat &B){
    return A.dis>B.dis;
};

void dijkstra(){
    dis[sh][sw][3]=0;
    priority_queue<dat,vector<dat>,decltype(compare)> PQ(compare);
    
    PQ.push({sh,sw,3,0});
    
    while(!PQ.empty()){
        auto u=PQ.top();PQ.pop();
        
        int d=dis[u.h][u.w][u.dir];
        
        if(d<u.dis) continue;
        
        for(int k=0;k<4;k++){
            if(nex[d][k]==d||nex[d][k]==INF) continue;
            
            if(chmin(dis[u.h][u.w][k],nex[d][k])){
                PQ.push({u.h,u.w,k,nex[d][k]});
            }
        }
        
        int toh=u.h+dh[u.dir],tow=u.w+dw[u.dir];
        
        if(toh<0||toh>=H||tow<0||tow>=W||T[toh][tow]=='#') continue;
        
        if(chmin(dis[toh][tow][u.dir],d)){
            PQ.push({toh,tow,u.dir,d});
        }
    }
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        cin>>H>>W>>N;
        if(H==0) break;
        
        cin>>S;
        for(int i=0;i<=N;i++) for(int k=0;k<4;k++) nex[i][k]=INF;
        int now=3;
        nex[0][3]=0;
        
        for(int i=0;i<N;i++){
            if(S[i]=='L') now=(now+3)%4;
            else now=(now+1)%4;
            
            nex[i+1][now]=i+1;
        }
        
        for(int k=0;k<4;k++){
            for(int i=N-1;i>=0;i--){
                chmin(nex[i][k],nex[i+1][k]);
            }
        }
        
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                cin>>T[i][j];
                if(T[i][j]=='S') {sh=i;sw=j;};
                if(T[i][j]=='G') {gh=i;gw=j;};
                for(int k=0;k<4;k++) dis[i][j][k]=INF;
            }
        }
        
        dijkstra();
        
        int mi=INF;
        for(int k=0;k<4;k++) chmin(mi,dis[gh][gw][k]);
        
        if(mi<=N) cout<<"Yes\n";
        else cout<<"No\n";
        
    }
}
accuracy: 1
time_ms: 250
memory_kb: 23808
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int inf = 1e9;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

struct state{
    int cost;
    int y;
    int x;
    state(int c, int y, int x):cost(c),y(y),x(x){}
    state(){}
    bool operator<(const state &a) const{
        return cost > a.cost;
    }
};

int main(){
    while(1){
        int h,w,n;
        cin >> h >> w >> n;
        if(h == 0) break;

        string s;
        cin >> s;
        vector<string> grid(h+2, string(w+2, '#'));
        for(int i=1; i<=h; i++){
            cin >> grid[i];
            grid[i] = "#" + grid[i] + "#";
        }

        vector<int> dir(n+1, 0);
        for(int i=0; i<n; i++){
            dir[i+1] = (s[i]=='L')? (dir[i]-1+4)%4: (dir[i]+1)%4;
        }
        vector<vector<int>> next(n+1, vector<int>(4, inf));
        next[n][dir[n]] = n;
        for(int i=n-1; i>=0; i--){
            for(int j=0; j<4; j++){
                next[i][j] = next[i+1][j];
            }
            next[i][dir[i]] = i;
        }
        int sy=-1,sx=-1;
        int gy=-1,gx=-1;
        for(int i=1; i<=h; i++){
            for(int j=1; j<=w; j++){
                if(grid[i][j] == 'S'){
                    sy = i;
                    sx = j;
                }
                if(grid[i][j] == 'G'){
                    gy = i;
                    gx = j;
                }
            }
        }

        priority_queue<state> wait;
        wait.push(state(0, sy, sx));
        vector<vector<int>> dp(h+2, vector<int>(w+2, inf));
        dp[sy][sx] = 0;
        while(!wait.empty()){
            int c = wait.top().cost;
            int y = wait.top().y;
            int x = wait.top().x;
            wait.pop();
            if(c > dp[y][x]) continue;

            for(int d=0; d<4; d++){
                int ny = y+dy[d];
                int nx = x+dx[d];
                int nc = next[c][d];
                if(grid[ny][nx] == '#') continue;
                if(nc == inf) continue;
                if(nc < dp[ny][nx]){
                    dp[ny][nx] = nc;
                    wait.push(state(nc, ny, nx));
                }
            }
        }
        if(dp[gy][gx] != inf){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 9556
final_rank: 5

code_snippet: #define  _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-5L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

// geometry library

#define int long long

typedef complex<long double> Point;
typedef pair<complex<long double>, complex<long double>> Line;

typedef struct Circle {
    complex<long double> center;
    long double r;
}Circle;

long double dot(Point a, Point b) {
    return (a.real() * b.real() + a.imag() * b.imag());
}
long double cross(Point a, Point b) {
    return (a.real() * b.imag() - a.imag() * b.real());
}

long double Dist_Line_Point(Line a, Point b) {
    if (dot(a.second - a.first, b - a.first) < eps) return abs(b - a.first);
    if (dot(a.first - a.second, b - a.second) < eps) return abs(b - a.second);
    return abs(cross(a.second - a.first, b - a.first)) / abs(a.second - a.first);
}

int is_intersected_ls(Line a, Line b) {
    return (cross(a.second - a.first, b.first - a.first) * cross(a.second - a.first, b.second - a.first) < eps) &&
        (cross(b.second - b.first, a.first - b.first) * cross(b.second - b.first, a.second - b.first) < eps);
}

Point intersection_l(Line a, Line b) {
    Point da = a.second - a.first;
    Point db = b.second - b.first;
    return a.first + da * cross(db, b.first - a.first) / cross(db, da);
}

long double Dist_Line_Line(Line a, Line b) {
    if (is_intersected_ls(a, b) == 1) {
        return 0;
    }
    return min({ Dist_Line_Point(a,b.first), Dist_Line_Point(a,b.second),Dist_Line_Point(b,a.first),Dist_Line_Point(b,a.second) });
}

pair<Point, Point> intersection_Circle_Circle(Circle a, Circle b) {
    long double dist = abs(a.center - b.center);
    assert(dist <= eps + a.r + b.r);
    assert(dist + eps >= abs(a.r - b.r));
    Point target = b.center - a.center;
    long double pointer = target.real() * target.real() + target.imag() * target.imag();
    long double aa = pointer + a.r * a.r - b.r * b.r;
    aa /= 2.0L;
    Point l{ (aa * target.real() + target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer,
            (aa * target.imag() - target.real() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer };
    Point r{ (aa * target.real() - target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer,
        (aa * target.imag() + target.real() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer };
    r = r + a.center;
    l = l + a.center;
    return mp(l, r);
}

//end of geometry

template<typename A>
A pows(A val, ll b) {
    assert(b >= 1);
    A ans = val;
    b--;
    while (b) {
        if (b % 2) {
            ans *= val;
        }
        val *= val;
        b /= 2LL;
    }
    return ans;
}

template<typename A>
class Compressor {
public:
    bool is_zipped = false;
    map<A, ll> zipper;
    map<ll, A> unzipper;
    queue<A> fetcher;
    Compressor() {
        is_zipped = false;
        zipper.clear();
        unzipper.clear();
    }
    void add(A now) {
        assert(is_zipped == false);
        zipper[now] = 1;
        fetcher.push(now);
    }
    void exec() {
        assert(is_zipped == false);
        int cnt = 0;
        for (auto i = zipper.begin(); i != zipper.end(); ++i) {
            i->second = cnt;
            unzipper[cnt] = i->first;
            cnt++;
        }
        is_zipped = true;
    }
    ll fetch() {
        assert(is_zipped == true);
        A hoge = fetcher.front();
        fetcher.pop();
        return zipper[hoge];
    }
    ll zip(A now) {
        assert(is_zipped == true);
        assert(zipper.find(now) != zipper.end());
        return zipper[now];
    }
    A unzip(ll a) {
        assert(is_zipped == true);
        assert(a < unzipper.size());
        return unzipper[a];
    }
    ll next(A now) {
        auto x = zipper.upper_bound(now);
        if (x == zipper.end()) return zipper.size();
        return (ll)((*x).second);
    }
    ll back(A now) {
        auto x = zipper.lower_bound(now);
        if (x == zipper.begin()) return -1;
        x--;
        return (ll)((*x).second);
    }
};

template<typename A>
class Matrix {
public:
    vector<vector<A>> data;
    Matrix(vector<vector<A>> a) :data(a) {

    }
    Matrix operator + (const Matrix obj) {
        vector<vector<A>> ans;
        assert(obj.data.size() == this->data.size());
        assert(obj.data[0].size() == this->data[0].size());
        REP(i, obj.data.size()) {
            ans.push_back(vector<A>());
            REP(q, obj.data[i].size()) {
                A hoge = obj.data[i][q] + (this->data[i][q]);
                ans.back().push_back(hoge);
            }
        }
        return Matrix(ans);
    }
    Matrix operator - (const Matrix obj) {
        vector<vector<A>> ans;
        assert(obj.data.size() == this->data.size());
        assert(obj.data[0].size() == this->data[0].size());
        REP(i, obj.data.size()) {
            ans.push_back(vector<A>());
            REP(q, obj.data[i].size()) {
                A hoge = this->data[i][q] - obj.data[i][q];
                ans.back().push_back(hoge);
            }
        }
        return Matrix(ans);
    }
    Matrix operator * (const Matrix obj) {
        vector<vector<A>> ans;
        assert(obj.data.size() == this->data[0].size());
        REP(i, this -> data.size()) {
            ans.push_back(vector<A>());
            REP(q, obj.data[0].size()) {
                A hoge = (this->data[i][0]) * (obj.data[0][q]);
                for (int t = 1; t < obj.data[i].size(); ++t) {
                    hoge += this->data[i][t] * obj.data[t][q];
                }
                ans.back().push_back(hoge);
            }
        }
        return Matrix(ans);
    }
    Matrix& operator *= (const Matrix obj) {
        *this = (*this * obj);
        return *this;
    }
    Matrix& operator += (const Matrix obj) {
        *this = (*this + obj);
        return *this;
    }
    Matrix& operator -= (const Matrix obj) {
        *this = (*this - obj);
        return *this;
    }
};

template <std::uint_fast64_t mod>
class modint {
public:
    using u64 = std::uint_fast64_t;
    u64 value = 0;
    modint(ll a) : value(((a% mod) + 2 * mod) % mod) {

    }

    constexpr modint operator+(const modint rhs) const {
        return modint(*this) += rhs;
    }
    constexpr modint operator-(const modint rhs) const {
        return modint(*this) -= rhs;
    }
    constexpr modint operator*(const modint rhs) const {
        return modint(*this) *= rhs;
    }
    constexpr modint operator/(const modint rhs) const {
        return modint(*this) /= rhs;
    }
    constexpr modint& operator+=(const modint rhs) {
        value += rhs.value;
        if (value >= mod) {
            value -= mod;
        }
        return *this;
    }
    constexpr modint& operator-=(const modint rhs) {
        if (value < rhs.value) {
            value += mod;
        }
        value -= rhs.value;
        return *this;
    }
    constexpr modint& operator*=(const modint rhs) {
        value = (value * rhs.value) % mod;
        return *this;
    }
    constexpr modint& operator/=(modint rhs) {
        ll rem = mod - 2;
        while (rem) {
            if (rem % 2) {
                *this *= rhs;
            }
            rhs *= rhs;
            rem /= 2LL;
        }
        return *this;
    }
    bool operator <(modint rhs) const {
        return value < rhs.value;
    }
    friend ostream& operator<<(ostream& os, modint& p) {
        os << p.value;
        return (os);
    }
};

class Dice {
public:
    vector<ll> vertexs;
    //Up: 0,Left: 1,Center: 2,Right: 3,Adj: 4, Down: 5
    Dice(vector<ll> init) :vertexs(init) {

    }
    //Look from Center
    void RtoL() {
        for (int q = 1; q < 4; ++q) {
            swap(vertexs[q], vertexs[q + 1]);
        }
    }
    void LtoR() {
        for (int q = 3; q >= 1; --q) {
            swap(vertexs[q], vertexs[q + 1]);
        }
    }
    void UtoD() {
        swap(vertexs[5], vertexs[4]);
        swap(vertexs[2], vertexs[5]);
        swap(vertexs[0], vertexs[2]);
    }
    void DtoU() {
        swap(vertexs[0], vertexs[2]);
        swap(vertexs[2], vertexs[5]);
        swap(vertexs[5], vertexs[4]);
    }
    bool ReachAble(Dice now) {
        set<Dice> hoge;
        queue<Dice> next;
        next.push(now);
        hoge.insert(now);
        while (next.empty() == false) {
            Dice seeing = next.front();
            next.pop();
            if (seeing == *this) return true;
            seeing.RtoL();
            if (hoge.count(seeing) == 0) {
                hoge.insert(seeing);
                next.push(seeing);
            }
            seeing.LtoR();
            seeing.LtoR();
            if (hoge.count(seeing) == 0) {
                hoge.insert(seeing);
                next.push(seeing);
            }
            seeing.RtoL();
            seeing.UtoD();
            if (hoge.count(seeing) == 0) {
                hoge.insert(seeing);
                next.push(seeing);
            }
            seeing.DtoU();
            seeing.DtoU();
            if (hoge.count(seeing) == 0) {
                hoge.insert(seeing);
                next.push(seeing);
            }
        }
        return false;
    }
    bool operator ==(const Dice& a) {
        for (int q = 0; q < 6; ++q) {
            if (a.vertexs[q] != (*this).vertexs[q]) {
                return false;
            }
        }
        return true;
    }
    bool operator <(const Dice& a) const {
        return (*this).vertexs < a.vertexs;
    }
};

pair<Dice, Dice> TwoDimDice(int center, int up) {
    int target = 1;
    while (true) {
        if (center != target && 7 - center != target && up != target && 7 - up != target) {
            break;
        }
        target++;
    }
    return mp(Dice(vector<ll>{up, target, center, 7 - target, 7 - center, 7 - up}), Dice(vector<ll>{up, 7 - target, center, target, 7 - center, 7 - up}));
}

tuple<Dice, Dice, Dice, Dice> OneDimDice(int center) {
    int bo = min(center, 7 - center);
    pair<int, int> goa;
    if (bo == 1) {
        goa = mp(2, 3);
    }
    else if (bo == 2) {
        goa = mp(1, 3);
    }
    else if (bo == 3) {
        goa = mp(1, 2);
    }
    tuple<Dice, Dice, Dice, Dice> now = make_tuple(Dice(vector<ll>{goa.first, goa.second, center, 7 - goa.second, 7 - center, 7 - goa.first}),
        Dice(vector<ll>{goa.first, 7 - goa.second, center, goa.second, 7 - center, 7 - goa.first}),
        Dice(vector<ll>{7 - goa.first, goa.second, center, 7 - goa.second, 7 - center, goa.first}),
        Dice(vector<ll>{7 - goa.first, 7 - goa.second, center, goa.second, 7 - center, goa.first}));
    return now;
}

template<typename A, typename B>
class Dijkstra {
public:
    vector<vector<pair<int, A>>> vertexs;
    B Cost_Function;
    Dijkstra(int n, B cost) : Cost_Function(cost) {
        vertexs = vector<vector<pair<int, A>>>(n, vector<pair<int, A>>{});
    }
    ~Dijkstra() {
        vertexs.clear();
    }
    void add_edge(int a, int b, A c) {
        vertexs[a].push_back(mp(b, c));
    }
    vector<ll> build_result(int StartPoint) {
        vector<ll> dist(vertexs.size(), 2e18);
        dist[StartPoint] = 0;
        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> next;
        next.push(make_pair(0, StartPoint));
        while (next.empty() == false) {
            pair<ll, int> now = next.top();
            next.pop();
            if (dist[now.second] != now.first) continue;
            for (auto x : vertexs[now.second]) {
                ll now_cost = now.first + Cost_Function(x.second);
                if (dist[x.first] > now_cost) {
                    dist[x.first] = now_cost;
                    next.push(mp(now_cost, x.first));
                }
            }
        }
        return dist;
    }
};

class Dinic {
public:
    struct edge {
        int to;
        int cap;
        int rev;
    };
    vector<vector<edge>> Graph;
    vector<int> level;
    vector<int> itr;
    Dinic(int n) {
        Graph = vector<vector<edge>>(n, vector<edge>());
    }
    void add_edge(int a, int b, int cap) {
        Graph[a].push_back(edge{ b, cap ,(int)Graph[b].size() });
        Graph[b].push_back(edge{ a,0,(int)Graph[a].size() - 1 });
    }
    void bfs(int s) {
        level = vector<int>(Graph.size(), -1);
        level[s] = 0;
        queue<int> next;
        next.push(s);
        while (next.empty() == false) {
            int now = next.front();
            next.pop();
            for (auto x : Graph[now]) {
                if (x.cap == 0) continue;
                if (level[x.to] == -1) {
                    level[x.to] = level[now] + 1;
                    next.push(x.to);
                }
            }
        }
    }
    int dfs(int now, int goal, int val) {
        if (goal == now) return val;
        for (int& i = itr[now]; i < (int)Graph[now].size(); ++i) {
            edge& target = Graph[now][i];
            if (target.cap > 0 && level[now] < level[target.to]) {
                int d = dfs(target.to, goal, min(val, target.cap));
                if (d > 0) {
                    target.cap -= d;
                    Graph[target.to][target.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
    int run(int s, int t) {
        int ans = 0;
        int f = 0;
        while (bfs(s), level[t] >= 0) {
            itr = vector<int>(Graph.size(), 0);
            while ((f = dfs(s, t, 1e9)) > 0) {
                ans += f;
            }
        }
        return ans;
    }
};


class HLDecomposition {
public:
    vector<vector<int>> vertexs;
    vector<int> depth;
    vector<int> backs;
    vector<int> connections;
    vector<int> zip, unzip;
    HLDecomposition(int n) {
        vertexs = vector<vector<int>>(n, vector<int>());
        depth = vector<int>(n);
        zip = vector<int>(n);
        unzip = zip;
    }
    void add_edge(int a,int b) {
        vertexs[a].push_back(b);
        vertexs[b].push_back(a);
    }
    int depth_dfs(int now, int back) {
        depth[now] = 0;
        for (auto x : vertexs[now]) {
            if (x == back) continue;
            depth[now] = max(depth[now], 1 + depth_dfs(x, now));
        }
        return depth[now];
    }
    void dfs(int now,int backing) {
        zip[now] = backs.size();
        unzip[backs.size()] = now;
        backs.push_back(backing);
        int now_max = -1;
        int itr = -1;
        for (auto x : vertexs[now]) {
            if (depth[x] > depth[now]) continue;
            if (now_max < depth[x]) {
                now_max = depth[x];
                itr = x;
            }
        }
        if (itr == -1) return;
        connections.push_back(connections.back());
        dfs(itr,backing);
        for (auto x : vertexs[now]) {
            if (depth[x] > depth[now]) continue;
            if (x == itr) continue;
            connections.push_back(zip[now]);
            dfs(x, backs.size());
        }
        return;
    }
    void build() {
        depth_dfs(0, -1);
        connections.push_back(-1);
        dfs(0, -1);
    }
    vector<pair<int,int>> query(int a, int b) {
        a = zip[a];
        b = zip[b];
        vector<pair<int, int>> ans;
        while (backs[a] != backs[b]) {
            if (a < b) swap(a, b);
            ans.push_back(mp(backs[a], a + 1));
            a = connections[a];
        }
        if (a > b) swap(a, b);
        ans.push_back(mp(a, b + 1));
        return ans;
    } 
    int lca(int a, int b) {
        a = zip[a];
        b = zip[b];
        while (backs[a] != backs[b]) {
            if (a < b) swap(a, b);
            a = connections[a];
        }
        return unzip[min(a, b)];
    }
};
//by ei1333
//https://ei1333.github.io/luzhiled/snippets/structure/segment-tree.html
template< typename Monoid >
struct SegmentTree {
    using F = function< Monoid(Monoid, Monoid) >;

    int sz;
    vector< Monoid > seg;

    const F f;
    const Monoid M1;

    SegmentTree(int n, const F f, const Monoid& M1) : f(f), M1(M1) {
        sz = 1;
        while (sz < n) sz <<= 1;
        seg.assign(2 * sz + 1, M1);
    }

    void set(int k, const Monoid& x) {
        seg[k + sz] = x;
    }

    void build() {
        for (int k = sz - 1; k > 0; k--) {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }

    void update(int k, const Monoid& x) {
        k += sz;
        seg[k] = x;
        while (k >>= 1) {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }

    Monoid query(int a, int b) {
        Monoid L = M1, R = M1;
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if (a & 1) L = f(L, seg[a++]);
            if (b & 1) R = f(seg[--b], R);
        }
        return f(L, R);
    }

    Monoid operator[](const int& k) const {
        return seg[k + sz];
    }

    template< typename C >
    int find_subtree(int a, const C& check, Monoid& M, bool type) {
        while (a < sz) {
            Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
            if (check(nxt)) a = 2 * a + type;
            else M = nxt, a = 2 * a + 1 - type;
        }
        return a - sz;
    }


    template< typename C >
    int find_first(int a, const C& check) {
        Monoid L = M1;
        if (a <= 0) {
            if (check(f(L, seg[1]))) return find_subtree(1, check, L, false);
            return -1;
        }
        int b = sz;
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if (a & 1) {
                Monoid nxt = f(L, seg[a]);
                if (check(nxt)) return find_subtree(a, check, L, false);
                L = nxt;
                ++a;
            }
        }
        return -1;
    }

    template< typename C >
    int find_last(int b, const C& check) {
        Monoid R = M1;
        if (b >= sz) {
            if (check(f(seg[1], R))) return find_subtree(1, check, R, true);
            return -1;
        }
        int a = sz;
        for (b += sz; a < b; a >>= 1, b >>= 1) {
            if (b & 1) {
                Monoid nxt = f(seg[--b], R);
                if (check(nxt)) return find_subtree(b, check, R, true);
                R = nxt;
            }
        }
        return -1;
    }
};

template< typename Monoid, typename OperatorMonoid = Monoid >
struct LazySegmentTree {
    using F = function< Monoid(Monoid, Monoid) >;
    using G = function< Monoid(Monoid, OperatorMonoid,int) >;
    using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;

    int sz, height;
    vector< Monoid > data;
    vector< OperatorMonoid > lazy;
    const F f;
    const G g;
    const H h;
    const Monoid M1;
    const OperatorMonoid OM0;


    LazySegmentTree(int n, const F f, const G g, const H h,
        const Monoid& M1, const OperatorMonoid OM0)
        : f(f), g(g), h(h), M1(M1), OM0(OM0) {
        sz = 1;
        height = 0;
        while (sz < n) sz <<= 1, height++;
        data.assign(2 * sz, M1);
        lazy.assign(2 * sz, OM0);
    }

    void set(int k, const Monoid& x) {
        data[k + sz] = x;
    }

    void build() {
        for (int k = sz - 1; k > 0; k--) {
            data[k] = f(data[2 * k + 0], data[2 * k + 1]);
        }
    }

    inline void propagate(int k) {
        if (lazy[k] != OM0) {
            lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
            lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
            data[k] = reflect(k);
            lazy[k] = OM0;
        }
    }

    inline Monoid reflect(int k) {
        if (lazy[k] == OM0) return data[k];
        for (int q = sz; q >= 0; q /= 2) {
            if (q & k) {
                return g(data[k], lazy[k], sz / q);
            }
        }
    }

    inline void recalc(int k) {
        while (k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
    }

    inline void thrust(int k) {
        for (int i = height; i > 0; i--) propagate(k >> i);
    }

    void update(int a, int b, const OperatorMonoid& x) {
        thrust(a += sz);
        thrust(b += sz - 1);
        for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) lazy[l] = h(lazy[l], x), ++l;
            if (r & 1) --r, lazy[r] = h(lazy[r], x);
        }
        recalc(a);
        recalc(b);
    }

    Monoid query(int a, int b) {
        thrust(a += sz);
        thrust(b += sz - 1);
        Monoid L = M1, R = M1;
        for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) L = f(L, reflect(l++));
            if (r & 1) R = f(reflect(--r), R);
        }
        return f(L, R);
    }

    Monoid operator[](const int& k) {
        return query(k, k + 1);
    }

    template< typename C >
    int find_subtree(int a, const C& check, Monoid& M, bool type) {
        while (a < sz) {
            propagate(a);
            Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type));
            if (check(nxt)) a = 2 * a + type;
            else M = nxt, a = 2 * a + 1 - type;
        }
        return a - sz;
    }

    template< typename C >
    int find_first(int a, const C& check) {
        Monoid L = M1;
        if (a <= 0) {
            if (check(f(L, reflect(1)))) return find_subtree(1, check, L, false);
            return -1;
        }
        thrust(a + sz);
        int b = sz;
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if (a & 1) {
                Monoid nxt = f(L, reflect(a));
                if (check(nxt)) return find_subtree(a, check, L, false);
                L = nxt;
                ++a;
            }
        }
        return -1;
    }


    template< typename C >
    int find_last(int b, const C& check) {
        Monoid R = M1;
        if (b >= sz) {
            if (check(f(reflect(1), R))) return find_subtree(1, check, R, true);
            return -1;
        }
        thrust(b + sz - 1);
        int a = sz;
        for (b += sz; a < b; a >>= 1, b >>= 1) {
            if (b & 1) {
                Monoid nxt = f(reflect(--b), R);
                if (check(nxt)) return find_subtree(b, check, R, true);
                R = nxt;
            }
        }
        return -1;
    }
};



unsigned long xor128() {
    static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned long t = (x ^ (x << 11));
    x = y; y = z; z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

void init() {
    iostream::sync_with_stdio(false);
    cout << fixed << setprecision(20);
}

void solve(){
    while (true) {
        int h, w, n;
        cin >> h >> w >> n;
        if (h == 0) return;
        string s;
        cin >> s;
        int dx[4] = { -1,0,1,0 };
        int dy[4] = { 0,1,0,-1 };
        vector<vector<int>> grid(h, vector<int>(w, 0));
        queue<pair<int, int>> next[4];
        pair<int, int> goal;
        REP(i, h) {
            string a;
            cin >> a;
            REP(q, w) {
                if (a[q] == '#') {
                    grid[i][q] = 1;
                }
                if (a[q] == 'G') {
                    goal = mp(i, q);
                }
                if (a[q] == 'S') {
                    REP(j,4)
                    next[j].push(mp(i, q));
                    grid[i][q] = 0;
                }
            }
        }
        int now = 0;
        s.push_back('L');
        REP(i, s.length()) {
            while (next[now].empty() == false) {
                pair<int, int> go = next[now].front();
                next[now].pop();
                int x = go.first + dx[now];
                int y = go.second + dy[now];
                if (x >= 0 && x < h && y >= 0 && y < w) {
                    if (grid[x][y] == 0) {
                        grid[x][y] = 1;
                        REP(j, 4) {
                            next[j].push(mp(x, y));
                        }
                    }
                }
            }
            if (s[i] == 'L') {
                now += 3;
            }
            else now++;
            now %= 4;
        }
        if (grid[goal.first][goal.second] == 1) {
            cout << "Yes" << endl;
        }
        else {
            cout << "No" << endl;
        }
    }
}

#undef int
int main() {
    init();
    solve();
}
accuracy: 1
time_ms: 40
memory_kb: 13044
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int dx[] = {-1,0,1,0},dy[] = {0,1,0,-1};

int h,w,n,sx,sy,gx,gy;
string s,field[1010];
const int INF = 1e+9;
int nxt[1000100][4],dir[1000100];
int dist[1010][1010];

void solve(){
	cin >> s;
	for(int i = 0;i < 4;i++) nxt[n + 1][i] = -1;
	for(int i = 0;i < n;i++) dir[i + 1] = (dir[i] + (s[i] == 'L' ? 3 : 1)) % 4;
	for(int i = n;i >= 0;i--){
		for(int j = 0;j < 4;j++){
			if(dir[i] == j) nxt[i][j] = i;
			else nxt[i][j] = nxt[i + 1][j];
		}
	}
	for(int i = 0;i < h;i++){
		cin >> field[i];
		for(int j = 0;j < w;j++){
			if(field[i][j] == 'S') sx = i,sy = j;
			else if(field[i][j] == 'G') gx = i,gy = j;
		}
	}
	for(int i = 0;i < h;i++){
		for(int j = 0;j < w;j++) dist[i][j] = INF;
	}
	using P = pair<int,pair<int,int>>;
	priority_queue<P,vector<P>,greater<P>> que;
	dist[sx][sy] = 0;
	que.emplace(0,make_pair(sx,sy));
	while(!que.empty()){
		P p = que.top();que.pop();
		int x = p.second.first,y = p.second.second;
		if(dist[x][y] < p.first) continue;
		for(int i = 0;i < 4;i++){
			int nx = x + dx[i],ny = y + dy[i];
			if(nx >= 0 && nx < h && ny >= 0 && ny < w && field[nx][ny] != '#'
			&& nxt[dist[x][y]][i] != -1 && dist[nx][ny] > nxt[dist[x][y]][i]){
				dist[nx][ny] = nxt[dist[x][y]][i];
				que.emplace(dist[nx][ny],make_pair(nx,ny));
			}
		}
	}
	if(dist[gx][gy] != INF) cout << "Yes" << endl;
	else cout << "No" << endl; 
}

signed main(){
	while(cin >> h >> w >> n,n) solve();
}
accuracy: 1
time_ms: 170
memory_kb: 8140
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
enum class Direction {
	North, East, South, West
};
Direction from_int(const int n) {
	switch (n % 4) {
	case 0: return Direction::North;
	case 1: return Direction::East;
	case 2: return Direction::South;
	case 3: return Direction::West;
	default: throw 0;
	}
}
int to_int(const Direction dir) {
	switch (dir) {
	case Direction::North: return 0;
	case Direction::East: return 1;
	case Direction::South: return 2;
	case Direction::West: return 3;
	default: throw 0;
	}
}
Direction turn(const Direction current, const char order) {
	switch (order) {
	case 'L': return from_int(to_int(current) + 3);
	case 'R': return from_int(to_int(current) + 1);
	default: throw 0;
	}
}
struct Coordinate {
	int x, y;
	Coordinate move(const Direction dir) const {
		switch (dir) {
		case Direction::North: return Coordinate{ x, y - 1 };
		case Direction::East: return Coordinate{ x + 1, y };
		case Direction::South: return Coordinate{ x, y + 1 };
		case Direction::West: return Coordinate{ x - 1, y };
		default: throw 0;
		}
	}
};
int main() {
	while (true) {
		int h, w, n; std::cin >> h >> w >> n; if (h == 0 && w == 0 && n == 0) break;
		std::string order; std::cin >> order;
		std::vector<Direction> directions{ Direction::North };
		for (const char c : order) {
			directions.push_back(turn(directions.back(), c));
		}
		std::vector<std::vector<int>> next_direction(n + 1);
		{
			std::vector<int> prev{ n + 1, n + 1, n + 1, n + 1};
			for (int i = n; i >= 0; --i) {
				prev[to_int(directions[i])] = i;
				next_direction[i] = prev;
			}
		}
		std::vector<std::string> state(h); for (auto& line : state) std::cin >> line;
		Coordinate start{ -1, -1 }, goal{ -1, -1 };
		for (auto y = 0; y < h; ++y) for (auto x = 0; x < w; ++x) {
			if (state[y][x] == 'S') start = Coordinate{ x, y };
			if (state[y][x] == 'G') goal = Coordinate{ x, y };
		}
		std::vector<std::vector<std::vector<int>>> min_step(h, std::vector<std::vector<int>>(w, std::vector<int>(4, INT_MAX))); min_step[start.y][start.x][to_int(Direction::North)] = 0;
		std::vector<std::queue<Coordinate>> history(n + 1); history[0].push(start);
		bool can_reach = false;
		for (auto i = 0; i <= n && !can_reach; ++i) {
			while (!history[i].empty()) {
				const auto top = history[i].front(); history[i].pop();
				if (min_step[top.y][top.x][to_int(directions[i])] != i) continue;
				const auto next_pos = top.move(directions[i]);
				if (0 <= next_pos.y && next_pos.y < h && 0 <= next_pos.x && next_pos.x < w && state[next_pos.y][next_pos.x] != '#') {
					if (min_step[next_pos.y][next_pos.x][to_int(directions[i])] > i) {
						min_step[next_pos.y][next_pos.x][to_int(directions[i])] = i;
						if (next_pos.x == goal.x && next_pos.y == goal.y) {
							can_reach = true;
							break;
						}
						history[i].emplace(next_pos);
					}
				}
				for (const auto next_dir : next_direction[i]) if (next_dir <= n) {
					if (min_step[top.y][top.x][to_int(directions[next_dir])] > next_dir) {
						min_step[top.y][top.x][to_int(directions[next_dir])] = next_dir;
						history[next_dir].emplace(top);
					}
				}
			}
		}
		if (can_reach || std::any_of(min_step[goal.y][goal.x].begin(), min_step[goal.y][goal.x].end(), [](const int step) {return step != INT_MAX; })) {
			std::cout << "Yes\n";
		}
		else {
			std::cout << "No\n";
		}
	}
}
accuracy: 1
time_ms: 260
memory_kb: 60776
final_rank: 15

code_snippet: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int H,W,N;
string s,m[1000];
int dis[4][1000][1000];
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
int nxt[4][1<<20];
main()
{
	while(cin>>H>>W>>N,H)
	{
		cin>>s;
		int sx,sy;
		for(int i=0;i<H;i++)
		{
			cin>>m[i];
			for(int j=0;j<W;j++)for(int r=0;r<4;r++)dis[r][i][j]=2*N;
			for(int j=0;j<W;j++)
			{
				if(m[i][j]=='S')
				{
					sx=i;
					sy=j;
				}
			}
		}
		int nowr=0;
		for(int i=0;i<N;i++)nowr+=s[i]=='L'?1:-1;
		nowr=(nowr%4+4)%4;
		int nowd[4];
		for(int r=0;r<4;r++)nowd[r]=nxt[r][N]=r==nowr?N:2*N;
		for(int i=N;i--;)
		{
			nowr-=s[i]=='L'?1:-1;
			nowr=(nowr%4+4)%4;
			nowd[nowr]=i;
			for(int r=0;r<4;r++)nxt[r][i]=nowd[r];
		}
		dis[0][sx][sy]=0;
		priority_queue<pair<pair<int,int>,pair<int,int> > >P;
		P.push({{0,0},{sx,sy}});
		bool flag=false;
		while(!P.empty())
		{
			int d=-P.top().first.first;
			int nr=P.top().first.second;
			int x=P.top().second.first;
			int y=P.top().second.second;
			P.pop();
			if(dis[nr][x][y]<d)continue;
			if(m[x][y]=='G')
			{
				flag=true;
				break;
			}
			for(int r=0;r<4;r++)
			{
				int tx=x+dx[r],ty=y+dy[r];
				if(tx<0||ty<0||tx>=H||ty>=W||m[tx][ty]=='#'||dis[r][tx][ty]<=nxt[r][d])continue;
				dis[r][tx][ty]=nxt[r][d];
				P.push({{-nxt[r][d],r},{tx,ty}});
			}
		}
		cout<<(flag?"Yes":"No")<<endl;
	}
}
accuracy: 1
time_ms: 370
memory_kb: 21740
final_rank: 12

code_snippet: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int H,W,N;
string s,m[1000];
int dis[4][1000][1000];
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
int nxt[4][1<<20];
main()
{
	while(cin>>H>>W>>N,H)
	{
		cin>>s;
		int sx,sy;
		for(int i=0;i<H;i++)
		{
			cin>>m[i];
			for(int j=0;j<W;j++)for(int r=0;r<4;r++)dis[r][i][j]=2*N;
			for(int j=0;j<W;j++)
			{
				if(m[i][j]=='S')
				{
					sx=i;
					sy=j;
				}
			}
		}
		int nowr=0;
		for(int i=0;i<N;i++)nowr+=s[i]=='L'?1:-1;
		nowr=(nowr%4+4)%4;
		int nowd[4];
		for(int r=0;r<4;r++)nowd[r]=nxt[r][N]=r==nowr?N:2*N;
		for(int i=N;i--;)
		{
			nowr-=s[i]=='L'?1:-1;
			nowr=(nowr%4+4)%4;
			nowd[nowr]=i;
			for(int r=0;r<4;r++)nxt[r][i]=nowd[r];
		}
		dis[0][sx][sy]=0;
		priority_queue<pair<pair<int,int>,pair<int,int> > >P;
		P.push({{0,0},{sx,sy}});
		bool flag=false;
		while(!P.empty())
		{
			int d=-P.top().first.first;
			int nr=P.top().first.second;
			int x=P.top().second.first;
			int y=P.top().second.second;
			P.pop();
			if(dis[nr][x][y]<d)continue;
			if(m[x][y]=='G')
			{
				flag=true;
				break;
			}
			for(int r=0;r<4;r++)
			{
				int tx=x+dx[r],ty=y+dy[r];
				if(tx<0||ty<0||tx>=H||ty>=W||m[tx][ty]=='#'||dis[r][tx][ty]<=nxt[r][d])continue;
				dis[r][tx][ty]=nxt[r][d];
				P.push({{-nxt[r][d],r},{tx,ty}});
			}
		}
		cout<<(flag?"Yes":"No")<<endl;
	}
}
accuracy: 1
time_ms: 370
memory_kb: 21656
final_rank: 11

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<utility>
#include<stack>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define stop char nyaa;cin>>nyaa;
const ll mod = 1000000007;
typedef pair<int, int> P;

int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,1,0,-1 };

typedef pair<int, P> speP;

int h, w, n;
bool valid(int x, int y) {
	if (x < 0 || y < 0 || x >= h || y >= w)return false;
	return true;
}

void solve() {
	string s;
	cin >> s;
	vector<int> ra(n + 1,0);
	rep(i, n) {
		int nex = 0;
		if (s[i] == 'L') {
			nex = ra[i] - 1;
		}
		else {
			nex = ra[i] + 1;
		}
		if (nex == 4)nex = 0;
		else if (nex < 0)nex = 3;
		ra[i + 1] = nex;
	}
	vector<int> loc[4];
	rep(i, n+1) {
		loc[ra[i]].push_back(i);
	}
	rep(i, 4)loc[i].push_back(mod);
	vector<vector<int>> v(n+1);
	rep(i, n+1) {
		v[i].resize(4);
		rep(j, 4) {
			int id = lower_bound(loc[j].begin(), loc[j].end(), i) - loc[j].begin();
			v[i][j] = loc[j][id];
		}
	}
	vector<vector<char>> mp(h);
	int sx, sy, gx, gy;
	rep(i, h) {
		mp[i].resize(w);
		rep(j, w) {
			cin >> mp[i][j];
			if (mp[i][j] == 'S') {
				sx = i, sy = j;
			}
			else if(mp[i][j]=='G'){
				gx = i, gy = j;
			}
		}
	}
	vector<vector<int>> dist(h);
	rep(i, h) {
		dist[i].resize(w, mod);
	}
	dist[sx][sy] = 0;
	priority_queue<speP, vector<speP>, greater<speP>> q;
	q.push({ 0,{sx,sy} });
	while (!q.empty()) {
		speP p = q.top(); q.pop();
		int x = p.second.first, y = p.second.second;
		int t = p.first;
		if (dist[x][y] < t)continue;
		rep(j, 4) {
			int nd = v[t][j];
			int cx = x, cy = y;
			while (true) {
				cx += dx[j], cy += dy[j];
				if (!valid(cx, cy))break;
				if (mp[cx][cy] == '#')break;
				if (nd < dist[cx][cy]) {
					dist[cx][cy] = nd;
					q.push({ nd,{cx,cy} });
				}
				else break;
			}
		}
	}
	if (dist[gx][gy] == mod) {
		cout << "No" << endl;
	}
	else {
		cout << "Yes" << endl;
	}
}
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (cin >> h >> w >> n, h | w | n) {
		solve();
	}
	//stop
	return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 8400
final_rank: 4