Go: package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
	"fmt"
)

var points [5][5][4]bool

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for i := 0; i < 9; i++ {
		y := i/2
		if i%2 == 0 {
			scanner.Scan()
			info := strings.Split(scanner.Text(), "")
			for k := range info {
				e, _ := strconv.Atoi(info[k])
				if e != 0 {
					points[y][k][1], points[y][k+1][3] = true, true
				}
			}
		} else {
			scanner.Scan()
			info := strings.Split(scanner.Text(), "")
			for k := range info {
				e, _ := strconv.Atoi(info[k])
				if e != 0 {
					points[y][k][2], points[y+1][k][0] = true, true
				}
			}
		}
	}
	x := 1
	y := 0
	dir := 1
	dx := []int{0, 1, 0, -1}
	dy := []int{-1, 0, 1, 0}
	ds := "URDL"
	fmt.Printf("%s", string(ds[dir]))
	for !(x == 0 && y == 0) {
		if points[y][x][(dir+3)%4] {
			dir = (dir+3)%4
		} else if points[y][x][dir] {

		} else if points[y][x][(dir+1)%4] {
			dir = (dir+1)%4
		} else {
			dir = (dir+2)%4
		}
		x += dx[dir]
		y += dy[dir]
		fmt.Printf("%s", string(ds[dir]))
	}
	fmt.Println()
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;

#define East  1
#define South 2
#define West  4
#define North 8

int main()
{
//	cut here before submit 
//	freopen("testcase.pg", "r", stdin);
	string str = "";
	vector <string> grid;
	int maxlen = 0;

	while (getline (cin, str ) ){
		if (str.empty() ){
			break;
		} // end if
		int len = str.length();
		maxlen = max (maxlen, len );
		grid.push_back(str );
	} // end while

	int size = grid.size();
	vector <vector <char> > room((size+1)/2+1, vector <char> (maxlen, 0 ) );
	int i,j,k;

	for (i = 0; i < size; ++i){
		for (j = 0; j < grid[i].length(); ++j){
			if (grid[i][j] == '1'){
				if (i % 2 == 0){	// north or south wall
					room[i/2][j] |= South;
				}else{			// east or west wall
					room[i/2+1][j-1] |= East;
				} // end if
			} // end if
		} // end for
	} // end for

	for (i = 0; i < room.size(); ++i){
		for (j = 0; j < room[i].size(); ++j){
			if (i == 0){
				room[i][j] |= North;
			}else if (i == room.size() - 1){
				room[i][j] |= South;
			} // end if
			if (i > 0 && room[i-1][j] & South){
				room[i][j] |= North;
			} // end if
			if (j == 0){
				room[i][j] |= West;
			}else if (j == room[i].size() - 1){
				room[i][j] |= East;
			} // end if
			if (j > 0 && room[i][j-1] & East){
				room[i][j] |= West;
			} // end if
		} // end for
	} // end for

	int row = 0;
	int col = 0;
	int dir = East;
	int right_hand = ( (dir << 1) > 8 ? 1 : (dir << 1) );
	string res = "";
	res += 'R';
	bool turn = false;
	int reach = 0;

	do{
		if (turn || (right_hand & room[row][col]) ){
			turn = false;
			// go faword
			switch (dir){
				case East:
					if (!(room[row][col] & East)){ 
						++col;// res += 'R';
						if (right_hand & room[row][col]){
							res += 'R';
						} // end if
					}else{
						dir = North;
						if (right_hand & room[row][col]){
							res += 'U';
						} // end if
						turn = true;
					} // end if
					break;
				case South:
					if (!(room[row][col] & South)){
					 	++row;// res += 'D';
						if (right_hand & room[row][col]){
							res += 'D';
						} // end if
					}else{
						dir = East;
						if (right_hand & room[row][col]){
							res += 'R';
						} // end if
						turn = true;
					} // end if
					break;
				case West:
					if (!(room[row][col] & West)){
						--col;// res += 'L';
						if (right_hand & room[row][col]){
							res += 'L';
						} // end if
					}else{
						dir = South;
						if (right_hand & room[row][col]){
							res += 'D';
						} // end if
						turn = true;
					} // end if
					break;
				case North:
					if (!(room[row][col] & North)){
						--row;// res += 'U';
						if (right_hand & room[row][col]){
							res += 'U';
						} // end if
					}else{
						dir = West;
						if (right_hand & room[row][col]){
							res += 'L';
						} // end if
						turn = true;
					} // end if  
					break;
			} // end switch
		}else{
			// turn
			switch (dir){
				case East:  dir = South; break;
				case South: dir = West; break;
				case West:  dir = North; break;
				case North: dir = East; break;
			} // end switch
			turn = true;
		} // end if

		right_hand = ( (dir << 1) > 8 ? 1 : (dir << 1) );
		if (row == 1 && col == 0){
			++reach;
		} // end if
	} while (reach < 3);

	cout << res << endl;
	
	return 0;
}

C++: #include <cstdio>

using namespace std;

#define rep(i,n) for(int i=0; i<int(n); ++i)

int main()
{
	int field[5][5] = {{0}};
	rep(i, 9){
		char str[6];
		scanf("%s", str);
		if(i%2){
			rep(j, 5){
				int a = str[j]-'0';
				field[i/2][j] |= a<<2;
				field[i/2+1][j] |= a;
			}
		}else{
			rep(j, 4){
				int a = str[j]-'0';
				field[i/2][j] |= a<<1;
				field[i/2][j+1] |= a<<3;
			}
		}
	}

	putchar('R');
	int y=0, x=1;
	int pre = 1;
	do{
		// printf("(%d, %d)\n", x, y);
		pre = (pre+2)%4;
		int dire = pre;
		do{
			dire = (dire + 1)%4;
		}while(!((field[y][x]>>dire)&1));
		pre = dire;
		putchar("URDL"[dire]);

		int dx[] = {0, 1, 0, -1};
		int dy[] = {-1, 0, 1, 0};
		x += dx[dire];
		y += dy[dire];
	}while(y!=0 || x!=0 || (field[0][0]==6 && pre==3));
	puts("");

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
//#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

string s = "URDL";
int dx[4] = { 0, 1,  0, -1};
int dy[4] = {-1, 0,  1,  0};

signed main() {
    string ans = "";
    int f[5][5][4] = {0};
    string wx[5];
    string wy[4];
    rep(i, 0, 9){
        if(i % 2 == 0){
            cin >> wx[i / 2];
        }else{
            cin >> wy[i / 2];
        }
    }
    f[0][0][0] = 1;
    int y = 0, x = 0, dir = 0;
    while(1){
        rep(i, 0, 4){
            int tmp = (dir + i) % 4;
            int ny = y + dy[tmp];
            int nx = x + dx[tmp];
            if(!(0 <= ny && ny <= 4 && 0 <= nx && nx <= 4)) continue;
            if(tmp == 0 && wy[y - 1][x] == '0') continue;
            if(tmp == 1 && wx[y][x] == '0') continue;
            if(tmp == 2 && wy[y][x] == '0') continue;
            if(tmp == 3 && wx[y][x - 1] == '0') continue;
            if(f[ny][nx][(dir + i + 3) % 4] == true) continue;
            f[ny][nx][(dir + i + 3) % 4] = true;
            y = ny;
            x = nx;
            dir = (dir + i + 3) % 4;
            ans += s[tmp];
            //o(y<<" "<<x<<" "<<dir<<" "<<ny<<" "<<nx<<" "<<tmp<<" "<<s[tmp]);
            break;
        }
        if(f[0][0][2] || f[0][0][3]) break;
    }
    cout << ans << endl;
}

C++: #include <iostream>
using namespace std;

int main(){
  int f,xyf;
  char h[5][4],v[4][5];
  int x,y;
  cin>>h[0];
  for(int i=0;i<4;i++){
    cin>>v[i];
    cin>>h[i+1];
  }
  f=xyf=1;
  y=0;
  x=1;cout<<"R";
  while(x!=0||y!=0){
    if(f==1){
      if(xyf==1){
	if(y>0&&v[y-1][x]=='1'){
	  y--;cout<<"U";f*=-1;xyf*=-1;
	}
	else if(x<4&&h[y][x]=='1'){
	  x++;cout<<"R";
	}
	else{
	  y++;f*=-1;cout<<"D";
	}
      }
      else{
	if(y>0&&v[y-1][x]=='1'){
	  y--;cout<<"U";f*=-1;
	}
	else if(x>0&&h[y][x-1]=='1'){
	  x--;cout<<"L";
	}
	else{
	  y++;cout<<"D";f*=-1;xyf*=-1;
	}
      }
    }
    else{
      if(xyf==1){
	if(x<4&&h[y][x]=='1'){
	  x++;cout<<"R";f*=-1;
	}
	else if(y<4&&v[y][x]=='1'){
	  y++;cout<<"D";
	}
	else{
	  x--;cout<<"L";f*=-1;xyf*=-1;
	}
      }
      else{
	if(x>0&&h[y][x-1]=='1'){
	  x--;cout<<"L";f*=-1;
	}
	else if(y>0&&v[y-1][x]=='1'){
	  y--;cout<<"U";
	}
	else{
	  x++;cout<<"R";f*=-1;xyf*=-1;
	}
      }
    }
  }
  cout<<endl;
  return 0;
}