Problem:
Problem B:
Kaeru Jump
There is a frog living in a big pond. He loves jumping between lotus leaves floating on the pond. Interestingly, these leaves have strange habits. First, a leaf will sink into the water after the frog jumps from it. Second, they are aligned regularly as if they are placed on the grid points as in the example below.
Figure 1: Example of floating leaves
Recently, He came up with a puzzle game using these habits. At the beginning of the game, he is on some leaf and faces to the upper, lower, left or right side. He can jump forward or to the left or right relative to his facing direction, but not backward or diagonally. For example, suppose he is facing to the left side, then he can jump to the left, upper and lower sides but not to the right side. In each jump, he will land on the nearest leaf on his jumping direction and face to that direction regardless of his previous state. The leaf he was on will vanish into the water after the jump. The goal of this puzzle is to jump from leaf to leaf until there is only one leaf remaining.
See the example shown in the figure below.
In this situation, he has three choices, namely, the leaves A, B and C. Note that he cannot jump to the leaf D since he cannot jump backward. Suppose that he choose the leaf B. After jumping there, the situation will change as shown in the following figure.
He can jump to either leaf E or F next.
After some struggles, he found this puzzle difficult, since there are a lot of leaves on the pond. Can you help him to find out a solution?
Input
H W
c
1,1
...
c
1,
W
.
.
.
c
H
,1
...
c
H
,
W
The first line of the input contains two positive integers
H
and
W
(1 ≤
H
,
W
≤ 10). The following
H
lines, which contain
W
characters each, describe the initial configuration of the leaves and the frog using following characters:
'.’ : water
‘o’ : a leaf
‘U’ : a frog facing upward (i.e. to the upper side) on a leaf
‘D’ : a frog facing downward (i.e. to the lower side) on a leaf
‘L’ : a frog facing leftward (i.e. to the left side) on a leaf
‘R’ : a frog facing rightward (i.e. to the right side) on a leaf
You can assume that there is only one frog in each input. You can also assume that the total number of leaves (including the leaf the frog is initially on) is at most 30.
Output
Output a line consists of the characters ‘U’ (up), ‘D’ (down), ‘L’ (left) and ‘R’ (right) that describes a series of movements. The output should not contain any other characters, such as spaces. You can assume that there exists only one solution for each input.
Sample Input 1
2 3
Uo.
.oo
Output for the Sample Input 1
RDR
Sample Input 2
10 10
.o....o...
o.oo......
..oo..oo..
..o.......
..oo..oo..
..o...o.o.
o..U.o....
oo......oo
oo........
oo..oo....
Output for the Sample Input 2
URRULULDDLUURDLLLURRDLDDDRRDR
Sample Input 3
10 1
D
.
.
.
.
.
.
.
.
o
Output for the Sample Input 3
D

Submitted solutions:

code_snippet: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
int n,m;
char a[15][15];
bool bo[15][15];
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
char UNdir(int x)
{
    if (x==0) return 'U';
    if (x==1) return 'R';
    if (x==2) return 'D';
    if (x==3) return 'L';
}
int dir(char x)
{
    if (x=='U') return 3;
    if (x=='R') return 0;
    if (x=='D') return 1;
    if (x=='L') return 2;
}
struct node
{
    int d;
    int x[105];
    int y[105];
    int num;
    char fa;
    string ans;
};
bool in(int x,int y)
{
    return (x>=0 && x<n && y>=0 && y<m);
}
void out(node x)
{
    char xx[15][15];
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
                xx[i][j]='.';
        }
    }
    xx[x.d/m][x.d%m]=x.fa;
    for (int i = 0; i < x.num; ++i)
    {
        xx[x.x[i]][x.y[i]]='o';
    }
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            cout<<xx[i][j];
        }
        cout<<endl;
    }
    cout<<endl;
}
int ans=0,ansd=0;
int main(int argc, char *argv[])
{
    int T;
    cin>>n>>m;
    node xx;xx.num=0;
    xx.ans="";
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0 ; j < m; ++j)
        {
            cin>>a[i][j];
            if (a[i][j]=='o')
            {
                xx.x[xx.num]=i;
                xx.y[xx.num++]=j;
            }
            else if (a[i][j]=='U'||a[i][j]=='L'||a[i][j]=='R'||a[i][j]=='D')
            {
                xx.d=i*m+j;
                xx.fa=a[i][j];
            }
        }
    }
    queue<node> q;
    q.push(xx);
    bool gan=0;
    while (!q.empty())
    {
        node now=q.front();
    
        if (now.num==0)
        {
            cout<<now.ans<<endl;
            break;
        }
        q.pop();
        int x=now.d/m,y=now.d%m;
        memset(bo,0,sizeof(bo));
        bo[x][y]=1;
        for (int i = 0; i < now.num; ++i)
        {
           bo[now.x[i]][now.y[i]]=1;
        }
        int face=dir(now.fa);
        for (int i = face; i < face+3; ++i)
        {
            int ii=i%4;
            int l=1;
            int xx=x+dx[ii],yy=y+dy[ii];
            while (1)
            {
                if (!in(xx,yy)) break;
                if (bo[xx][yy])
                {
                    node go;
                    go.d=xx*m+yy;
                    go.ans=now.ans+UNdir(ii);
                    go.fa=UNdir(ii);
                    go.num=0;
                                        for (int j = 0; j < now.num; ++j)
                    {
                        int xxx=now.x[j],yyy=now.y[j];
                        if (!(xxx==xx&&yyy==yy))
                        {
                            go.x[go.num]=xxx;
                            go.y[go.num++]=yyy;
                        }
                    }
                    //cout<<x<<' '<<y<<' '<<' '<<now.num<<' '<<now.ans<<' '<<xx<<' '<<yy<<' '<<go.num<<' '<<ii<<' '<<go.ans<<endl;

                   // out(go);
                    //system("pause"); 
                    q.push(go); 
                    break;
                }
                xx+=dx[ii];
                yy+=dy[ii];
            }
        }
    }
    //system("pause");
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 7580
final_rank: 11

code_snippet: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
int n,m;
char a[15][15];
bool bo[15][15];
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
char UNdir(int x)
{
    if (x==0) return 'U';
    if (x==1) return 'R';
    if (x==2) return 'D';
    if (x==3) return 'L';
}
int dir(char x)
{
    if (x=='U') return 3;
    if (x=='R') return 0;
    if (x=='D') return 1;
    if (x=='L') return 2;
}
struct node
{
    int d;
    int x[105];
    int y[105];
    int num;
    char fa;
    string ans;
};
bool in(int x,int y)
{
    return (x>=0 && x<n && y>=0 && y<m);
}
void out(node x)
{
    char xx[15][15];
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
                xx[i][j]='.';
        }
    }
    xx[x.d/m][x.d%m]=x.fa;
    for (int i = 0; i < x.num; ++i)
    {
        xx[x.x[i]][x.y[i]]='o';
    }
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            cout<<xx[i][j];
        }
        cout<<endl;
    }
    cout<<endl;
}
int ans=0,ansd=0;
int main(int argc, char *argv[])
{
    int T;
    cin>>n>>m;
    node xx;xx.num=0;
    xx.ans="";
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0 ; j < m; ++j)
        {
            cin>>a[i][j];
            if (a[i][j]=='o')
            {
                xx.x[xx.num]=i;
                xx.y[xx.num++]=j;
            }
            else if (a[i][j]=='U'||a[i][j]=='L'||a[i][j]=='R'||a[i][j]=='D')
            {
                xx.d=i*m+j;
                xx.fa=a[i][j];
            }
        }
    }
    queue<node> q;
    q.push(xx);
    bool gan=0;
    while (!q.empty())
    {
        node now=q.front();
    
        if (now.num==0)
        {
            cout<<now.ans<<endl;
            break;
        }
        q.pop();
        int x=now.d/m,y=now.d%m;
        memset(bo,0,sizeof(bo));
        bo[x][y]=1;
        for (int i = 0; i < now.num; ++i)
        {
           bo[now.x[i]][now.y[i]]=1;
        }
        int face=dir(now.fa);
        for (int i = face; i < face+3; ++i)
        {
            int ii=i%4;
            int l=1;
            int xx=x+dx[ii],yy=y+dy[ii];
            while (1)
            {
                if (!in(xx,yy)) break;
                if (bo[xx][yy])
                {
                    node go;
                    go.d=xx*m+yy;
                    go.ans=now.ans+UNdir(ii);
                    go.fa=UNdir(ii);
                    go.num=0;
                                        for (int j = 0; j < now.num; ++j)
                    {
                        int xxx=now.x[j],yyy=now.y[j];
                        if (!(xxx==xx&&yyy==yy))
                        {
                            go.x[go.num]=xxx;
                            go.y[go.num++]=yyy;
                        }
                    }
                    //cout<<x<<' '<<y<<' '<<' '<<now.num<<' '<<now.ans<<' '<<xx<<' '<<yy<<' '<<go.num<<' '<<ii<<' '<<go.ans<<endl;

                   // out(go);
                    //system("pause"); 
                    q.push(go); 
                    break;
                }
                xx+=dx[ii];
                yy+=dy[ii];
            }
        }
    }
    //system("pause");
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 7896
final_rank: 12

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <bitset>

using namespace std;

using lint = long long;
using ldouble = long double;

const string URDL = "URDL";
vector<pair<int, int>> leaves;

string rec(int idx, int dir, int pat) {
    if (__builtin_popcount(pat) == 1) return "";
    // cerr << idx << ":" << URDL[dir] << ":" << bitset<30>(pat) << endl;

    for (int d = -1; d <= 1; ++d) {
        int ndir = (dir + d + 4) % 4;
        int nidx = -1;

        for (int i = 0; i < leaves.size(); ++i) {
            if (!((pat >> i) & 1) || i == idx) continue;

            switch (ndir) {
                case 0:
                    if (leaves[i].second != leaves[idx].second ||
                        leaves[i].first > leaves[idx].first) continue;
                    if (nidx < 0 || leaves[i].first > leaves[nidx].first) nidx = i;
                    break;

                case 1:
                    if (leaves[i].first != leaves[idx].first ||
                        leaves[i].second < leaves[idx].second) continue;
                    if (nidx < 0 || leaves[i].second < leaves[nidx].second) nidx = i;
                    break;

                case 2:
                    if (leaves[i].second != leaves[idx].second ||
                        leaves[i].first < leaves[idx].first) continue;
                    if (nidx < 0 || leaves[i].first < leaves[nidx].first) nidx = i;
                    break;

                case 3:
                    if (leaves[i].first != leaves[idx].first ||
                        leaves[i].second > leaves[idx].second) continue;
                    if (nidx < 0 || leaves[i].second > leaves[nidx].second) nidx = i;
                    break;
            }
        }

        if (nidx < 0) continue;

        int npat = pat ^ (1 << idx);
        string res = rec(nidx, ndir, npat);
        if (res != "x") {
            res.insert(res.begin(), URDL[ndir]);
            return res;
        }
    }

    return "x";
}

int main() {
    int H, W;
    cin >> H >> W;

    vector<string> S(H);
    for (auto& s : S) cin >> s;

    int si, dir;
    for (int x = 0; x < H; ++x) {
        for (int y = 0; y < W; ++y) {
            if (S[x][y] != '.') {
                leaves.emplace_back(x, y);
                if (S[x][y] != 'o') {
                    si = leaves.size() - 1;
                    for (int i = 0; i < 4; ++i) {
                        if (S[x][y] == URDL[i]) dir = i;
                    }
                }
            }
        }
    }

    cout << rec(si, dir, (1 << leaves.size()) - 1) << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3232
final_rank: 6

code_snippet: #include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
char d[4] = {'D', 'R', 'U', 'L'};

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

    int H, W;
    cin >> H >> W;
    vector<string> s(H);
    vector<P> ls;
    int sx, sy, dir;
    map<P, int> rev;
    for (int i = 0; i < H; i++)
    {
        cin >> s[i];
        for (int j = 0; j < W; j++)
        {
            if (s[i][j] == '.')
                continue;
            if (s[i][j] == 'o')
            {
                rev[P(i, j)] = ls.size();
                ls.push_back(P(i, j));
                continue;
            }
            sx = i;
            sy = j;
            if (s[i][j] == 'D')
                dir = 0;
            else if (s[i][j] == 'R')
                dir = 1;
            else if (s[i][j] == 'U')
                dir = 2;
            else
                dir = 3;
            s[i][j] = '.';
        }
    }
    // pos, dir, mask
    map<T, string> mp;
    int sz = ls.size();
    ll mask = (1LL << sz) - 1;
    ls.push_back(P(sx, sy));
    queue<T> q;
    q.push(T(sz, dir, mask));
    mp[T(sz, dir, mask)] = "";
    while (!q.empty())
    {
        T t = q.front();
        q.pop();
        int pos = get<0>(t);
        int dir = get<1>(t);
        mask = get<2>(t);
        for (int i = 0; i < 4; i++)
        {
            if (abs(dir - i) == 2)
                continue;
            for (int j = 1; j < 10; j++)
            {
                int nx = ls[pos].first + j * dx[i], ny = ls[pos].second + j * dy[i];
                if (nx < 0 || H <= nx || ny < 0 || W <= ny)
                    continue;
                if (s[nx][ny] != 'o')
                    continue;
                int idx = rev[P(nx, ny)];
                // 訪問済み
                if (((mask >> idx) & 1) == 0)
                    continue;
                ll mask2 = mask ^ (1LL << idx);
                T nt = T(idx, i, mask2);
                if (mp.find(nt) != mp.end())
                    break;
                q.push(nt);
                string ns = mp[t];
                ns += d[i];
                mp[nt] = ns;
                break;
            }
        }
    }
    for(auto t:mp){
        if(get<2>(t.first) == 0){
            cout<<t.second<<endl;
            return 0;
        }
    }
    assert(false);
}
accuracy: 1
time_ms: 10
memory_kb: 7196
final_rank: 10

code_snippet: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

template<typename T>
ostream& operator << (ostream& os, vector<T>& v){
	rep(i,v.size()){ os << v[i] << (i == v.size() - 1 ? "" : " "); } return os;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& v){
	for(T& x: v){ is >> x; } return is;
}

int h, w;

const int dy[16] = { 0,-1, 0, 1, 1,-1, 1,-1, 0,-2, 0, 2};
const int dx[16] = { 1, 0,-1, 0, 1, 1,-1,-1, 2, 0,-2, 0};
const char C[4] = {'R', 'U', 'L', 'D'};

struct Data{
	vector<vector<char>> c;
	int leves;
	int y, x, d;
	string s;
	Data(vector<vector<char>>& c, int leves, int y, int x, int d, string s) : c(c), leves(leves), y(y), x(x), d(d), s(s) {}
};

string bfs(Data& start){
	queue<Data> q;
	q.emplace(start);

	while(not q.empty()){
		Data p = q.front(); q.pop();
		//cout << p.y << ' ' << p.x <<' ' << p.d << endl;
		//for(auto i : p.c){ for(auto j : i){ cout << j << ' '; } cout << endl; } cout << endl;


		if(p.leves == 1) return p.s;
		if(p.s.size() >= 30) continue;

		rep(dir,4){
			if((p.d + 2) % 4 == dir) continue;
			int ny = p.y + dy[dir];
			int nx = p.x + dx[dir];
			if(ny < 0 || ny >= h || nx < 0 || nx >= w) continue;

			bool f = true;
			while(p.c[ny][nx] == '.'){
				ny += dy[dir];
				nx += dx[dir];
				if(ny < 0 || ny >= h || nx < 0 || nx >= w){
					f = false;
					break;
				}
			}
			if(not f) continue;

			p.c[p.y][p.x] = '.';
			q.emplace(p.c, p.leves - 1, ny, nx, dir, p.s + C[dir]);
			p.c[p.y][p.x] = 'o';
		}
	}
	assert(false);
	return "null";
}

int main(){
	cin >> h >> w;

	vector<vector<char>> c(h, vector<char>(w));
	int y, x, leves = 0;
	char dir;
	rep(i,h) rep(j,w){
		cin >> c[i][j];
		if(isupper(c[i][j])){
			y = i;
			x = j;
			dir = c[i][j];
			c[i][j] = 'o';
		}
		if(c[i][j] == 'o') leves++;
	}

	rep(i,4) if(C[i] == dir){
		Data data(c, leves, y, x, i, "");
		cout << bfs(data) << endl;
		return 0;
	}
}
accuracy: 1
time_ms: 30
memory_kb: 6588
final_rank: 15

code_snippet: #include <bits/stdc++.h>
                                        #include<iostream>
                                        #include<cstdio>
                                        #include<vector>
                                        #include<queue>
                                        #include<map>
                                        #include<cstring>
                                        #include<string>
                                        #include <math.h>
                                        #include<algorithm>
                                    //    #include <boost/multiprecision/cpp_int.hpp>
                                        #include<functional>
                               #define int long long
                                        #define inf  1000000007
                                        #define pa pair<int,int>
                                        #define ll long long
                                        #define pal pair<double,pa>
                                        #define ppap pair<pa,int>
                                  
                                        #define ssa pair<string,int>
                                        #define  mp make_pair
                                        #define  pb push_back
                                        #define EPS (1e-10)
                                        #define equals(a,b) (fabs((a)-(b))<EPS)
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
                                        using namespace std;
                               class pas{
                                        	public:
                                        	int x,y,h;
                                        	pas(int x=0,int y=0,int h=0):x(x),y(y),h(h) {}
                                        	bool operator < (const pas &p) const{
                                        		return x != p.x ? x<p.x: y<p.y;
                                        	}
                                        	bool operator == (const pas &p) const{
                                        		return abs(x-p.x)==0 && abs(y-p.y)==0;
                                        	}
                                        		
                                        
                                        };
                                        class pa2{
                                        	public:
                                        	int x,y;
                                        	pa2(int x=0,int y=0):x(x),y(y) {}
                                        	pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);}
                                        	bool operator < (const pa2 &p) const{
                                        		return x != p.x ? x<p.x: y<p.y;
                                        	}
                                        	bool operator == (const pa2 &p) const{
                                        		return abs(x-p.x)==0 && abs(y-p.y)==0;
                                        	}
                                        		
                                        
                                        };
                                        
                                               #define ppa pair<int,pas>
                                        class Point{
                                        	public:
                                        	double x,y;
                                        	Point(double x=0,double y=0):x(x),y(y) {}
                                        	Point operator + (Point p) {return Point(x+p.x,y+p.y);}
                                        	Point operator - (Point p) {return Point(x-p.x,y-p.y);}
                                        	Point operator * (double a) {return Point(x*a,y*a);}
                                        	Point operator / (double a) {return Point(x/a,y/a);}
                                        	double absv() {return sqrt(norm());}
                                        	double norm() {return x*x+y*y;}
                                        	bool operator < (const Point &p) const{
                                        		return x != p.x ? x<p.x: y<p.y;
                                        	}
                                        	bool operator == (const Point &p) const{
                                        		return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;
                                        	}
                                        };
                                        typedef Point Vector;
                                 #define pl pair<int,pas>
                                        struct Segment{
                                        Point p1,p2;
                                        };
                                         double dot(Vector a,Vector b){
                                        	return a.x*b.x+a.y*b.y;
                                        }
                                        double cross(Vector a,Vector b){
                                        	return a.x*b.y-a.y*b.x;
                                        }
                                    
            bool parareru(Point a,Point b,Point c,Point d){
            //	if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl;
            	return abs(cross(a-b,d-c))<EPS;
            }
            double distance_ls_p(Point a, Point b, Point c) {
              if ( dot(b-a, c-a) < EPS ) return (c-a).absv();
              if ( dot(a-b, c-b) < EPS ) return (c-b).absv();
              return abs(cross(b-a, c-a)) / (b-a).absv();
            }
            bool is_intersected_ls(Segment a,Segment b) {
            	if(a.p1==b.p1||a.p2==b.p1||a.p1==b.p2||a.p2==b.p2) return false;
            	if(parareru((a.p2),(a.p1),(a.p1),(b.p2))&&parareru((a.p2),(a.p1),(a.p1),(b.p1))){
            //		cout<<"sss"<<endl;
            		if(dot(a.p1-b.p1,a.p1-b.p2)<EPS) return true;
            		if(dot(a.p2-b.p1,a.p2-b.p2)<EPS) return true;
            		if(dot(a.p1-b.p1,a.p2-b.p1)<EPS) return true;
            		if(dot(a.p1-b.p2,a.p2-b.p2)<EPS) return true;
            		return false;
            	}
              else return ( cross(a.p2-a.p1, b.p1-a.p1) * cross(a.p2-a.p1, b.p2-a.p1) < EPS ) && ( cross(b.p2-b.p1, a.p1-b.p1) * cross(b.p2-b.p1, a.p2-b.p1) < EPS );
            }
             
            double segment_dis(Segment a,Segment b){
            	if(is_intersected_ls(a,b))return 0;
            	double r=distance_ls_p(a.p1, a.p2, b.p1);
            	r=min(r,distance_ls_p(a.p1, a.p2, b.p2));
            	r=min(r,distance_ls_p(b.p1, b.p2, a.p2));
            	r=min(r,distance_ls_p(b.p1, b.p2, a.p1));
            	return r;
            }
            Point intersection_ls(Segment a, Segment b) {
              Point ba = b.p2-b.p1;
              double d1 = abs(cross(ba, a.p1-b.p1));
              double d2 = abs(cross(ba, a.p2-b.p1));
              double t = d1 / (d1 + d2);
             
              return a.p1 + (a.p2-a.p1) * t;
            }
             
                            string itos( int i ) {
                            ostringstream s ;
                            s << i ;
                            return s.str() ;
                            }
                             
                            int gcd(int v,int b){
                            	if(v>b) return gcd(b,v);
                            	if(v==b) return b;
                            	if(b%v==0) return v;
                            	return gcd(v,b%v);
                            }
             
                            double distans(double x1,double y1,double x2,double y2){
                            	double rr=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
                            	return sqrt(rr);
                            	
                            }
                            
   //         int pr[2000010];
     //       int inv[2000010];
            /*
            int beki(int wa,int rr,int warukazu){
            	if(rr==0) return 1ll;
            	if(rr==1) return wa%warukazu;
            	if(rr%2==1) return (beki(wa,rr-1,warukazu)*wa)%warukazu;
            	int zx=beki(wa,rr/2,warukazu);
            	return (zx*zx)%warukazu;
            }
            
			int comb(int nn,int rr){
				int r=pr[nn]*inv[rr];
				r%=inf;
				r*=inv[nn-rr];
				r%=inf;
				return r;
			}
            
            void gya(int ert){
            	pr[0]=1;
            	for(int i=1;i<ert;i++){
            		pr[i]=(pr[i-1]*i)%inf;
            	}
            	for(int i=0;i<ert;i++) inv[i]=beki(pr[i],inf-2,inf);
            	
            }
             */
            
             //sort(ve.begin(),ve.end(),greater<int>());
                                     //----------------kokomade tenpure------------
            //vector<double> ans(100000000),ans2(100000000);

int h,w;
int cnt;
int a[15][15]={0};
string ans;
void dfs(int x,int y,string muki,int nokori){
	//cout<<x<<" "<<y<<" "<<nokori<<endl;
	if(nokori==0){
		cout<<ans<<endl;
		exit(0);
	}
	if(muki!="D"){
		int dx=x-1,dy=y;
		while(a[dx][dy]==0)dx--;
		if(a[dx][dy]!=2){
			a[x][y]=0;
			string ans2=ans;
			ans+="U";
			dfs(dx,dy,"U",nokori-1);
			ans=ans2;
			a[x][y]=1;
		}
	}
	if(muki!="U"){
		int dx=x+1,dy=y;
		while(a[dx][dy]==0)dx++;
		if(a[dx][dy]!=2){
			a[x][y]=0;
			string ans2=ans;
			ans+="D";
			dfs(dx,dy,"D",nokori-1);
			ans=ans2;
			a[x][y]=1;
		}
	}
	
	if(muki!="L"){
		int dx=x,dy=y+1;
		while(a[dx][dy]==0)dy++;
		if(a[dx][dy]!=2){
			a[x][y]=0;
			string ans2=ans;
			ans+="R";
			dfs(dx,dy,"R",nokori-1);
			ans=ans2;
			a[x][y]=1;
		}
	}
	if(muki!="R"){
		int dx=x,dy=y-1;
		while(a[dx][dy]==0)dy--;
		if(a[dx][dy]!=2){
			a[x][y]=0;
			string ans2=ans;
			ans+="L";
			dfs(dx,dy,"L",nokori-1);
			ans=ans2;
			a[x][y]=1;
		}
	}
}




   signed main(){

   	for(int i=0;i<15;i++)a[i][0]=2,a[0][i]=2,a[14][i]=2,a[i][14]=2;
   	int x,y;
   	string st;
   	cin>>h>>w;
   	cnt=0;
   	for(int i=1;i<=h;i++){
   		string s;
   		cin>>s;
   		s="d"+s;
   		for(int j=1;j<=w;j++){
   			if(s[j]=='o')cnt++, a[i][j]=1;
   			if(s[j]=='U'){
   				st="U";
   				x=i,y=j;
   			}
   			if(s[j]=='D'){
   				st="D";
   				x=i,y=j;
   			}
   			if(s[j]=='L'){
   				st="L";
   				x=i,y=j;
   			}
   			if(s[j]=='R'){
   				st="R";
   				x=i,y=j;
   			}
   		}
   	}
  // cout<<x<<" "<<y<<endl;
   	a[x][y]=1;
   	ans="";
   	dfs(x,y,st,cnt);
   	
   	
   	return 0;
        }
accuracy: 1
time_ms: 10
memory_kb: 3164
final_rank: 5

code_snippet: #include<iostream>
#include<queue>
#include<string>
using namespace std;
struct Kaeru { char b[10][10]; int px, py, dir; string F; };
int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
queue<Kaeru>Q; int H, W; char x[10][10]; int cx, cy, cd = 0;
char CC[5] = "URDL";
int main() {
	cin >> H >> W;
	while (!Q.empty())Q.pop(); int CNT = 0;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			cin >> x[i][j]; if (x[i][j] == 'o')CNT++;
			if (x[i][j] == 'U') { cx = i; cy = j; cd = 0; x[i][j] = '.'; }
			if (x[i][j] == 'R') { cx = i; cy = j; cd = 1; x[i][j] = '.'; }
			if (x[i][j] == 'D') { cx = i; cy = j; cd = 2; x[i][j] = '.'; }
			if (x[i][j] == 'L') { cx = i; cy = j; cd = 3; x[i][j] = '.'; }
		}
	}
	Kaeru D; for (int i = 0; i < 100; i++)D.b[i / 10][i % 10] = x[i / 10][i % 10];
	D.px = cx; D.py = cy; D.dir = cd; D.F = ""; Q.push(D);
	while (!Q.empty()) {
		Kaeru a1 = Q.front(); Q.pop();
		Kaeru a2 = a1;
		if (a2.F.size() == CNT) { cout << a2.F << endl; break; }

		for (int i = 0; i < 4; i++) {
			a2 = a1; char y[10][10]; for (int j = 0; j < 100; j++)y[j / 10][j % 10] = a2.b[j / 10][j % 10];
			if (i == (a1.dir + 2) % 4)continue;
			int ex = a2.px, ey = a2.py;
			while (true) {
				ex += dx[i]; ey += dy[i];
				if (ex < 0 || ey < 0 || ex >= H || ey >= W)break;
				if (y[ex][ey] == 'o') {
					y[ex][ey] = '.';
					for (int j = 0; j < 100; j++)a2.b[j / 10][j % 10] = y[j / 10][j % 10];
					a2.px = ex; a2.py = ey; a2.dir = i; a2.F += CC[i];
					Q.push(a2); break;
				}
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 4224
final_rank: 16

code_snippet: #include<stdio.h>
#include <iostream>
#include <math.h>
#include <numeric>
#include <vector>
#include <map>
#include <functional>
#include <stdio.h>
#include <array>
#include <algorithm>
#include <string>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <queue>
#include<iomanip>
#include<bitset>
#include<stack>
#include<set>
#include<limits>
#include <complex>
using namespace std;

const int mod = 1000000007;
struct Mod {
public:
	int num;
	Mod() : num(0) { ; }
	Mod(long long int n) : num((n % mod + mod) % mod) { ; }
	Mod(int n) : num((n % mod + mod) % mod) { ; }
	operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = (a * a) ^ (n / 2);
	if (n % 2) res = res * a;
	return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
	assert(b.num != 0);
	return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
	assert(b.num != 0);
	return Mod(a) * inv(b);
}
Mod operator/=(Mod &a, const Mod b) {
	assert(b.num != 0);
	return a = a * inv(b);
}

#define MAX_N 1024000

Mod fact[MAX_N], factinv[MAX_N];
void init() {
	fact[0] = Mod(1); factinv[0] = 1;
	for (int i = 0; i < MAX_N - 1; ++i) {
		fact[i + 1] = fact[i] * Mod(i + 1);
		factinv[i + 1] = factinv[i] / Mod(i + 1);
	}
}
Mod comb(const int a, const int b) {
	return fact[a] * factinv[b] * factinv[a - b];
}
template<typename T>
vector<vector<T>> keisann(const vector<vector<T>>l, const vector<vector<T>>r) {
	vector<vector<T>>ans(l.size(), vector<T>(r[0].size()));
	assert(l[0].size() == r.size());
	for (unsigned int h = 0; h < l.size(); ++h) {
		for (unsigned int i = 0; i < r.size(); ++i) {
			for (unsigned int w = 0; w < r[0].size(); ++w) {
			
				ans[h][w] +=l[h][i] * r[i][w];
			}
		}
	}
	return ans;
}
template<typename T>
vector<vector<T>>powgyou(vector<vector<T>>a, const long long int n) {
	assert(a.size() == a[0].size());
	if (!n) {
		vector<vector<T>>e(a.size(), vector<T>(a[0].size()));
		for (unsigned int i = 0; i < a.size(); ++i) {
			e[i][i] = 1;
		}
		return e;
	}
	if (n == 1)return a;
	else {
		vector<vector<T>>ans(a.size(), vector<T>(a[0].size(), 0));
		ans = powgyou(a, n / 2);
		ans = keisann(ans, ans);
		if (n % 2) {
			ans = keisann(ans, a);
		}
		return ans;
	}
}



long long int powint(long long int a, int b) {
	if (b == 0)return 1;
	if (b == 1)return a;
	else {
		long long int ans = 1;
		long long int c = powint(a, b / 2);
		ans *= c*c;
		ans *= (b % 2) ? a : 1;
		return ans;
	}
	
}

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


vector<vector<int>>fd(10, vector<int>(10,0));
vector<int>ans;

int aok(vector<vector<int>>a, const int ax,const int ay,const int away,int count) {
	if (count == 0)return 1;
	for (int i = 0; i < 4; ++i) {
		if ((i + 2) % 4 == away) {
			continue;
		}

		int nx(ax), ny(ay);

		while (1) {
			nx += dx[i]; ny += dy[i];
			if (nx >= 10 || nx < 0 || ny>=10 || ny < 0)break;
			if (a[ny][nx]) {
				a[ny][nx] = 0;
				if (aok(a, nx, ny, i,count-1) != -1) {
					ans.push_back(i);
					return 1;
				}
				else {
					a[ny][nx] = 1;
					break;
				}
				a[ny][nx] = 1;
			}
		}
	}
	return -1;
}

int main(void) {
	int H, W; cin >> H >> W;
	int way;
	int count = 0;
	int ax, ay;
	for (int i = 0; i < H; ++i) {
		string st; cin >> st;
		for (int j = 0; j < W; ++j) {
			if (st[j] == '.') {
				fd[i][j] = 0;
			}
			else if (st[j] == 'o') {
				fd[i][j] = 1;
				count++;
			}
			else {
				ax = j; ay = i;
				fd[i][j] = 0;
				if (st[j] == 'U') {
					way = 3;
				}
				else if (st[j] == 'D') {
					way = 1;
				}
				else if (st[j] == 'L') {
					way = 0;
				}
				else {
					way = 2;
				}
			}
		}
	}
	aok(fd, ax, ay, way, count);
	reverse(ans.begin(), ans.end());
	for (int i = 0; i < ans.size(); ++i) {
		if (ans[i] == 0) {
			cout << 'L';
		}
		else if (ans[i] == 1) {
			cout << 'D';
		}
		else if (ans[i] == 2) {
			cout << 'R';
		}
		else {
			cout << 'U';
		}
	}
	cout << endl;
	return 0;
}

/*
4
0 4
7 12
6 8
4 6

*/
accuracy: 1
time_ms: 10
memory_kb: 11232
final_rank: 14

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

using namespace std;

#define LOG(...) fprintf(stderr,__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
const char dir[] = {'L', 'D', 'R', 'U'};

struct P {
    int x, y;
    int d;
    int cnt;
    vvb G;
    string moves;
};

int H, W;

int main() {
    cin >> H >> W;

    vvb G(H, vb(W, false));
    int sx, sy, d;
    int cnt = 0;
    REP(y, H) REP(x, W) {
        char c;
        cin >> c;
        if (c == 'o') {
            cnt++;
            G[y][x] = true;
        } else if (c != '.') {
            sx = x;
            sy = y;
            d = find(dir, dir+4, c) - dir;
        }
    }

    queue<P> que;
    string moves;
    que.push({sx, sy, d, 0, G, moves});

    while (!que.empty()) {
        P p = que.front(); que.pop();
        if (p.cnt >= 30) continue;
        if (p.cnt == cnt) {
            cout << p.moves << endl;
            break;
        }
        int x = p.x, y = p.y;
        if (p.d != 0) for (int x = p.x; x < W; x++) {
            if (p.G[y][x]) {
                vvb g = p.G;
                g[y][x] = false;
                que.push({x, y, 2, p.cnt+1, g, p.moves + "R"});
                break;
            }
        }
        if (p.d != 2) for (int x = p.x; x >= 0; x--) {
            if (p.G[y][x]) {
                vvb g = p.G;
                g[y][x] = false;
                que.push({x, y, 0, p.cnt+1, g, p.moves + "L"});
                break;
            }
        }
        if (p.d != 3) for (int y = p.y; y < H; y++) {
            if (p.G[y][x]) {
                vvb g = p.G;
                g[y][x] = false;
                que.push({x, y, 1, p.cnt+1, g, p.moves + "D"});
                break;
            }
        }
        if (p.d != 1) for (int y = p.y; y >= 0; y--) {
            if (p.G[y][x]) {
                vvb g = p.G;
                g[y][x] = false;
                que.push({x, y, 3, p.cnt+1, g, p.moves + "U"});
                break;
            }
        }
    }
}
accuracy: 1
time_ms: 60
memory_kb: 7476
final_rank: 17

code_snippet: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

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

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

  int H, W; cin >> H >> W;
  VS vs(H); REP(i,H) cin >> vs[i];

  queue<VS> q;
  map<VS,pair<VS,char>> memo;
  q.push(vs);
  memo[vs] = MP(VS(), '!');
  VS goal;
  while(!q.empty()){
	VS vs = q.front(); q.pop();
	int sx = -1, sy = -1, d = -1, cnt = 0;
	REP(y,H) REP(x,W){
	  if(vs[y][x] == '.') continue;
	  else if(vs[y][x] == 'o')
		++cnt;
	  else{
		sx = x; sy = y;
		if(vs[y][x] == 'U') d = 0;
		if(vs[y][x] == 'D') d = 1;
		if(vs[y][x] == 'L') d = 2;
		if(vs[y][x] == 'R') d = 3;
	  }
	}
	if(cnt == 0){
	  goal = vs;
	  break;
	}

	REP(i,4){
	  if(i == d) continue;
	  int tx = sx, ty = sy;
	  while(0<=tx&&tx<W&&0<=ty&&ty<H){
		if(vs[ty][tx] == 'o') break;
		tx += dx[i];
		ty += dy[i];
	  }

	  if(0<=tx&&tx<W&&0<=ty&&ty<H){
		VS vs_ = vs;
		vs_[sy][sx] = '.';
		char ch;
		switch(i){
		case 0: ch = 'D'; break;
		case 1: ch = 'U'; break;
		case 2: ch = 'R'; break;
		case 3: ch = 'L'; break;
		}
		vs_[ty][tx] = ch;
		if(!memo.count(vs_)){
		  memo[vs_] = MP(vs,ch);
		  q.push(vs_);
		}
	  }
	}
  }

  string ans;
  while(!goal.empty()){
	if(memo[goal].second == '!') break;
	ans += string(1, memo[goal].second);
	goal = memo[goal].first;
  }
  reverse(ALL(ans));
  cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 34736
final_rank: 19

code_snippet: #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <functional>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
int H,W;
vector<string> s;
string ans;
string ddr="URDL";
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};	//URDL
bool on(int x,int y){
	return 0<=x&&x<H&&0<=y&&y<W;
}
bool dfs(int x,int y,int di,int cnt,vector<string> s){
/*	rep(i,H) cout<<s[i]<<endl;
	show(cnt);
	puts("");*/
	if(cnt==0) return true;
	for(int nd=(di+3)%4;nd!=(di+2)%4;nd=(nd+1)%4){
		int nx=x,ny=y;
		while(on(nx,ny)){
			if(s[nx][ny]=='o'){
				s[nx][ny]='.';
				if(dfs(nx,ny,nd,cnt-1,s)){
					ans=ddr[nd]+ans;
					return true;
				}
				s[nx][ny]='o';
				break;
			}
			nx+=dx[nd],ny+=dy[nd];
		}
	}
	return false;
}
int main(){
	cin>>H>>W;
	rep(i,H){
		string st;
		cin>>st;
		s.pb(st);
	}
	int sx,sy,di,cnt=0;
	rep(i,H) rep(j,W){
		if(s[i][j]=='o') cnt++;
		if(s[i][j]=='U') sx=i,sy=j,di=0,s[i][j]='.';
		if(s[i][j]=='R') sx=i,sy=j,di=1,s[i][j]='.';
		if(s[i][j]=='D') sx=i,sy=j,di=2,s[i][j]='.';
		if(s[i][j]=='L') sx=i,sy=j,di=3,s[i][j]='.';
	}
	dfs(sx,sy,di,cnt,s);
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 1228
final_rank: 7

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

int h,w;
const int dy[] = {-1,0,1,0}, dx[] = {0,1,0,-1};
const string dir = "URDL";
vector<string> g;

string dfs(int y,int x,int d,int rem,string s){
  if(rem==0)return s;
  char tmp = g[y][x];
  g[y][x] = '.';

  for(int i=3;i<=5;i++){
    int nd = (d+i)%4, ny = y + dy[nd], nx = x + dx[nd];
    while(ny>=0 && nx>=0 && ny<h && nx<w){
      if(g[ny][nx] == 'o'){
	string tmp = dfs(ny,nx,nd,rem-1,s+dir[nd]);
	if(tmp.size())return tmp;
	break;
      }
      ny += dy[nd]; nx += dx[nd];
    }
  }

  g[y][x] = tmp;
  return "";
}

int main(){
  int y,x,d,cnt = 0;
  cin >> h >> w;
  g.resize(h);
  for(int i=0;i<h;i++){
    cin >> g[i];
    for(int j=0;j<w;j++){
      if(isupper(g[i][j])){
	y = i; x = j;
	for(int k=0;k<4;k++){
	  if(dir[k] == g[i][j])d = k;
	}
      }else if(g[i][j] == 'o')cnt++;
    }
  }
  cout << dfs(y,x,d,cnt,"") << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 1216
final_rank: 3

code_snippet: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

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

struct state {
	int x, y, dir;
	vs field;
	string command;
	state(int x, int y, int dir, vs field, string command) : x(x), y(y), dir(dir), field(field), command(command) {};
};

int getDir(char c) {
	switch(c) {
	case 'U':
		return 3;
	case 'D':
		return 1;
	case 'L':
		return 2;
	case 'R':
		return 0;
	}
}

char getDirChar(int dir) {
	switch(dir) {
	case 0:
		return 'R';
	case 1:
		return 'D';
	case 2:
		return 'L';
	case 3:
		return 'U';
	}
}

bool isProperXY(int x, int y, int H, int W) {
	return 0 <= x && x < W && 0 <= y && y < H;
}

bool isEmptyField(vs field) {
	REP(i, field.size()) {
		REP(j, field[i].size()) {
			if(field[i][j] != '.') {
				return false;
			}
		}
	}
	return true;
}

int main() {
	int H, W;
	cin >> H >> W;
	vs field(H);

	int x, y, dir;
	REP(i, H) {
		cin >> field[i];
		REP(j, field[i].size()) {
			if(field[i][j] != '.' && field[i][j] != 'o') {
				x = j;
				y = i;
				dir = getDir(field[i][j]);
				field[i][j] = '.';
			}
		}
	}

	string ans;
	stack<state> S;
	S.push(state(x, y, dir, field, ""));
	while(!S.empty()) {
		state st = S.top();
		S.pop();

		if(isEmptyField(st.field)) {
			ans = st.command;
			break;
		}

		FOR(d, -1, 1) {
			int nx = st.x, ny = st.y, ndir = (st.dir+d+4)%4;
			bool update = true;
			while(st.field[ny][nx] != 'o') {
				nx += dx[ndir], ny += dy[ndir];
				if(!isProperXY(nx, ny, H, W)) {
					update = false;
					break;
				}
			}

			if(update) {
				vs nfield = st.field;
				nfield[ny][nx] = '.';
				string ncommand = st.command;
				ncommand.push_back(getDirChar(ndir));
				S.push(state(nx, ny, ndir, nfield, ncommand));
			}
		}
	}

	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 1232
final_rank: 9

code_snippet: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

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

struct state {
	int x, y, dir;
	vs field;
	string command;
	state(int x, int y, int dir, vs field, string command) : x(x), y(y), dir(dir), field(field), command(command) {};
};

int getDir(char c) {
	switch(c) {
	case 'U':
		return 3;
	case 'D':
		return 1;
	case 'L':
		return 0;
	case 'R':
		return 2;
	}
}

char getDirChar(int dir) {
	switch(dir) {
	case 0:
		return 'R';
	case 1:
		return 'D';
	case 2:
		return 'L';
	case 3:
		return 'U';
	}
}

bool isProperXY(int x, int y, int H, int W) {
	return 0 <= x && x < W && 0 <= y && y < H;
}

bool isEmptyField(vs field) {
	REP(i, field.size()) {
		REP(j, field[i].size()) {
			if(field[i][j] != '.') {
				return false;
			}
		}
	}
	return true;
}

int main() {
	int H, W;
	cin >> H >> W;
	vs field(H);

	int x, y, dir;
	REP(i, H) {
		cin >> field[i];
		REP(j, field[i].size()) {
			if(field[i][j] != '.' && field[i][j] != 'o') {
				x = j;
				y = i;
				dir = getDir(field[i][j]);
				field[i][j] = '.';
			}
		}
	}

	string ans;
	stack<state> S;
	S.push(state(x, y, dir, field, ""));
	while(!S.empty()) {
		state st = S.top();
		S.pop();

		if(isEmptyField(st.field)) {
			ans = st.command;
			break;
		}

		FOR(d, -1, 1) {
			int nx = st.x, ny = st.y, ndir = (st.dir+d+4)%4;
			bool update = true;
			while(st.field[ny][nx] != 'o') {
				nx += dx[ndir], ny += dy[ndir];
				if(!isProperXY(nx, ny, H, W)) {
					update = false;
					break;
				}
			}

			if(update) {
				vs nfield = st.field;
				nfield[ny][nx] = '.';
				string ncommand = st.command;
				ncommand.push_back(getDirChar(ndir));
				S.push(state(nx, ny, ndir, nfield, ncommand));
			}
		}
	}

	cout << ans << endl;
	return 0;
}
accuracy: 0.38333333333333336
time_ms: 30
memory_kb: 1228
final_rank: 20

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
 
using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;
 
const int tx[] = {+0,+1,+0,-1};
const int ty[] = {-1,+0,+1,+0};
 
static const double EPS = 1e-8;

int init_dic(int x,int y,
	    map<P,int>& dic){
  if(dic.find(P(x,y)) == dic.end()){
    int idx = dic.size();
    dic[P(x,y)] = idx;
  }
  return dic[P(x,y)];
}

void fill_dic(int W,int H,map<P,int>& dic){
  for(int x=0;x<=W;x++){
    for(int y=0;y<=H;y++){
      if(dic.find(P(x,y)) == dic.end()){
	dic[P(x,y)] = -1;
      }
    }
  }
}

class State{
public:
  int x;
  int y;
  int dir;
  int remaining;
  int cost;
  vector<char> route;
  State(int _x,int _y,int _dir,int _remaining,int _cost,const vector<char>& _r)
    : x(_x),y(_y),dir(_dir),remaining(_remaining),cost(_cost),route(_r) {}
  State(int _x,int _y,int _dir,int _remaining,int _cost)
    : x(_x),y(_y),dir(_dir),remaining(_remaining),cost(_cost) {}
  bool operator<(const State& s) const{
    return cost < s.cost;
  }
  bool operator>(const State& s) const{
    return cost > s.cost;
  }
};

int main(){
  int H,W;
  while(~scanf("%d %d",&H,&W)){
    char stage[101][101];
    int sx,sy;
    int dir;
    int init_state=0;
    map<P,int> dic;

    for(int y=0;y<H;y++){
      char buf[101];
      scanf("%s",buf);
      for(int x=0;x<W;x++){
	stage[y][x] = buf[x];
	if(buf[x] == 'U' || buf[x] == 'R'
	   || buf[x] == 'D' || buf[x] == 'L'){
	  sx = x;
	  sy = y;
	  init_state |= (1<<init_dic(x,y,dic));

	  const char dirs[4] = {'U','R','D','L'};
	  for(int i=0;i<4;i++){
	    if(dirs[i] == buf[x]){
	      dir = i;
	      break;
	    }
	  }
	}
	if(buf[x] == 'o'){
	  init_state |= (1<<init_dic(x,y,dic));
	}
      }
    }
    
    fill_dic(W,H,dic);
    priority_queue<State,vector<State>,greater<State> > que;
    que.push(State(sx,sy,dir,init_state,0));

    set<int> visited[101][101][4];
    bool flag = false;
    while(!que.empty()){
      State s = que.top();
      que.pop();

      if(visited[s.x][s.y][s.dir].count(s.remaining) > 0) continue;
      visited[s.x][s.y][s.dir].insert(s.remaining);

      if(__builtin_popcount(s.remaining) == 1){
	const char dirs[4] = {'U','R','D','L'};
	string ans = "";
	for(int i=0; i<s.route.size(); i++){
	  ans += dirs[s.route[i]];
	}
	cout << ans << endl;
      }

      for(int i=0;i<4;i++){
	if(i == (s.dir + 2) % 4) continue;

	int dx,dy;
	for(int j=1;j<=max(H,W);j++){
	  dx = s.x + tx[i] * j;
	  dy = s.y + ty[i] * j;
	  if(dx < 0 || dy < 0 || dx >= W || dy >= H ) continue;
	  if(dic[P(dx,dy)] == -1) continue;
	  
	  if(s.remaining & (1<<dic[P(dx,dy)])){
	    int next = s.remaining & ~(1<<dic[P(s.x,s.y)]);
	    vector<char> next_route = s.route;
	    next_route.push_back(i);
	    que.push(State(dx,dy,i,next,s.cost+1,next_route));
	    break;
	  }
	}
      }
    }
  }
}
accuracy: 1
time_ms: 30
memory_kb: 4524
final_rank: 13

code_snippet: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
int n,m;
char a[15][15];
bool bo[15][15];
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
char UNdir(int x)
{
    if (x==0) return 'U';
    if (x==1) return 'R';
    if (x==2) return 'D';
    if (x==3) return 'L';
}
int dir(char x)
{
    if (x=='U') return 3;
    if (x=='R') return 0;
    if (x=='D') return 1;
    if (x=='L') return 2;
}
struct node
{
    int d;
    int x[105];
    int y[105];
    int num;
    char fa;
    string ans;
};
bool in(int x,int y)
{
    return (x>=0 && x<n && y>=0 && y<m);
}
void out(node x)
{
    char xx[15][15];
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
                xx[i][j]='.';
        }
    }
    xx[x.d/m][x.d%m]=x.fa;
    for (int i = 0; i < x.num; ++i)
    {
        xx[x.x[i]][x.y[i]]='o';
    }
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            cout<<xx[i][j];
        }
        cout<<endl;
    }
    cout<<endl;
}
int ans=0,ansd=0;
int main(int argc, char *argv[])
{
    int T;
    cin>>n>>m;
    node xx;xx.num=0;
    xx.ans="";
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0 ; j < m; ++j)
        {
            cin>>a[i][j];
            if (a[i][j]=='o')
            {
                xx.x[xx.num]=i;
                xx.y[xx.num++]=j;
            }
            else if (a[i][j]=='U'||a[i][j]=='L'||a[i][j]=='R'||a[i][j]=='D')
            {
                xx.d=i*m+j;
                xx.fa=a[i][j];
            }
        }
    }
    queue<node> q;
    q.push(xx);
    bool gan=0;
    while (!q.empty())
    {
        node now=q.front();
    
        if (now.num==0)
        {
            cout<<now.ans<<endl;
            break;
        }
        q.pop();
        int x=now.d/m,y=now.d%m;
        memset(bo,0,sizeof(bo));
        bo[x][y]=1;
        for (int i = 0; i < now.num; ++i)
        {
           bo[now.x[i]][now.y[i]]=1;
        }
        int face=dir(now.fa);
        for (int i = face; i < face+3; ++i)
        {
            int ii=i%4;
            int l=1;
            int xx=x+dx[ii],yy=y+dy[ii];
            while (1)
            {
                if (!in(xx,yy)) break;
                if (bo[xx][yy])
                {
                    node go;
                    go.d=xx*m+yy;
                    go.ans=now.ans+UNdir(ii);
                    go.fa=UNdir(ii);
                    go.num=0;
                                        for (int j = 0; j < now.num; ++j)
                    {
                        int xxx=now.x[j],yyy=now.y[j];
                        if (!(xxx==xx&&yyy==yy))
                        {
                            go.x[go.num]=xxx;
                            go.y[go.num++]=yyy;
                        }
                    }
                    q.push(go); 
                    break;
                }
                xx+=dx[ii];
                yy+=dy[ii];
            }
        }
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5952
final_rank: 8

code_snippet: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

string ba;

struct P {
  string s;
  int x, y;
  P(const string &s, int y, int x) : s(s),y(y),x(x) {}
};

int main() {
  int h,w;
  while(cin >> h >> w) {
    ba = "";
    REP(i,h) {
      string s; cin >> s;
      ba += s;
    }
    const string strdir = "URDL";
    map<char, int> mp;
    REP(i,4) mp[strdir[i]] = i;
    const int dx[] = {0,1,0,-1};
    const int dy[] = {-1,0,1,0};
    queue<P> Q;
    REP(i,h) REP(j,w) 
      REP(k,4) if (ba[i*w+j] == strdir[k]) Q.push(P(ba,i,j));
    map<string, pair<string, int> > pre;
    pre[ba] = make_pair("",0);
    string last;
    while(!Q.empty()) {
      P p = Q.front(); Q.pop();

      bool f = 0;
      REP(i,w*h) if (p.s[i] == 'o') f = 1;
      if (!f) {
        last = p.s;
        break;
      }

      // REP(i,h) {
      //   REP(j,w) cout << p.s[i*w+j];cout << endl;
      // }
      
      int y=p.y, x = p.x;
      int sd = mp[p.s[y*w+x]];
      //p.s[y*w+x] = '.';
      for (int dd=sd-1; dd<=sd+1; ++dd) {
        int d = (dd+4)%4;
        int yy=y, xx=x;
        while(1) {
          yy+=dy[d];
          xx+=dx[d];
          if (yy<0||yy>=h||xx<0||xx>=w) break;
          if (p.s[yy*w+xx] == 'o') {
            string next = p.s;
            next[y*w+x] = '.';
            next[yy*w+xx] = strdir[d];
            if (pre.count(next) == 0) {
              pre[next] = make_pair(p.s,d);
              Q.push(P(next,yy,xx));
            }
            break;
          }
        }
      }
    }
    vector<char> ans;
    for(string now=last; now!=""; now=pre[now].first) {
      //REP(i,h){REP(j,w)cout<<now[i*w+j];cout<<endl;}
      ans.push_back(strdir[pre[now].second]);
    }
    ans.pop_back();
    reverse(ALL(ans));
    FOR(it,ans) cout << *it; cout << endl;
  }
  
}
accuracy: 1
time_ms: 100
memory_kb: 8264
final_rank: 18

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int H, W;
int grid[11][11];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
string state = "UDLR";
int lim[4] = {1,0,3,2};
bool used[11][11];
string dfs(int x, int y, int s, int rem, string res){
  //printf("dfs:%d %d %d %d %s\n", x, y, s, rem, res.c_str());
  if(rem == 0){
    return res;
  }
  REP(r, 4)if(r != lim[s]){
    REP(p, 10){
      int nx = x + p * dx[r];
      int ny = y + p * dy[r];
      if(0 <= nx && 0 <= ny && nx < W && ny < H && !used[ny][nx] && grid[ny][nx]){
        used[ny][nx] = true;
        //printf("call %d %d %d %d %s\n", nx, ny, r, rem -1, (res+state[r]).c_str());
        string response = dfs(nx, ny, r, rem-1, res+state[r]);
        used[ny][nx] = false;
        if(response != "") return response;
        break;
      }
    }
  }
  return "";
}

int main(){
  while(cin>>H>>W && H){
    int nx, ny, s;
    int cnt = 0;
    REP(y, H){
      string t; cin>>t;
      REP(x, W){
        if(t[x] == 'o'){
          grid[y][x] = 1;
          cnt += 1;
        }else if(t[x] == '.'){
          grid[y][x] = 0;
        }else{
          grid[y][x] = 0;
          nx = x;
          ny = y;
          REP(i, 4)if(state[i] == t[x])s = i;
        }
      }
    }
    memset(used, 0, sizeof(used));
    string ans = dfs(nx, ny, s, cnt, "");
    cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1

code_snippet: #include <iostream>
#include <string>
#include <cstring>
using namespace std;

int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
char dir[] = {'U', 'L', 'D', 'R'};

int W,H;
string field[10];

string solve(int x, int y, int d, int c, string str)
{
	if(c == 0)	return str;
	

	string res;
	for(int i=-1; i<2; i++) {
		bool ok = false;
		int nx=x, ny=y, nd=(d+i+4)%4;
		while(1) {
			nx += dx[nd]; ny+=dy[nd];
			if(nx<0||ny<0||nx>=W||ny>=H) break;
			if(field[ny][nx]=='o') {
				ok=true;
				break;
			}
		}

		if(ok) {
			field[ny][nx] = '.';
			res = max(res, solve(nx,ny,nd,c-1,str+dir[nd]));
			field[ny][nx] = 'o';
		}

	}

	return res;
}

int main()
{
	cin >> H >> W;
	for(int i=0; i<H; i++)
		cin >> field[i];

	int sx,sy,sd,cnt=0;
	for(int i=0; i<H; i++)
	for(int j=0; j<W; j++)
	{
		if(field[i][j] == 'o') cnt++;

		if(field[i][j]=='U') sd = 0;
		if(field[i][j]=='L') sd = 1;
		if(field[i][j]=='D') sd = 2;
		if(field[i][j]=='R') sd = 3;

		if(isupper(field[i][j])) {
			sx=j; sy=i;
		}
	}

	cout << solve(sx,sy,sd,cnt,"") << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1

code_snippet: #include <iostream>
#include <string>
using namespace std;
int h,w;
char field[11][11];
int sang;
int sx,sy;
int leaf;
char rec[1001];
bool fin;
const string ss="UUULLDDRRR";

void dfs(int cx,int cy,int cang,int cnt){
	if(fin)return;
	if(leaf==cnt){
		fin=true;
		for(int i=0;i<leaf;i++)cout<<rec[i];
		cout<<endl;
		return;
	}
	string s;
	for(int i=0;i<cnt;i++)s+=rec[i];
	if(ss==s){
	//	cout<<endl;
	}
	if(cang!=2){
		for(int i=-1;i+cy>=0;i--){
			int nx=cx;
			int ny=i+cy;
			if(field[ny][nx]=='o'){
				field[ny][nx]='.';
				rec[cnt]='U';
				dfs(nx,ny,0,cnt+1);
				field[ny][nx]='o';
				break;
			}
		}
	}
	if(cang!=0){
		for(int i=1;i+cy<h;i++){
			int nx=cx;
			int ny=i+cy;
			if(field[ny][nx]=='o'){
				field[ny][nx]='.';
				rec[cnt]='D';
				dfs(nx,ny,2,cnt+1);
				field[ny][nx]='o';
				break;
			}
		}
	}
	if(cang!=3){
		for(int i=1;i+cx<w;i++){
			int nx=cx+i;
			int ny=cy;
			if(field[ny][nx]=='o'){
				field[ny][nx]='.';
				rec[cnt]='R';
				dfs(nx,ny,1,cnt+1);
				field[ny][nx]='o';
				break;
			}
		}
	}
	if(cang!=1){
		for(int i=-1;i+cx>=0;i--){
			int nx=cx+i;
			int ny=cy;
			if(field[ny][nx]=='o'){
				field[ny][nx]='.';
				rec[cnt]='L';
				dfs(nx,ny,3,cnt+1);
				field[ny][nx]='o';
				break;
			}
		}
	}
}

int main(){

	cin>>h>>w;
	fin=false;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>field[i][j];
			if(field[i][j]=='o')leaf++;
			else if(field[i][j]!='.'&&field[i][j]!='o'){
				switch(field[i][j]){
				case 'U':
					sang=0;
					break;
				case 'D':
					sang=2;
					break;
				case 'R':
					sang=1;
					break;
				case 'L':
					sang=3;
					break;
				}
				field[i][j]='.';
				sx=j;sy=i;
			}
		}
	}
	dfs(sx,sy,sang,0);

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 0
final_rank: 4