Problem:
Short Coding
You participate in a project to explore an asteroid called Yokohama 2020. A maze-like space in
its underground was found by a survey a couple of years ago. The project is to investigate the
maze more in detail using an exploration robot.
The shape of the maze was fully grasped by a survey with ground penetrating radars. For planning the exploration, the maze is represented as a grid of cells, where the first cell of the first row is the upper left corner of the grid, and the last cell of the last row is the lower right corner.
Each of the grid cell is either vacant, allowing robot's moves to it from an adjacent vacant cell, or filled with rocks, obstructing such moves. The entrance of the maze is located in a cell in the uppermost row and the exit is in a cell in the lowermost row.
The exploration robot is controlled by a program stored in it, which consists of sequentially
numbered lines, each containing one of the five kinds of commands described below. The register
pc
specifies the line number of the program to be executed. Each command specifies an action
of the robot and a value to be set to
pc
.
The robot starts at the entrance of the maze facing downwards, and the value of
pc
is set to 1.
The program commands on the lines specified by
pc
are executed repetitively, one by one, until
the robot reaches the exit of the maze.
When the value of
pc
exceeds the number of lines of the program by its increment, it is reset to 1. The robot stops on reaching the exit cell, which is the goal of the project.
As the capacity of the program store for the robot is quite limited, the number of lines of the
program should be minimal. Your job is to develop a program with the fewest possible number
of lines among those which eventually lead the robot to the exit.
command
description
GOTO
$l$
Set $l$ to
pc
. The command parameter $l$ is a positive integer less
      than or equal to the number of lines of the program.
IF-OPEN
$l$
If there is a vacant adjacent cell in its current direction, set $l$ to
pc
; otherwise, that is, facing a filled cell or a border of the grid,
increment
pc
by one. The command parameter $l$ is a positive integer
less than or equal to the number of lines of the program.
FORWARD
If there is a vacant adjacent cell in its current direction, move there;
      otherwise, stay in the current cell. In either case, increment
pc
by
      one.
LEFT
Turn 90 degrees to the left without changing the position, and increment
pc
by one.
RIGHT
Turn 90 degrees to the right without changing the position, and
      increment
pc
by one.
Input
The input consists of a single test case of the following format.
$n$ $m$
$s_1$
...
$s_n$
The first line contains two integers $n$ ($2 \leq n \leq 10$) and $m$ ($2 \leq m \leq 10$). The maze is represented as a grid with $n$ rows and $m$ columns. The next $n$ lines describe the grid. Each of the lines

contains a string of length $m$ corresponding to one grid row. The $i$-th character of the $j$-th
string
...(truncated)

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(a) begin(a),end(a)

int n,m,sx,sy,gx,gy;
vector<int>ans = {2,13,0,13,5,1}, tmp;
string mp[10];

string out(int op)
{
	if(op==0)
	{
		return "LEFT";
	}
	else if(op==1)
	{
		return "FORWARD";
	}
	else if(op==2)
	{
		return "RIGHT";		
	}
	else if(op<=7)
	{
		return "GOTO " + to_string(op-2);
	}
	else
	{
		return "IF-OPEN " + to_string(op-7);			
	}
}

bool vacant(int x, int y)
{
	if(x<0 || x>=n)
	{
		return false;
	}
	if(y<0 || y>=m)
	{
		return false;
	}
	return mp[x][y] != '#';
}

bool check(vector<int>&ops, bool debug=false)
{
	set<vector<int>>flag;
	int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};
	int x = sx, y = sy, d = 0, row = 0;
	while(!flag.count({x,y,d,row}))
	{
		flag.insert({x,y,d,row});
		
		if(debug) cout << x << " " << y << " " << d << " " << row << endl;
		if(debug) cout << gx << " " << gy << endl;
		
		if(x==gx && y==gy) return true;
		if(ops[row]==0)
		{
			d+=1;
			d%=4;
		}
		else if(ops[row]==1)
		{
			if(vacant(x+dx[d],y+dy[d]))
			{
				x += dx[d];
				y += dy[d];
			}
		}
		else if(ops[row]==2)
		{
			d+=3;
			d%=4;			
		}
		else if(ops[row]<=7)
		{
			row = ops[row] - 3;
			continue;
		}
		else if(ops[row]<=12)
		{
			if(vacant(x+dx[d],y+dy[d]))
			{
				row = ops[row] - 8;
				continue;
			}				
		}
		row = (row + 1) % ops.size();
	}
	return false;
}

//0:left
//1:forward
//2:right
//3-7:goto 1-5
//8-12:if open 1-5
void get_op(int siz, vector<int>&ops, vector<int>&ans)
{
	if(siz && ops.size()<ans.size() && check(ops))
	{
		ans = ops;
	}
	if(siz>=5) return;
	for(int i=0;i<13;i++)
	{
		if(siz==i-3) continue;
		if(siz==i-8) continue;
		ops.push_back(i);
		get_op(siz+1,ops,ans);
		ops.pop_back();
	}
}

int main(){
	cin >> n >> m;
	for(int i=0;i<n;i++)
	{
		cin >> mp[i];
		if(i==0) for(int j=0;j<m;j++)
		{
			if(mp[i][j]=='S') sx = i, sy = j;
		}
		if(i==n-1) for(int j=0;j<m;j++)
		{
			if(mp[i][j]=='G') gx = i, gy = j;
		}
	}
	get_op(0, tmp, ans);
	cout << ans.size() << endl;
	for(auto op:ans)
	{
		cout << out(op) << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3528
final_rank: 6

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

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(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() {
    int N, M;
    cin >> N >> M;
    vector<vector<bool>> G(N,vector<bool>(M));
    int SX, SY, GX, GY;
    rep(i,0,N) {
        rep(j,0,M) {
            char C;
            cin >> C;
            if (C == '#') G[i][j] = false;
            else G[i][j] = true;
            if (C == 'S') SX = i, SY = j;
            if (C == 'G') GX = i, GY = j;
        }
    }
    auto isVacant = [&](int X, int Y) -> bool {
        return 0 <= X && X < N && 0 <= Y && Y < M && G[X][Y];
    };
    auto Move = [&](int Len, int Command, int& X, int& Y, int& dir, int& pc) -> void {
        if (Command < Len) { //GOTO
            pc = Command;
        }
        else if (Command < Len * 2) { //IF-OPEN
            if (isVacant(X+dx[dir],Y+dy[dir])) pc = Command-Len;
            else pc = (pc+1) % Len;
        }
        else if (Command == Len * 2) { //FORWARD
            if (isVacant(X+dx[dir],Y+dy[dir])) X += dx[dir], Y += dy[dir];
            pc = (pc + 1) % Len;
        }
        else if (Command == Len * 2 + 1) { //LEFT
            dir = (dir + 1) % 4;
            pc = (pc + 1) % Len;
        }
        else if (Command == Len * 2 + 2) { //RIGHT
            dir = (dir + 3) % 4;
            pc = (pc + 1) % Len;
        }
    };
    vector<int> Com;
    auto Execute = [&](int Len) -> bool {
        vector DP(N,vector(M,vector(4,vector<bool>(Len,false))));
        int X = SX, Y = SY, dir = 0, pc = 0;
        while(1) {
            if (X == GX && Y == GY) return true;
            if (DP[X][Y][dir][pc]) return false;
            DP[X][Y][dir][pc] = true;
            Move(Len,Com[pc],X,Y,dir,pc);
        }
        return false;
    };
    auto DFS = [&](auto self, int Len, int Dep) -> void {
        if (Len == Dep) {
            if (Execute(Len)) {
                cout << Len << endl;
                rep(i,0,Len) {
                    if (Com[i] < Len) cout << "GOTO " << Com[i]+1 << endl;
                    else if (Com[i] < Len * 2) cout << "IF-OPEN " << Com[i] - Len + 1 << endl;
                    else if (Com[i] == Len * 2) cout << "FORWARD" << endl;
                    else if (Com[i] == Len * 2 + 1) cout << "LEFT" << endl;
                    else if (Com[i] == Len * 2 + 2) cout << "RIGHT" << endl;
                }
                exit(0);
            }
            return;
        }
        rep(i,0,Len*2+3) {
            Com.push_back(i);
            self(self,Len,Dep+1);
            Com.pop_back();
        }
    };
    rep(i,1,5) {
        Com.clear();
        DFS(DFS,i,0);
    }
    cout << 5 << endl;
    cout << "RIGHT" << endl;
    cout << "IF-OPEN 5" << endl;
    cout << "LEFT" << endl;
    cout << "GOTO 2" << endl;
    cout << "FORWARD" << endl;
}
accuracy: 1
time_ms: 170
memory_kb: 3480
final_rank: 8

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

int N, M;
int iscell[12][12];

void init()
{
    for (int x = 0; x < 12; ++x)
    {
        for (int y = 0; y < 12; ++y)
        {
            iscell[x][y] = 0;
        }
    }
    
    return;
}

// command
// 0 : GOTO L
// 1 : IF-OPEN L
// 2 : FORWARD
// 3 : LEFT
// 4 : RIGHT

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

// left : dir -> dir + 1
// right : dir -> dir - 1

vector<pair<int, int>> commands;

bool simulate()
{
    // (cell, dir, commands line)
    int siz = commands.size();
    set< array<int, 4> > seen;
    queue< array<int, 4> > que;
    int sx = 0, sy = 0;
    for (int x = 0; x < 12; ++x)
    {
        for (int y = 0; y < 12; ++y)
        {
            if (iscell[x][y] == 2) sx = x, sy = y;
        }
    }
    assert(sx != 0 && sy != 0);
    
    array<int, 4> init_state{sx, sy, 0, 0};
    que.push(init_state);
    seen.insert(init_state);
    
    while (que.size())
    {
        auto [x, y, dir, line] = que.front();
        
        que.pop();
        if (iscell[x][y] == 3) return true;
        
        auto [com, nl] = commands[line];
        array<int, 4> next_state{-1, -1, -1, -1};
        
        if (com == 0)
        {
            int nline = nl;
            
            next_state = {x, y, dir, nline};
        }
        else if (com == 1)
        {
            int nx = x + dx[dir], ny = y + dy[dir];
            int nline = (line + 1) % siz;
            if (iscell[nx][ny]) nline = nl;
            
            next_state = {x, y, dir, nline};
        }
        else if (com == 2)
        {
            int nx = x + dx[dir], ny = y + dy[dir];
            int nline = (line + 1) % siz;
            
            if (iscell[nx][ny]) next_state = {nx, ny, dir, nline};
            else next_state = {x, y, dir, nline};
        }
        else if (com == 3)
        {
            int ndir = (dir + 1) % 4;
            int nline = (line + 1) % siz;
            
            next_state = {x, y, ndir, nline};
        }
        else if (com == 4)
        {
            int ndir = (dir - 1 + 4) % 4;
            int nline = (line + 1) % siz;
            
            next_state = {x, y, ndir, nline};
        }
        
        if (seen.find(next_state) == seen.end())
        {
            seen.insert(next_state);
            que.push(next_state);
        }
    }
    
    return false;
}

void dfs(int depth, int target, vector<pair<int, int>> &res)
{
    assert((int)commands.size() == depth);
    if (depth == target)
    {
        if (simulate()) res = commands;
        return;
    }
    
    for (int i = 0; i < 5; ++i)
    {
        for (int el = 0; el < target; ++el)
        {
            if (i >= 2 && el >= 1) continue;
            
            commands.push_back({i, el});
            dfs(depth + 1, target, res);
            commands.pop_back();
        }
    }
    
    return;
}

// command
// 0 : GOTO L
// 1 : IF-OPEN L
// 2 : FORWARD
// 3 : LEFT
// 4 : RIGHT

void output(vector<pair<int, int>> &res)
{
    int siz = res.size();
    cout << siz << endl;
    for (auto [com, line] : res)
    {
        assert(0 <= com && com <= 4);
        if (com == 0) cout << "GOTO" << " " << line + 1 << endl;
        else if (com == 1) cout << "IF-OPEN" << " " << line + 1 << endl;
        else if (com == 2) cout << "FORWARD" << endl;
        else if (com == 3) cout << "LEFT" << endl;
        else if (com == 4) cout << "RIGHT" << endl;
    }
    
    return;
}


int main()
{
    init();
    
    cin >> N >> M;
    for (int x = 0; x < N; ++x)
    {
        string S; cin >> S;
        for (int y = 0; y < M; ++y)
        {
            int state = 0;
            if (S[y] == '.') state = 1;
            else if (S[y] == 'S') state = 2;
            else if (S[y] == 'G') state = 3;
            
            iscell[x+1][y+1] = state;
        }
    }
    
    vector<pair<int, int>> res;
    for (int len = 1; len <= 8; ++len)
    {
        dfs(0, len, res);
        if (res.size() > 0) break;
    }
    
    output(res);
    
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 3464
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vvvvb = vector<vvvb>;
#define all(A) A.begin(),A.end()
#define ALL(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
ll N,M;
vector<string> S;
//0,i:goto i, 1,i:if i, 2,-1;forward,3,-1:left,4,-1:right
vector<pair<int,int>> AN={{4,-1},{1,4},{3,-1},{0,1},{2,-1}};

bool rin(ll y,ll x){
    if(y<0||x<0||y>=N||x>=M)return 0;
    else return 1;
}

ll SY,SX,GY,GX;
vll dx={1,0,-1,0};
vll dy={0,-1,0,1};
bool can_reach(vector<pair<int,int>> COMMAND){
    
    ll K=COMMAND.size();
    for(int k=0;k<K;k++)if(COMMAND[k].second>=K)return 0;
    vvvvb seen(N,vvvb(M,vvb(4,vb(K,0))));
    ll y=SY;
    ll x=SX;
    ll d=3;
    ll k=0;
    while(1){
        if(y==GY&&x==GX)return 1;
        if(seen[y][x][d][k])return 0;
        seen[y][x][d][k]=1;
        if(COMMAND[k].first==0){
            k=COMMAND[k].second;
            continue;
        }
        else if(COMMAND[k].first==1){
            ll nx=x+dx[d];
            ll ny=y+dy[d];
            if(rin(ny,nx)&&S[ny][nx]!='#')k=COMMAND[k].second;
            else k=(k+1)%K;
            continue;
        }
        else if(COMMAND[k].first==2){
            ll nx=x+dx[d];
            ll ny=y+dy[d];
            if(rin(ny,nx)&&S[ny][nx]!='#'){
                x=nx;
                y=ny;
            }
            k=(k+1)%K;
            continue;
        }
        else if(COMMAND[k].first==3){
            d=(d+1)%4;
            k=(k+1)%K;
            continue;
        }
        else{
            d=(d+3)%4;
            k=(k+1)%K;
            continue;
        }
    }
    return 0;
}

void dfs(vector<pair<int,int>> COMMAND){
    int N=COMMAND.size();
    if(N==5)return;
    if(N!=0&&can_reach(COMMAND)){
        if(N<int(AN.size()))AN=COMMAND;
    }
    for(int i=0;i<5;i++){
        int ni=i,nj=-1;
        if(i<2){
            for(int n=0;n<3;n++){
                nj=n;
                COMMAND.push_back({ni,nj});
                dfs(COMMAND);
                COMMAND.pop_back();
            }
        }
        else{
            COMMAND.push_back({ni,nj});
            dfs(COMMAND);
            COMMAND.pop_back();
        }
        
    }
}


int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    
    
    cin>>N>>M;
    S.resize(N);
    rep(i,N){
        cin>>S[i];
        rep(j,M){
            if(S[i][j]=='S'){
                SY=i;
                SX=j;
            }
            if(S[i][j]=='G'){
                GY=i;
                GX=j;
            }
        }
    }
    vector<pair<int,int>> COMMAND={};
    dfs(COMMAND);
    
    int an=AN.size();
    cout<<an<<endl;
    rep(i,an){
        if(AN[i].first==0){
            cout<<"GOTO "<<AN[i].second+1<<endl;
        }
        else if(AN[i].first==1){
            cout<<"IF-OPEN "<<AN[i].second+1<<endl;
        }
        else if(AN[i].first==2){
            cout<<"FORWARD"<<endl;
        }
        else if(AN[i].first==3)cout<<"LEFT"<<endl;
        else cout<<"RIGHT"<<endl;
    }
    

    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3540
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vvvvb = vector<vvvb>;
#define all(A) A.begin(),A.end()
#define ALL(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
ll N,M;
vector<string> S;
//0,i:goto i, 1,i:if i, 2,-1;forward,3,-1:left,4,-1:right
vector<pair<int,int>> AN={{4,-1},{1,4},{3,-1},{0,1},{2,-1}};

bool rin(ll y,ll x){
    if(y<0||x<0||y>=N||x>=M)return 0;
    else return 1;
}

ll SY,SX,GY,GX;
vll dx={1,0,-1,0};
vll dy={0,-1,0,1};
bool can_reach(vector<pair<int,int>> COMMAND){
    ll K=COMMAND.size();
    for(int k=0;k<K;k++)if(COMMAND[k].second>=K)return 0;
    vvvvb seen(N,vvvb(M,vvb(4,vb(K,0))));
    ll y=SY;
    ll x=SX;
    ll d=3;
    ll k=0;
    while(1){
        if(y==GY&&x==GX)return 1;
        if(seen[y][x][d][k])return 0;
        seen[y][x][d][k]=1;
        if(COMMAND[k].first==0){
            k=COMMAND[k].second;
            continue;
        }
        else if(COMMAND[k].first==1){
            ll nx=x+dx[d];
            ll ny=y+dy[d];
            if(rin(ny,nx)&&S[ny][nx]!='#')k=COMMAND[k].second;
            else k=(k+1)%K;
            continue;
        }
        else if(COMMAND[k].first==2){
            ll nx=x+dx[d];
            ll ny=y+dy[d];
            if(rin(ny,nx)&&S[ny][nx]!='#'){
                x=nx;
                y=ny;
            }
            k=(k+1)%K;
            continue;
        }
        else if(COMMAND[k].first==3){
            d=(d+1)%4;
            k=(k+1)%K;
            continue;
        }
        else{
            d=(d+3)%4;
            k=(k+1)%K;
            continue;
        }
    }
    return 0;
}

void dfs(vector<pair<int,int>> COMMAND){
    int N=COMMAND.size();
    if(N==5)return;
    if(N!=0&&can_reach(COMMAND)){
        if(N<int(AN.size()))AN=COMMAND;
    }
    for(int i=0;i<5;i++){
        int ni=i,nj=-1;
        if(i<2){
            for(int n=0;n<3;n++){
                nj=n;
            }
        }
        COMMAND.push_back({ni,nj});
        dfs(COMMAND);
        COMMAND.pop_back();
    }
}


int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    
    
    cin>>N>>M;
    S.resize(N);
    rep(i,N){
        cin>>S[i];
        rep(j,M){
            if(S[i][j]=='S'){
                SY=i;
                SX=j;
            }
            if(S[i][j]=='G'){
                GY=i;
                GX=j;
            }
        }
    }
    vector<pair<int,int>> COMMAND={};
    dfs(COMMAND);
    
    int an=AN.size();
    cout<<an<<endl;
    rep(i,an){
        if(AN[i].first==0){
            cout<<"GOTO "<<AN[i].second+1<<endl;
        }
        else if(AN[i].first==1){
            cout<<"IF-OPEN "<<AN[i].second+1<<endl;
        }
        else if(AN[i].first==2){
            cout<<"FORWARD"<<endl;
        }
        else if(AN[i].first==3)cout<<"LEFT"<<endl;
        else cout<<"RIGHT"<<endl;
    }
    

    return 0;
}
accuracy: 0.06578947368421052
time_ms: 10
memory_kb: 3412
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
template<int mod>istream &operator>>(istream &is,static_modint<mod> &a){long long b;is>>b;a=b;return is;}
istream &operator>>(istream &is,modint &a){long long b;cin>>b;a=b;return is;}
#endif
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) static_cast<void>(0)
#define debugg(...) static_cast<void>(0)
#define esper(...) static_cast<void>(0)
template<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;}
#endif
using ll=long long;
using ull=unsigned long long;
using P=pair<ll,ll>;
template<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;
template<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}
template<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}
template<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;}
template<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;}
template<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;}
template<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;}
template<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;}
#define reps(i,a,n) for(int i=(a);i<(n);i++)
#define rep(i,n) reps(i,0,n)
#define all(x) x.begin(),x.end()
#define pcnt(x) __builtin_popcountll(x)
#define fin(x) return cout<<x<<'\n',static_cast<void>(0)
ll myceil(ll a,ll b){return (a+b-1)/b;}
template<typename T,size_t n,size_t id=0>
auto vec(const int (&d)[n],const T &init=T()){
  if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));
  else return init;
}
void SOLVE();
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout<<fixed<<setprecision(16);
  #ifdef LOCAL
  clock_t start=clock();
  #endif
  int testcase=1;
  //cin>>testcase;
  for(int i=0;i<testcase;i++){
    SOLVE();
  }
  #ifdef LOCAL
  cerr<<"time:";
  cerr<<(clock()-start)/1000;
  cerr<<"ms\n";
  #endif
}
string f[5]={"GOTO","IF-OPEN","FORWARD","LEFT","RIGHT"};
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
void SOLVE(){
  int n,m;
  cin>>n>>m;
  vector<string>s(n);
  cin>>s;
  int sx=-1,sy=-1,gx=-1,gy=-1;
  rep(i,n)rep(j,m){
    if(s[i][j]=='S')sx=i,sy=j;
    if(s[i][j]=='G')gx=i,gy=j;
  }
  int len=-1;
  vector<string>now;
  auto judge=[&]()->bool {
    auto seen=vec<bool>({n,m,4,len},false);
    seen[sx][sy][0][0]=true;
    queue<tuple<int,int,int,int>>que;
    que.push({sx,sy,0,0});
    while(!que.empty()){
      auto [x,y,dir,pos]=que.front();que.pop();
      if(now[pos]=="LEFT"){
        dir++;
        if(dir==4)dir=0;
        pos++;
      }
      else if(now[pos]=="RIGHT"){
        dir--;
        if(dir==-1)dir=3;
        pos++;
      }
      else if(now[pos]=="FORWARD"){
        int nx=x+dx[dir];
        int ny=y+dy[dir];
        if(nx>=0&&nx<n&&ny>=0&&ny<m&&s[nx][ny]!='#')x=nx,y=ny;
        pos++;
      }
      else{
        if(now[pos].substr(0,4)=="GOTO"){
          pos=now[pos][5]-'1';
        }
        else{
          int nx=x+dx[dir];
          int ny=y+dy[dir];
          if(nx>=0&&nx<n&&ny>=0&&ny<m&&s[nx][ny]!='#')pos=now[pos][8]-'1';
          else pos++;
        }
      }
      if(pos==len)pos=0;
      if(!seen[x][y][dir][pos]){
        seen[x][y][dir][pos]=true;
        que.push({x,y,dir,pos});
      }
    }
    rep(i,4)rep(j,len)if(seen[gx][gy][i][j])return true;
    return false;
  };
  auto dfs=[&](auto self)->void {
    if(now.size()==len){
      if(judge()){
        cout<<len<<endl;
        rep(i,len)cout<<now[i]<<endl;
        exit(0);
      }
      return;
    }
    rep(j,2)rep(i,len){
      now.push_back(f[j]+" "+char('1'+i));
      self(self);
      now.pop_back();
    }
    reps(i,2,5){
      now.push_back(f[i]);
      self(self);
      now.pop_back();
    }
  };
  reps(i,1,5){
    len=i;
    dfs(dfs);
  }
  cout<<"5\nLEFT\nIF-OPEN 5\nRIGHT\nGOTO 2\nFORWARD\n";
}
accuracy: 1
time_ms: 180
memory_kb: 3504
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
using ll=long long;
#define all(p) p.begin(),p.end()
int main(){
    int N,M;
    cin>>N>>M;
    int sx,sy,gx,gy;
    vector<string> S(N);
    rep(i,0,N) cin>>S[i];
    rep(i,0,N) rep(j,0,M){
        if(S[i][j]=='S') sx=i,sy=j;
        if(S[i][j]=='G') gx=i,gy=j;
    }
    vector<int> dx={1,0,-1,0},dy={0,1,0,-1};
    vector<string> lis={"GOTO","IF-OPEN","FORWARD","LEFT","RIGHT"};
    bool ok=0;
    auto ins=[&](int x,int y) -> bool {
        return (0<=x&&x<N&&0<=y&&y<M&&S[x][y]!='#');
    };
    auto ch=[&](vector<pair<string,int>> p) -> bool {
        int len=p.size();
        vector seen(N,vector(M,vector(len,vector<bool>(4))));
        seen[sx][sy][0][0]=1;
        vector<tuple<int,int,int,int>> order={{sx,sy,0,0}};
        rep(i,0,order.size()){
            int a,b,c,d;
            tie(a,b,c,d)=order[i];
            if(a==gx&&b==gy) return true;
            rep(j,0,5){
                int x=a,y=b,z=c,w=d;
                int s=dx[d]+a,t=dy[d]+b;
                if(p[c].first==lis[j]){
                    if(j==0){
                        z=p[c].second;
                    }
                    if(j==1){
                        if(ins(s,t)){
                            z=p[c].second;
                        }
                        else{
                            z=(z+1)%len;
                        }
                    }
                    if(j==2){
                        z=(z+1)%len;
                        if(ins(s,t)) x=s,y=t;
                    }
                    if(j==3){
                        z=(z+1)%len;
                        w=(w+1)%4;
                    }
                    if(j==4){
                        z=(z+1)%len;
                        w=(w+3)%4;
                    }
                    if(seen[x][y][z][w]==0){
                        seen[x][y][z][w]=1;
                        order.push_back({x,y,z,w});
                    }
                }
            }
        }
        return false;
    };
    vector<pair<string,int>> q;
    auto f=[&](auto self,int ind) -> void {
        if(ind==(int)q.size()){
            ok=ch(q);
            if(ok){
                cout<<ind<<"\n";
                for(auto x:q){
                    cout<<x.first;
                    if(x.second==-1) cout<<"\n";
                    else cout<<" "<<x.second+1<<"\n";
                }
            }
            return;
        }
        rep(i,0,5){
            if(i<2){
                rep(j,0,ind){
                    if(j==ind) continue;
                    q[ind]={lis[i],j};
                    self(self,ind+1);
                    if(ok) return;
                }
            }
            else{
                q[ind]={lis[i],-1};
                self(self,ind+1);
                if(ok) return;
            }
        }
    };
    rep(i,1,100){
        q.resize(i);
        f(f,0);
        if(ok) break;
    }
}
accuracy: 1
time_ms: 60
memory_kb: 3492
final_rank: 5

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

const ll GOTO=0,IF_OPEN=1,FORWARD=2,LEFT=3,RIGHT=4;

ll n,m;
vector<string>s;

ll sx=-1,sy=-1,gx=-1,gy=-1;
vector<ll>dx={1,0,-1,0},dy={0,1,0,-1};

using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;

vector<pair<ll,ll>>commands;

bool execute(ll p,ll len){
  vector<pair<ll,ll>>program;
  ll k=commands.size();
  for(ll j=0;j<len;++j){
    program.emplace_back(commands[p%k]);
    p/=k;
  }

  ll pc=0;
  ll now=0;
  ll x=sx,y=sy,dir=0;
  vvvvi v(n,vvvi(m,vvi(4,vi(len,0))));
  while(1){
    if(x==gx&&y==gy){
      return true;
    }
    if(v[x][y][dir][pc]==0){
      now+=1;
    }
    if(v[x][y][dir][pc]==now){
      return false;
    }
    v[x][y][dir][pc]=now;

    ll tp=program[pc].first;
    if(tp==GOTO){
      pc=program[pc].second;
    }
    if(tp==IF_OPEN){
      ll xx=x+dx[dir],yy=y+dy[dir];
      if(0<=xx&&xx<n&&0<=yy&&yy<m&&s[xx][yy]!='#'){
        pc=program[pc].second;
      }else{
        ++pc;
      }
    }
    if(tp==FORWARD){
      ll xx=x+dx[dir],yy=y+dy[dir];
      if(0<=xx&&xx<n&&0<=yy&&yy<m&&s[xx][yy]!='#'){
        x=xx;
        y=yy;
      }
      ++pc;
    }
    if(tp==LEFT){
      dir=(dir+1)%4;
      ++pc;
    }
    if(tp==RIGHT){
      dir=(dir+3)%4;
      ++pc;
    }
    if(pc==len){
      pc=0;
    }
  }
}

void output(ll p,ll len){
  vector<pair<ll,ll>>program;
  ll k=commands.size();
  for(ll j=0;j<len;++j){
    program.emplace_back(commands[p%k]);
    p/=k;
  }

  cout<<len<<endl;
  for(ll i=0;i<len;++i){
    ll tp=program[i].first;
    if(tp==GOTO){
      cout<<"GOTO"<<' '<<program[i].second+1<<endl;
    }
    if(tp==IF_OPEN){
      cout<<"IF-OPEN"<<' '<<program[i].second+1<<endl;
    }
    if(tp==FORWARD){
      cout<<"FORWARD"<<endl;
    }
    if(tp==LEFT){
      cout<<"LEFT"<<endl;
    }
    if(tp==RIGHT){
      cout<<"RIGHT"<<endl;
    }
  }
}

int main(){
  cin>>n>>m;
  s.assign(n,"");
  for(ll i=0;i<n;++i){
    cin>>s[i];
  }

  for(ll i=0;i<n;++i){
    for(ll j=0;j<m;++j){
      if(s[i][j]=='S'){
        sx=i;
        sy=j;
      }
      if(s[i][j]=='G'){
        gx=i;
        gy=j;
      }
    }
  }

  for(ll len=1;;++len){
    commands.clear();
    for(ll i=0;i<len;++i){
      commands.emplace_back(GOTO,i);
    }
    for(ll i=0;i<len;++i){
      commands.emplace_back(IF_OPEN,i);
    }
    commands.emplace_back(FORWARD,0);
    commands.emplace_back(LEFT,0);
    commands.emplace_back(RIGHT,0);

    ll k=commands.size();
    ll t=1;
    for(ll i=0;i<len;++i){
      t*=k;
    }
    for(ll i=0;i<t;++i){
      if(execute(i,len)){
        output(i,len);
        return 0;
      }
    }
  }
}
accuracy: 1
time_ms: 930
memory_kb: 3496
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define all(a) (a).begin(), (a).end()

int n, m;
vector<string> s;

int sx = 0, sy, gx, gy;

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

inline bool isok(int x, int y) {
    if(x < 0 || n <= x || y < 0 || m <= y) return false;
    return s[x][y] != '#';
}

bool check(const vector<int> &meirei) {
    int sz = meirei.size();
    if(sz == 0) return false;
    vector used(n, vector(m, vector(4, vector<bool>(sz, false))));
    // used[sx][sy][1][0] = true;
    int x = sx;
    int y = sy;
    int d = 1;
    int l = 0;
    while(!used[x][y][d][l]) {
        // cout << x << " " << y << " " << d << " " << l << endl;
        used[x][y][d][l] = true;
        if(meirei[l] < 3) {
            // GOTO
            l += 2 + meirei[l];
        } else if(meirei[l] < 6) {
            // IF
            if(isok(x + dx[d], y + dy[d])) {
                l += meirei[l] - 1;
            } else {
                l++;
            }
        } else if(meirei[l] == 6) {
            // FO
            if(isok(x + dx[d], y + dy[d])) {
                x += dx[d];
                y += dy[d];
            }
            l++;
        } else if(meirei[l] == 7) {
            // R
            d++;
            d %= 4;
            l++;
        } else if(meirei[l] == 8) {
            d += 3;
            d %= 4;
            l++;
        }
        l %= sz;
        if(x == gx && y == gy) return true;
    }
    return false;
}

int ans_sz = 6;
vector<int> ans;

void dfs1(vector<int> &meirei) {
    if(check(meirei) && meirei.size() < ans_sz) {
        ans_sz = meirei.size();
        ans = meirei;
    }
    if(meirei.size() == 5U) return;
    rep(i, 9) {
        meirei.push_back(i);
        dfs1(meirei);
        meirei.pop_back();
    }
}

int main() {
    cin >> n >> m;
    s.resize(n);
    rep(i, n) cin >> s[i];

    while(s[sx][sy] != 'S') sy++;
    gx = n - 1;
    while(s[gx][gy] != 'G') gy++;

    vector<int> meirei;
    dfs1(meirei);

    cout << ans_sz << endl;
    if(ans_sz <= 5) {
        rep(l, ans_sz) {
            if(ans[l] < 3) {
                cout << "GOTO " << (l + 2 + ans[l]) % ans_sz + 1 << endl;
            } else if(ans[l] < 6) {
                cout << "IF-OPEN " << (l + ans[l] - 1) % ans_sz + 1 << endl;
            } else if(ans[l] == 6) {
                cout << "FORWARD" << endl;
            } else if(ans[l] == 7) {
                cout << "RIGHT" << endl;
            } else {
                cout << "LEFT" << endl;
            }
        }
    } else {
        cout << "RIGHT" << endl;
        cout << "IF-OPEN 6" << endl;
        cout << "LEFT" << endl;
        cout << "IF-OPEN 6" << endl;
        cout << "GOTO 3" << endl;
        cout << "FORWARD" << endl;
    }
}
accuracy: 1
time_ms: 810
memory_kb: 3472
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define inf 1000000007
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define rrep(i,n) for(int i = (int)n-1; i >= 0; --i)
#define srep(i,a,b) for(int i = (int)a; i < (int)(b); ++i)
#define all(x) (x).begin(),(x).end()
#define SUM(v) accumulate(all(v), 0LL)
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
#define SZ(c) (int)(c).size()
template<typename T>
ostream& operator << (ostream& os, vector<T>& vec) {
    os << "{";
    for (int i = 0; i<(int)vec.size(); i++) {
        os << vec[i] << (i + 1 == (int)vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
// pair出力
template<typename T, typename U>
ostream& operator << (ostream& os, pair<T, U> pair_var) {
    os << "(" << pair_var.first << ", " << pair_var.second << ")";
    return os;
}
// map出力
template<typename T, typename U>
ostream& operator << (ostream& os, 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;
}
// set 出力
template<typename T>
ostream& operator << (ostream& os, 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;
}
// tuple 出力
template<int N,class Tuple>
void out(ostream &os,const Tuple &t){}
template<int N,class Tuple,class H,class ...Ts>
void out(ostream &os,const Tuple &t){
    if(N)os<<", ";
    os<<get<N>(t);
    out<N+1,Tuple,Ts...>(os,t);
}
template<class ...Ts>
ostream& operator<<(ostream &os, const tuple<Ts...> &t){
    os<<"(";
    out<0,tuple<Ts...>,Ts...>(os,t);
    os<<")";
    return os;
}
#define overload2(_1, _2, name, ...) name
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size)                                                                                                                                  \
    vector<type> name(size);                                                                                                                                   \
    IN(name)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w)                                                                                                                                   \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN(name)
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)                                                                                                                         \
    vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
#define INT(...)                                                                                                                                               \
    int __VA_ARGS__;                                                                                                                                           \
    IN(__VA_ARGS__)
#define LL(...)                                                                                                                                                \
    ll __VA_ARGS__;                                                                                                                                            \
    IN(__VA_ARGS__)
#define STR(...)                                                                                                                                               \
    string __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS__)
#define CHR(...)                                                                                                                                               \
    char __VA_ARGS__;                                                                                                                                          \
    IN(__VA_ARGS__)
#define DBL(...)                                                                                                                                               \
    double __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS__)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) {
    for(auto &i : a) scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
    scan(head);
    IN(tail...);
}
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { cout << yesno[t] << endl; }
void no(bool t = 1) { yes(!t); }
#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define dbg(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define dbg(...) (void(0))
#define dump(x) (void(0))
#endif
template<typename A, typename T>
std::enable_if_t<std::is_convertible<T, A>::value> fill(A& array, const T& val)
{
    array = val;
}
template<typename A, typename T>
std::enable_if_t<!std::is_convertible<T, A>::value> fill(A& array, const T& val)
{
    for (auto& a : array) {
        fill(a, val);
    }
}
template <typename T, typename S> T ceil(T x, S y) {
    assert(y);
    return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));
}
template <typename T, typename S> T floor(T x, S y) {
    assert(y);
    return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));
}
vector<int> iota(int n) {vector<int> a(n);iota(all(a), 0);return a;}
template <class T> T POW(T x, int n) {T res = 1;for(; n; n >>= 1, x *= x){if(n & 1) res *= x;}return res;}
ll pow2(int i) { return 1LL << i; }
int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }
// int allbit(int n) { return (1 << n) - 1; }
ll allbit(ll n) { return (1LL << n) - 1; }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(ll t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }


template <class S> void fold_in(vector<S> &v) {}
template <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) {
    for(auto e : a) v.emplace_back(e);
    fold_in(v, tail...);
}
template <class S> void renumber(vector<S> &v) {}
template <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) {
    for(auto &&e : a) e = lb(v, e);
    renumber(v, tail...);
}
template <class S, class... Args> void zip(vector<S> &head, Args &&...args) {
    vector<S> v;
    fold_in(v, head, args...);
    sort(all(v)), v.erase(unique(all(v)), v.end());
    renumber(v, head, args...);
}
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}
const int LIMIT = 10;

int dx[2][4] = {{1,0,-1,0},{0,1,0,-1}};
bool flag[11][11][LIMIT][4];
int main(){
    INT(n,m);
    VEC(string,s,n);
    int t[2][2];
    rep(i,n){
        rep(j,m){
            if(s[i][j]=='S'){
                t[0][0] = i;
                t[0][1] = j;
                s[i][j] = '.';
            }else if(s[i][j]=='G'){
                t[1][0] = i;
                t[1][1] = j;
                s[i][j] = '.';
            }
        }
    }
    for(int len=1;len<=LIMIT;len++){
        vector<pair<int,int> > p;
        auto is_field= [&](int a,int b)->bool{
            if(a<0||a>=n||b<0||b>=m)return false;
            if(s[a][b]=='#')return false;
            return true;
        };
        auto check = [&]()->bool{
            int x[2];
            x[0] = t[0][0];
            x[1] = t[0][1];
            fill(flag,false);
            int d = 0;
            int pc = 0;
            while(1){
                if(flag[x[0]][x[1]][pc][d])break;
                bool ok = 1;
                rep(z,2){
                    if(x[z]!=t[1][z])ok = 0;
                }
                if(ok)return true;
                flag[x[0]][x[1]][pc][d] = true;
                auto [cid,l] = p[pc];
                if(cid==0){
                    pc = l;
                }else if(cid==1){
                    if(is_field(x[0]+dx[0][d],x[1]+dx[1][d])){
                        pc = l;
                    }else{
                        pc++;
                    }
                }else if(cid==2){
                    if(is_field(x[0]+dx[0][d],x[1]+dx[1][d])){
                        x[0] += dx[0][d];
                        x[1] += dx[1][d];
                    }
                    pc++;
                }else if(cid==3){
                    d++;
                    d%=4;
                    pc++;
                }else{
                    d += 3;
                    d %= 4;
                    pc++;
                }
                pc %= len;
            }
            return false;
        };
        auto dfs = [&](auto&&f,int id)->bool{
            if(id==len){
                if(check()){
                    return true;
                }else{
                    return false;
                }
            }
            rep(i,5){
                if(i<=2){
                    for(int j=0;j<len;j++){
                        p.push_back(MP(i,j));
                        if(f(f,id+1))return true;
                        p.pop_back();
                    }
                }else{
                    p.push_back(MP(i,0));
                    if(f(f,id+1))return true;
                    p.pop_back();
                }
            }
            return false;
        };
        if(dfs(dfs,0)){
            cout << len << "\n";
            for(auto [tt,l]:p){
                if(tt==0){
                    cout << "GOTO " << l+1 << "\n";
                }else if(tt==1){
                    cout << "IF-OPEN " << l+1 << "\n";
                }else if(tt==2){
                    cout << "FORWARD\n";
                }else if(tt==3){
                    cout << "LEFT\n";
                }else if(tt==4){
                    cout << "RIGHT\n";
                }
            }
            break;
        }
    }    
    return 0;
}
accuracy: 1
time_ms: 380
memory_kb: 3508
final_rank: 11

code_snippet: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
int N,M;
string S[10];
int sx,sy,gx,gy;
pair<int,int>P[5];
bool vis[10][10][4][5];
int dx[4]={1,0,-1,0};
int dy[4]={0,-1,0,1};
queue<pair<pair<int,int>,pair<int,int> > >Q;
bool check(int len)
{
	for(int i=0;i<N;i++)for(int j=0;j<M;j++)for(int r=0;r<4;r++)for(int k=0;k<len;k++)
	{
		vis[i][j][r][k]=false;
	}
	while(!Q.empty())Q.pop();
	Q.push(make_pair(make_pair(sx,sy),make_pair(0,0)));
	vis[sx][sy][0][0]=true;
	while(!Q.empty())
	{
		int x=Q.front().first.first,y=Q.front().first.second;
		int dir=Q.front().second.first,pc=Q.front().second.second;
		Q.pop();
		if(x==gx&&y==gy)return true;
		int op=P[pc].first;
		int tx,ty;
		switch(op)
		{
			case 0:
				pc=P[pc].second;
				break;
			case 1:
				tx=x+dx[dir],ty=y+dy[dir];
				if(0<=tx&&tx<N&&0<=ty&&ty<M&&S[tx][ty]!='#')pc=P[pc].second;
				else pc=(pc+1)%len;
				break;
			case 2:
				tx=x+dx[dir],ty=y+dy[dir];
				if(0<=tx&&tx<N&&0<=ty&&ty<M&&S[tx][ty]!='#')x=tx,y=ty;
				pc=(pc+1)%len;
				break;
			case 3:
				dir=(dir+3)%4;
				pc=(pc+1)%len;
				break;
			case 4:
				dir=(dir+1)%4;
				pc=(pc+1)%len;
				break;
		}
		if(!vis[x][y][dir][pc])
		{
			vis[x][y][dir][pc]=true;
			Q.push(make_pair(make_pair(x,y),make_pair(dir,pc)));
		}
	}
	return false;
}
bool dfs(int id,int len)
{
	if(id==len)
	{
		if(check(len))
		{
			cout<<len<<endl;
			for(int i=0;i<len;i++)
			{
				int op=P[i].first;
				switch(op)
				{
					case 0:
						cout<<"GOTO "<<P[i].second+1<<endl;
						break;
					case 1:
						cout<<"IF-OPEN "<<P[i].second+1<<endl;
						break;
					case 2:
						cout<<"FORWARD"<<endl;
						break;
					case 3:
						cout<<"LEFT"<<endl;
						break;
					case 4:
						cout<<"RIGHT"<<endl;
						break;
				}
			}
			return true;
		}
	}
	else
	{
		for(int op=0;op<5;op++)
		{
			P[id].first=op;
			if(op<=1)
			{
				for(int i=0;i<len;i++)
				{
					P[id].second=i;
					if(dfs(id+1,len))return true;
				}
			}
			else if(dfs(id+1,len))return true;
		}
	}
	return false;
}
int main()
{
	cin>>N>>M;
	for(int i=0;i<N;i++)
	{
		cin>>S[i];
		for(int j=0;j<M;j++)
		{
			if(S[i][j]=='S')sx=i,sy=j;
			if(S[i][j]=='G')gx=i,gy=j;
		}
	}
	for(int len=1;len<=5;len++)
	{
		if(dfs(0,len))return 0;
	}
}
accuracy: 1
time_ms: 120
memory_kb: 3440
final_rank: 3

code_snippet: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
int N,M;
string S[10];
int sx,sy,gx,gy;
pair<int,int>P[5];
bool vis[10][10][4][5];
int dx[4]={1,0,-1,0};
int dy[4]={0,-1,0,1};
queue<pair<pair<int,int>,pair<int,int> > >Q;
bool check(int len)
{
	for(int i=0;i<N;i++)for(int j=0;j<M;j++)for(int r=0;r<4;r++)for(int k=0;k<len;k++)
	{
		vis[i][j][r][k]=false;
	}
	while(!Q.empty())Q.pop();
	Q.push(make_pair(make_pair(sx,sy),make_pair(0,0)));
	vis[sx][sy][0][0]=true;
	while(!Q.empty())
	{
		int x=Q.front().first.first,y=Q.front().first.second;
		int dir=Q.front().second.first,pc=Q.front().second.second;
		Q.pop();
		if(x==gx&&y==gy)return true;
		int op=P[pc].first;
		int tx,ty;
		switch(op)
		{
			case 0:
				pc=P[pc].second;
				break;
			case 1:
				tx=x+dx[dir],ty=y+dy[dir];
				if(0<=tx&&tx<N&&0<=ty&&ty<M&&S[tx][ty]!='#')pc=P[pc].second;
				else pc=(pc+1)%len;
				break;
			case 2:
				tx=x+dx[dir],ty=y+dy[dir];
				if(0<=tx&&tx<N&&0<=ty&&ty<M&&S[tx][ty]!='#')x=tx,y=ty;
				pc=(pc+1)%len;
				break;
			case 3:
				dir=(dir+3)%4;
				pc=(pc+1)%len;
				break;
			case 4:
				dir=(dir+1)%4;
				pc=(pc+1)%len;
				break;
		}
		if(!vis[x][y][dir][pc])
		{
			vis[x][y][dir][pc]=true;
			Q.push(make_pair(make_pair(x,y),make_pair(dir,pc)));
		}
	}
	return false;
}
bool dfs(int id,int len)
{
	if(id==len)
	{
		if(check(len))
		{
			cout<<len<<endl;
			for(int i=0;i<len;i++)
			{
				int op=P[i].first;
				switch(op)
				{
					case 0:
						cout<<"GOTO "<<P[i].second+1<<endl;
						break;
					case 1:
						cout<<"IF-OPEN "<<P[i].second+1<<endl;
						break;
					case 2:
						cout<<"FORWARD"<<endl;
						break;
					case 3:
						cout<<"LEFT"<<endl;
						break;
					case 4:
						cout<<"RIGHT"<<endl;
						break;
				}
			}
			return true;
		}
	}
	else
	{
		for(int op=0;op<5;op++)
		{
			P[id].first=op;
			if(op<=1)
			{
				for(int i=0;i<len;i++)
				{
					P[id].second=i;
					if(dfs(id+1,len))return true;
				}
			}
			else if(dfs(id+1,len))return true;
		}
	}
	return false;
}
int main()
{
	cin>>N>>M;
	for(int i=0;i<N;i++)
	{
		cin>>S[i];
		for(int j=0;j<M;j++)
		{
			if(S[i][j]=='S')sx=i,sy=j;
			if(S[i][j]=='G')gx=i,gy=j;
		}
	}
	for(int len=1;len<=5;len++)
	{
		if(dfs(0,len)) return 0;
	}
}
accuracy: 1
time_ms: 120
memory_kb: 3440
final_rank: 3

code_snippet: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
int N,M;
string S[10];
int sx,sy,gx,gy;
pair<int,int>P[5];
bool vis[10][10][4][5];
int dx[4]={1,0,-1,0};
int dy[4]={0,-1,0,1};
queue<pair<pair<int,int>,pair<int,int> > >Q;
bool check(int len)
{
	for(int i=0;i<N;i++)for(int j=0;j<M;j++)for(int r=0;r<4;r++)for(int k=0;k<len;k++)
	{
		vis[i][j][r][k]=false;
	}
	while(!Q.empty())Q.pop();
	Q.push(make_pair(make_pair(sx,sy),make_pair(0,0)));
	vis[sx][sy][0][0]=true;
	while(!Q.empty())
	{
		int x=Q.front().first.first,y=Q.front().first.second;
		int dir=Q.front().second.first,pc=Q.front().second.second;
		Q.pop();
		if(x==gx&&y==gy)return true;
		int op=P[pc].first;
		int tx,ty;
		switch(op)
		{
			case 0:
				pc=P[pc].second;
				break;
			case 1:
				tx=x+dx[dir],ty=y+dy[dir];
				if(0<=tx&&tx<N&&0<=ty&&ty<M&&S[tx][ty]!='#')pc=P[pc].second;
				else pc=(pc+1)%len;
				break;
			case 2:
				tx=x+dx[dir],ty=y+dy[dir];
				if(0<=tx&&tx<N&&0<=ty&&ty<M&&S[tx][ty]!='#')x=tx,y=ty;
				pc=(pc+1)%len;
				break;
			case 3:
				dir=(dir+3)%4;
				pc=(pc+1)%len;
				break;
			case 4:
				dir=(dir+1)%4;
				pc=(pc+1)%len;
				break;
		}
		if(!vis[x][y][dir][pc])
		{
			vis[x][y][dir][pc]=true;
			Q.push(make_pair(make_pair(x,y),make_pair(dir,pc)));
		}
	}
	return false;
}
bool dfs(int id,int len)
{
	if(id==len)
	{
		if(check(len))
		{
			cout<<len<<endl;
			for(int i=0;i<len;i++)
			{
				int op=P[i].first;
				switch(op)
				{
					case 0:
						cout<<"GOTO "<<P[i].second+1<<endl;
						break;
					case 1:
						cout<<"IF-OPEN "<<P[i].second+1<<endl;
						break;
					case 2:
						cout<<"FORWARD"<<endl;
						break;
					case 3:
						cout<<"LEFT"<<endl;
						break;
					case 4:
						cout<<"RIGHT"<<endl;
						break;
				}
			}
			return true;
		}
	}
	else
	{
		for(int op=0;op<5;op++)
		{
			P[id].first=op;
			if(op<=1)
			{
				for(int i=0;i<len;i++)
				{
					P[id].second=i;
					if(dfs(id+1,len))return true;
				}
			}
			else if(dfs(id+1,len))return true;
		}
	}
	return false;
}
int main()
{
	cin>>N>>M;
	for(int i=0;i<N;i++)
	{
		cin>>S[i];
		for(int j=0;j<M;j++)
		{
			if(S[i][j]=='S')sx=i,sy=j;
			if(S[i][j]=='G')gx=i,gy=j;
		}
	}
	for(int len=1;len<=5;len++)
	{
		if(dfs(0,len))return 0;
	}
}
accuracy: 1
time_ms: 120
memory_kb: 3436
final_rank: 2

code_snippet: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
int N,M;
string S[10];
int sx,sy,gx,gy;
vector<pair<int,int> >P;
bool vis[10][10][4][7];
int dx[4]={1,0,-1,0};
int dy[4]={0,-1,0,1};
bool check()
{
	for(int i=0;i<N;i++)for(int j=0;j<M;j++)for(int r=0;r<4;r++)for(int k=0;k<P.size();k++)
	{
		vis[i][j][r][k]=false;
	}
	queue<pair<pair<int,int>,pair<int,int> > >Q;
	Q.push(make_pair(make_pair(sx,sy),make_pair(0,0)));
	vis[sx][sy][0][0]=true;
	while(!Q.empty())
	{
		int x=Q.front().first.first,y=Q.front().first.second;
		int dir=Q.front().second.first,pc=Q.front().second.second;
		Q.pop();
		if(x==gx&&y==gy)return true;
		int op=P[pc].first;
		int tx,ty;
		switch(op)
		{
			case 0:
				pc=P[pc].second;
				break;
			case 1:
				tx=x+dx[dir],ty=y+dy[dir];
				if(0<=tx&&tx<N&&0<=ty&&ty<M&&S[tx][ty]!='#')pc=P[pc].second;
				else pc=(pc+1)%P.size();
				break;
			case 2:
				tx=x+dx[dir],ty=y+dy[dir];
				if(0<=tx&&tx<N&&0<=ty&&ty<M&&S[tx][ty]!='#')x=tx,y=ty;
				pc=(pc+1)%P.size();
				break;
			case 3:
				dir=(dir+3)%4;
				pc=(pc+1)%P.size();
				break;
			case 4:
				dir=(dir+1)%4;
				pc=(pc+1)%P.size();
				break;
		}
		if(!vis[x][y][dir][pc])
		{
			vis[x][y][dir][pc]=true;
			Q.push(make_pair(make_pair(x,y),make_pair(dir,pc)));
		}
	}
	return false;
}
bool dfs(int id,int len)
{
	if(id==len)
	{
		if(check())
		{
			cout<<len<<endl;
			for(int i=0;i<len;i++)
			{
				int op=P[i].first;
				switch(op)
				{
					case 0:
						cout<<"GOTO "<<P[i].second+1<<endl;
						break;
					case 1:
						cout<<"IF-OPEN "<<P[i].second+1<<endl;
						break;
					case 2:
						cout<<"FORWARD"<<endl;
						break;
					case 3:
						cout<<"LEFT"<<endl;
						break;
					case 4:
						cout<<"RIGHT"<<endl;
						break;
				}
			}
			return true;
		}
	}
	else
	{
		for(int op=0;op<5;op++)
		{
			P[id].first=op;
			if(op<=1)
			{
				for(int i=0;i<len;i++)if(i!=id)
				{
					P[id].second=i;
					if(dfs(id+1,len))return true;
				}
			}
			else if(dfs(id+1,len))return true;
		}
	}
	return false;
}
int main()
{
	cin>>N>>M;
	for(int i=0;i<N;i++)
	{
		cin>>S[i];
		for(int j=0;j<M;j++)
		{
			if(S[i][j]=='S')sx=i,sy=j;
			if(S[i][j]=='G')gx=i,gy=j;
		}
	}
	for(int len=1;;len++)
	{
		P.resize(len);
		if(dfs(0,len))return 0;
	}
}
accuracy: 0.6842105263157895
time_ms: 20
memory_kb: 3212
final_rank: 16

code_snippet: #include<iostream>
#include<vector>
#include<queue>
#include<cstdlib>
using namespace std;
int N,M;
string S[10];
int sx,sy,gx,gy;
vector<pair<int,int> >P;
bool vis[10][10][4][7];
int dx[4]={1,0,-1,0};
int dy[4]={0,-1,0,1};
bool check()
{
	for(int i=0;i<N;i++)for(int j=0;j<M;j++)for(int r=0;r<4;r++)for(int k=0;k<P.size();k++)
	{
		vis[i][j][r][k]=false;
	}
	queue<pair<pair<int,int>,pair<int,int> > >Q;
	Q.push(make_pair(make_pair(sx,sy),make_pair(0,0)));
	vis[sx][sy][0][0]=true;
	while(!Q.empty())
	{
		int x=Q.front().first.first,y=Q.front().first.second;
		int dir=Q.front().second.first,pc=Q.front().second.second;
		Q.pop();
		if(x==gx&&y==gy)return true;
		int op=P[pc].first;
		int tx,ty;
		switch(op)
		{
			case 0:
				pc=P[pc].second;
				break;
			case 1:
				tx=x+dx[dir],ty=y+dy[dir];
				if(0<=tx&&tx<N&&0<=ty&&ty<M&&S[tx][ty]!='#')pc=P[pc].second;
				else pc=(pc+1)%P.size();
				break;
			case 2:
				tx=x+dx[dir],ty=y+dy[dir];
				if(0<=tx&&tx<N&&0<=ty&&ty<M&&S[tx][ty]!='#')x=tx,y=ty;
				pc=(pc+1)%P.size();
				break;
			case 3:
				dir=(dir+3)%4;
				pc=(pc+1)%P.size();
				break;
			case 4:
				dir=(dir+1)%4;
				pc=(pc+1)%P.size();
				break;
		}
		if(!vis[x][y][dir][pc])
		{
			vis[x][y][dir][pc]=true;
			Q.push(make_pair(make_pair(x,y),make_pair(dir,pc)));
		}
	}
	return false;
}
void dfs(int id,int len)
{
	if(id==len)
	{
		if(check())
		{
			cout<<len<<endl;
			for(int i=0;i<len;i++)
			{
				int op=P[i].first;
				switch(op)
				{
					case 0:
						cout<<"GOTO "<<P[i].second+1<<endl;
						break;
					case 1:
						cout<<"IF-OPEN "<<P[i].second+1<<endl;
						break;
					case 2:
						cout<<"FORWARD"<<endl;
						break;
					case 3:
						cout<<"LEFT"<<endl;
						break;
					case 4:
						cout<<"RIGHT"<<endl;
						break;
				}
			}
			exit(0);
		}
	}
	else
	{
		for(int op=0;op<5;op++)
		{
			P[id].first=op;
			if(op<=1)
			{
				for(int i=0;i<len;i++)if(i!=id)
				{
					P[id].second=i;
					dfs(id+1,len);
				}
			}
			else dfs(id+1,len);
		}
	}
}
int main()
{
	cin>>N>>M;
	for(int i=0;i<N;i++)
	{
		cin>>S[i];
		for(int j=0;j<M;j++)
		{
			if(S[i][j]=='S')sx=i,sy=j;
			if(S[i][j]=='G')gx=i,gy=j;
		}
	}
	for(int len=1;;len++)
	{
		P.resize(len);
		dfs(0,len);
	}
}
accuracy: 1
time_ms: 50
memory_kb: 3472
final_rank: 1

code_snippet: #if 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>
#include <list>
#include <cmath>

auto& in = std::cin;
auto& out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI = 3.141592653589793238462643383279502884197169399375105820974944;


template<typename T, typename U>
std::enable_if_t<std::rank<T>::value == 0> fill_all(T& arr, const U& v) {
    arr = v;
}
template<typename ARR, typename U>
std::enable_if_t<std::rank<ARR>::value != 0> fill_all(ARR& arr, const U& v) {
    for (auto& i : arr) {
        fill_all(i, v);
    }
}



int H, W;
int sH, sW;
char map[20][20];
enum { HP, HM, WP, WM };

std::tuple<int, int> move_forward(int h, int w, int dir) {
    switch (dir) {
    case HP: ++h; break;
    case HM: --h; break;
    case WP: ++w; break;
    case WM: --w; break;
    default: assert(false); break;
    }
    return { h,w };
}

class Program {
public:
    virtual std::tuple<int, int, int, int> Do(int h, int w, int dir, int pc)const = 0;
    virtual void Print()const = 0;
};
class Goto : public Program {
private:
    int target;
public:
    Goto() {}
    static const Goto* get(int i) {
        static std::array<Goto, 10000> arr;
        static bool inited = false;
        if (!inited) {
            for (size_t i = 0; i < arr.size(); i++) {
                arr[i].target = i;
            }
            inited = true;
        }
        return &arr[i];
    }
    std::tuple<int, int, int, int> Do(int h, int w, int dir, int pc)const override {
        return { h, w, dir, target };
    }
    void Print()const override {
        out << "GOTO " << target+1 << '\n';
    }
};
class IF_OPEN : public Program {
private:
    int target;
public:
    IF_OPEN() { }
    static const IF_OPEN* get(int i) {
        static std::array<IF_OPEN, 10000> arr;
        static bool inited = false;
        if (!inited) {
            for (size_t i = 0; i < arr.size(); i++) {
                arr[i].target = i;
            }
            inited = true;
        }
        return &arr[i];
    }
    std::tuple<int, int, int, int> Do(int h, int w, int dir, int pc)const override {
        auto [nh, nw] = move_forward(h, w, dir);
        if (map[nh][nw] != '#') {
            return { h, w, dir, target };
        }
        return { h, w, dir, pc+1 };
    }
    void Print()const override {
        out << "IF-OPEN " << target+1 << '\n';
    }
};
class FORWARD : public Program {
public:
    std::tuple<int, int, int, int> Do(int h, int w, int dir, int pc)const override {
        auto [nh, nw] = move_forward(h, w, dir);
        if (map[nh][nw] != '#') {
            return { nh, nw, dir, pc + 1 };
        }
        return { h, w, dir, pc + 1 };
    }
    void Print()const override {
        out << "FORWARD\n";
    }
}Program_FORWARD;
class Left : public Program {
public:
    std::tuple<int, int, int, int> Do(int h, int w, int dir, int pc)const override {
        switch (dir) {
        case HP: dir = WP; break;
        case HM: dir = WM; break;
        case WP: dir = HM; break;
        case WM: dir = HP; break;
        default: assert(false); break;
        }
        return { h, w, dir, pc + 1 };
    }
    void Print()const override {
        out << "LEFT\n";
    }
}Program_Left;
class Right : public Program {
public:
    std::tuple<int, int, int, int> Do(int h, int w, int dir, int pc)const override {
        switch (dir) {
        case HP: dir = WM; break;
        case HM: dir = WP; break;
        case WP: dir = HP; break;
        case WM: dir = HM; break;
        default: assert(false); break;
        }
        return { h, w, dir, pc + 1 };
    }
    void Print()const override {
        out << "RIGHT\n";
    }
}Program_Right;


bool used[20][20][4][5];
bool check(const std::vector<const Program*>& program) {
    int h = sH, w = sW, dir = HP, pc = 0;
    fill_all(used, false);

    for (;;) {
        if (map[h][w] == 'G') {
            return true;
        }
        if (pc == program.size()) { pc = 0; }
        auto& memo = used[h][w][dir][pc];
        if (memo) { return false; }
        memo = true;
        std::tie(h, w, dir, pc) = program[pc]->Do(h, w, dir, pc);
    }
}

bool func(std::vector<const Program*>& program, int num) {
    if (program.size() == num) {
        return check(program);
    }

    for (int i = 0; i < num; i++) {
        program.push_back(Goto::get(i));
        if (func(program, num)) {
            return true;
        }
        program.pop_back();
    }
    for (int i = 0; i < num; i++) {
        program.push_back(IF_OPEN::get(i));
        if (func(program, num)) {
            return true;
        }
        program.pop_back();
    }

    const Program* const Programs[] = {
        &Program_FORWARD , &Program_Left , &Program_Right
    };
    for (auto p: Programs) {
        program.push_back(p);
        if (func(program, num)) {
            return true;
        }
        program.pop_back();
    }

    return false;
}

int main()
{
    using std::endl;
    in.sync_with_stdio(false);
    out.sync_with_stdio(false);
    in.tie(nullptr);
    out.tie(nullptr);

    fill_all(map, '#');
    in >> H >> W;
    for (int i = 1; i <= H; i++)
    {
        in >> map[i];
        map[i][W + 1] = '#';
        for (int j = W; j >= 1; --j) {
            map[i][j] = map[i][j - 1];
        }
        map[i][0] = '#';
    }
    for (int i = 1; i <= H; i++)
    {
        for (int j = W; j >= 1; --j) {
            if (map[i][j] == 'S') {
                sH = i;
                sW = j;
            }
        }
    }


    std::vector<const Program*> program;
    for (int i = 0; i < 4; i++) {
        if (func(program, i + 1)) {
            break;
        }
    }
    //右壁の法則
    if (program.empty()) {
        program.push_back(&Program_Right);
        program.push_back(IF_OPEN::get(4));
        program.push_back(&Program_Left);
        program.push_back(Goto::get(1));
        program.push_back(&Program_FORWARD);
    }
    assert(check(program));

    out << program.size() << '\n';
    for (auto& p : program) {
        p->Print();
    }


    return 0;
}
#endif
accuracy: 1
time_ms: 60
memory_kb: 3776
final_rank: 12

code_snippet: #if 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>
#include <list>
#include <cmath>

auto& in = std::cin;
auto& out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI = 3.141592653589793238462643383279502884197169399375105820974944;


template<typename T, typename U>
std::enable_if_t<std::rank<T>::value == 0> fill_all(T& arr, const U& v) {
    arr = v;
}
template<typename ARR, typename U>
std::enable_if_t<std::rank<ARR>::value != 0> fill_all(ARR& arr, const U& v) {
    for (auto& i : arr) {
        fill_all(i, v);
    }
}



int H, W;
int sH, sW;
char map[20][20];
enum { HP, HM, WP, WM };

std::tuple<int, int> move_forward(int h, int w, int dir) {
    switch (dir) {
    case HP: ++h; break;
    case HM: --h; break;
    case WP: ++w; break;
    case WM: --w; break;
    default: assert(false); break;
    }
    return { h,w };
}

class Program {
public:
    virtual std::tuple<int, int, int, int> Do(int h, int w, int dir, int pc)const = 0;
    virtual void Print()const = 0;
};
class Goto : public Program {
private:
    int target;
public:
    Goto() {}
    static const Goto* get(int i) {
        static std::array<Goto, 10000> arr;
        static bool inited = false;
        if (!inited) {
            for (size_t i = 0; i < arr.size(); i++) {
                arr[i].target = i;
            }
            inited = true;
        }
        return &arr[i];
    }
    std::tuple<int, int, int, int> Do(int h, int w, int dir, int pc)const override {
        return { h, w, dir, target };
    }
    void Print()const override {
        out << "GOTO " << target+1 << '\n';
    }
};
class IF_OPEN : public Program {
private:
    int target;
public:
    IF_OPEN() { }
    static const IF_OPEN* get(int i) {
        static std::array<IF_OPEN, 10000> arr;
        static bool inited = false;
        if (!inited) {
            for (size_t i = 0; i < arr.size(); i++) {
                arr[i].target = i;
            }
            inited = true;
        }
        return &arr[i];
    }
    std::tuple<int, int, int, int> Do(int h, int w, int dir, int pc)const override {
        auto [nh, nw] = move_forward(h, w, dir);
        if (map[nh][nw] != '#') {
            return { h, w, dir, target };
        }
        return { h, w, dir, pc+1 };
    }
    void Print()const override {
        out << "IF-OPEN " << target+1 << '\n';
    }
};
class FORWARD : public Program {
public:
    std::tuple<int, int, int, int> Do(int h, int w, int dir, int pc)const override {
        auto [nh, nw] = move_forward(h, w, dir);
        if (map[nh][nw] != '#') {
            return { nh, nw, dir, pc + 1 };
        }
        return { h, w, dir, pc + 1 };
    }
    void Print()const override {
        out << "FORWARD\n";
    }
}Program_FORWARD;
class Left : public Program {
public:
    std::tuple<int, int, int, int> Do(int h, int w, int dir, int pc)const override {
        switch (dir) {
        case HP: dir = WP; break;
        case HM: dir = WM; break;
        case WP: dir = HM; break;
        case WM: dir = HP; break;
        default: assert(false); break;
        }
        return { h, w, dir, pc + 1 };
    }
    void Print()const override {
        out << "LEFT\n";
    }
}Program_Left;
class Right : public Program {
public:
    std::tuple<int, int, int, int> Do(int h, int w, int dir, int pc)const override {
        switch (dir) {
        case HP: dir = WM; break;
        case HM: dir = WP; break;
        case WP: dir = HP; break;
        case WM: dir = HM; break;
        default: assert(false); break;
        }
        return { h, w, dir, pc + 1 };
    }
    void Print()const override {
        out << "RIGHT\n";
    }
}Program_Right;


bool used[20][20][4][5];
bool check(const std::vector<const Program*>& program) {
    int h = sH, w = sW, dir = HP, pc = 0;
    fill_all(used, false);

    for (;;) {
        if (map[h][w] == 'G') {
            return true;
        }
        if (pc == program.size()) { pc = 0; }
        auto& memo = used[h][w][dir][pc];
        if (memo) { return false; }
        memo = true;
        std::tie(h, w, dir, pc) = program[pc]->Do(h, w, dir, pc);
    }
}

bool func(std::vector<const Program*>& program, int num) {
    if (program.size() == num) {
        return check(program);
    }

    for (int i = 0; i < num; i++) {
        program.push_back(Goto::get(i));
        if (func(program, num)) {
            return true;
        }
        program.pop_back();
    }
    for (int i = 0; i < num; i++) {
        program.push_back(IF_OPEN::get(i));
        if (func(program, num)) {
            return true;
        }
        program.pop_back();
    }

    const Program* const Programs[] = {
        &Program_FORWARD , &Program_Left , &Program_Right
    };
    for (auto p: Programs) {
        program.push_back(p);
        if (func(program, num)) {
            return true;
        }
        program.pop_back();
    }

    return false;
}

int main()
{
    using std::endl;
    in.sync_with_stdio(false);
    out.sync_with_stdio(false);
    in.tie(nullptr);
    out.tie(nullptr);

    fill_all(map, '#');
    in >> H >> W;
    for (int i = 1; i <= H; i++)
    {
        in >> map[i];
        map[i][W + 1] = '#';
        for (int j = W; j >= 1; --j) {
            map[i][j] = map[i][j - 1];
        }
        map[i][0] = '#';
    }
    for (int i = 1; i <= H; i++)
    {
        for (int j = W; j >= 1; --j) {
            if (map[i][j] == 'S') {
                sH = i;
                sW = j;
            }
        }
    }


    std::vector<const Program*> program;
    for (int i = 0; i < 10; i++) {
        if (func(program, i + 1)) {
            break;
        }
    }
    //右壁の法則
    //if (program.empty()) {
    //    program.push_back(&Program_Right);
    //    program.push_back(&Program_IF_OPEN_4);
    //    program.push_back(&Program_Left);
    //    program.push_back(&Program_GOTO_1);
    //    program.push_back(&Program_FORWARD);
    //}
    assert(check(program));

    out << program.size() << '\n';
    for (auto& p : program) {
        p->Print();
    }


    return 0;
}
#endif
accuracy: 1
time_ms: 470
memory_kb: 3776
final_rank: 14