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 <vector>
using namespace std;

typedef vector <string> VS;

VS R;
string D = "RDLU";
const int RIGHT = 0;
const int DOWN = 1;
const int LEFT = 2;
const int UP = 3;
int cnt[100][100];

void dfs( int dir, int x, int y, int a ) {
    // cout << dir << ", " << x << ", " << y << endl;
    cout << D[dir];
    cnt[y][x]++;
    if ( x == 0 && y == 0 && cnt[y][x] > 1 ) return;

    if ( dir == RIGHT ) {
        // 0:ue, 1:sita
        if ( a == 0 ) {
            if ( y-1 >= 0 && x+1 < R[y-1].size() && R[y-1][x+1] == '1' ) {
                return dfs( UP, x+1, y-1, 0 );
            }
            if ( x+1 < R[y].size() && R[y][x+1] == '1' ) {
                return dfs( RIGHT, x+1, y, 0 );
            }
            if ( y+1 < R.size() && x+1 < R[y+1].size() && R[y+1][x+1] == '1' ) {
                return dfs( DOWN, x+1, y+1, 1 );
            }
        } else {
            if ( y+1 < R.size() && x+1 < R[y+1].size() && R[y+1][x+1] == '1' ) {
                return dfs( DOWN, x+1, y+1, 0 );
            }
            if ( x+1 < R[y].size() && R[y][x+1] == '1' ) {
                return dfs( RIGHT, x+1, y, 1 );
            }
            if ( y-1 >= 0 && x+1 < R[y-1].size() && R[y-1][x+1] == '1' ) {
                return dfs( UP, x+1, y-1, 1 );
            }
        }
    } else if ( dir == DOWN ) {
        // 0:hidari, 1:migi
        if ( a == 0 ) {
            if ( y+1 < R.size() && x-1 >= 0 && R[y+1][x-1] == '1' ) {
                return dfs( LEFT, x-1, y+1, 0 );
            }
            if ( y+2 < R.size() && R[y+2][x] == '1' ) {
                return dfs( DOWN, x, y+2, 0 );
            }
            if ( y+1 < R.size() && x < R[y+1].size() && R[y+1][x] == '1' ) {
                return dfs( RIGHT, x, y+1, 1 );
            }
        } else {
            if ( y+1 < R.size() && x < R[y+1].size() && R[y+1][x] == '1' ) {
                return dfs( RIGHT, x, y+1, 0 );
            }
            if ( y+2 < R.size() && R[y+2][x] == '1' ) {
                return dfs( DOWN, x, y+2, 1 );
            }
            if ( y+1 < R.size() && x-1 >= 0 && R[y+1][x-1] == '1' ) {
                return dfs( LEFT, x-1, y+1, 1 );
            }
        }
    } else if ( dir == LEFT ) {
        // 0:ue, 1:sita
        if ( a == 0 ) {
            if ( y-1 >= 0 && x < R[y-1].size() && R[y-1][x] == '1' ) {
                return dfs( UP, x, y-1, 1 );
            }
            if ( x-1 >= 0 && R[y][x-1] == '1' ) {
                return dfs( LEFT, x-1, y, 0 );
            }
            if ( y+1 < R.size()&& x < R[y+1].size() && R[y+1][x] == '1' ) {
                return dfs( DOWN, x, y+1, 0 );
            }
        } else {
            if ( y+1 < R.size()&& x < R[y+1].size() && R[y+1][x] == '1' ) {
                return dfs( DOWN, x, y+1, 1 );
            }
            if ( x-1 >= 0 && R[y][x-1] == '1' ) {
                return dfs( LEFT, x-1, y, 1 );
            }
            if ( y-1 >= 0 && x < R[y-1].size() && R[y-1][x] == '1' ) {
                return dfs( UP, x, y-1, 0 );
            }
        }
    } else if ( dir == UP ) {
        // 0:hidari, 1:migi
        if ( a == 0 ) {
            if ( y-1 >= 0 && x-1 >= 0 && R[y-1][x-1] == '1' ) {
                return dfs( LEFT, x-1, y-1, 1 );
            }
            if ( y-2 >= 0 && R[y-2][x] == '1' ) {
                return dfs( UP, x, y-2, 0 );
            }
            if ( y-1 >= 0 && x < R[y-1].size() && R[y-1][x] == '1' ) {
                return dfs( RIGHT, x, y-1, 0 );
            }
        } else {
            if ( y-1 >= 0 && x < R[y-1].size() && R[y-1][x] == '1' ) {
                return dfs( RIGHT, x, y-1, 1 );
            }
            if ( y-2 >= 0 && R[y-2][x] == '1' ) {
                return dfs( UP, x, y-2, 1 );
            }
            if ( y-1 >= 0 && x-1 >= 0 && R[y-1][x-1] == '1' ) {
                return dfs( LEFT, x-1, y-1, 0 );
            }
        }
    }
}

int main() {
    for ( int i = 0; i < 100; i++ ) {
        for ( int j = 0; j < 100; j++ ) {
            cnt[i][j] = 0;
        }
    }
    R = VS( 9 );
    for ( int i = 0; i < 9; i++ ) {
        cin >> R[i];
    }
    dfs( 0, 0, 0, 0 );
    cout << endl;
    return 0;
}

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

int i, j, a[6][6] = {}, b[6][5] = {};
string output = "";

int right(), left(), up(), down();

int right(){
	output += "R";
	if(b[i-1][j] == 1){
		i--;
		up();
	}
	else if(a[i][j+1] == 1){
		j++;
		right();
	}
	else if (a[i][j + 1] == 2) { output += "R"; }
	else {
		down();
	}
	return 0;
}

int left(){
	output += "L";
	if (b[i][j-1] == 1) {
		j--;
		down();
	}
	else if (a[i][j-1] == 1) {
		j--;
		left();
	}
	else if (a[i][j - 1] == 2) { output += "L"; }
	else {
		i--;
		j--;
		up();
	}
	return 0;
}

int up(){
	output += "U";
	if (a[i][j] == 1) {
		left();
	}
	else if (a[i][j] == 2) {  }
	else if (b[i-1][j] == 1) {
		i--;
		up();
	}
	else if (a[i][j + 1] == 2) {  }
	else {
		j++;
		right();
	}
	return 0;
}

int down(){
	output += "D";
	if (a[i+1][j+1] == 1) {
		i++;
		j++;
		right();
	}
	else if (a[i + 1][j + 1] == 2) {  }
	else if (b[i + 1][j] == 1) {
		i++;
		down();
	}
	else if (a[i + 1][j] == 2) { }
	else {
		i++;
		left();
	}
	return 0;
}

int main(){
	string input, k;
	for(i = 1; i<10;i++){
		getline(cin, input);
		if (input.size() != 4) {
			for(int j= 0;j<5;j++){
				k = input[j];
				b[i / 2][j] = stoi(k);
			}
		}
		else{
			for(int j = 1;j<5;j++){
				k = input[j-1];
				a[(i + 1)/ 2][j] = stoi(k);
			}
		}

	}

	a[1][1] = 2;
	i = 1;
	j = 1;
	right();
	cout << output << endl;
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#define rep(i,n) for(int i = 0 ; i < n ; i++ )
using namespace std;
void input(bool grid[][9]){
  rep(i,9){
    string s;
    cin >> s;
    if(i%2 == 0){
      rep(j,4)grid[i][j*2+1] = s[j] - '0';
    } else {
      rep(j,5)grid[i][j*2] = s[j] - '0';
    }
  }
}

enum dir{
  UP,RIGHT,DOWN,LEFT
};
const int di[] = {-1,0,1,0};
const int dj[] = {0,1,0,-1};
const char output[] = "URDL";

bool in_range(int a,int b){
  return 0 <= a && a < 9 && 0 <= b && b < 9;
}
int main(){
  bool grid[9][9] = {{0}};
  input(grid);

  int i = 0,j = 0;
  dir prev = RIGHT;
  for(;;){
    dir next[] = {LEFT,UP,RIGHT,DOWN};
    rotate(next,next + prev,next + 4);

    int k;
    for(k = 0 ; k < 3 ; k++ ){
      if(in_range(i+di[next[k]],j+dj[next[k]]) &&
	 grid[i+di[next[k]]][j+dj[next[k]]]){
	break;
      }
    }
    cout << output[next[k]];
    i+=di[next[k]]*2;
    j+=dj[next[k]]*2;
    prev = next[k];
    if(i == 0 && j == 0)break;
  } 
  cout << endl;
  return 0;
}