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++: #define _USE_MATH_DEFINES
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

char tate[4][5];
char yoko[5][4];
bool flag = false;

void test(int x, int y, int j){
	//cout<<x<<" "<<y<<" "<<endl;
	int i;
	//cin>>i;
	if(x==0&&y==0){
		return;
	}

	if(j==0){                                  //右に動いた時
		if(x-1>=0 && tate[x-1][y]=='1'){       //行き止まり　上に
			cout<<"U";
			test(x-1, y, 1);
		}
		else if(y+1<5 && yoko[x][y]=='1'){     //右に行ける
			cout<<"R";
			test(x, y+1, 0);                   
		}
		else if(x+1<5 && tate[x][y]=='1'){     //行き止まり　下に
			cout<<"D";
			test(x+1, y, 3);    //下に
		}
	}
	else if(j==1){                             //上に動いた時
		if(y-1>=0 && yoko[x][y-1]=='1'){       //行き止まり　左に
			cout <<"L";
			test(x, y-1, 2);
		}
		else if(x-1>=0 && tate[x-1][y]=='1'){  //上に行ける
			cout <<"U";
			test(x-1, y, 1);
		}
		else if(y+1<5 && yoko[x][y]=='1'){     //行き止まり　右に
			cout <<"R";
			test(x, y+1, 0);
		}
	}
	else if(j==2){                             //左に動いた時
		if(x+1<5 && tate[x][y]=='1'){          //行き止まり　下に
			cout<<"D";
			test(x+1, y, 3);
		}
		else if(y-1>=0 && yoko[x][y-1]=='1'){  //左に行けるなら
			cout<<"L";
			test(x, y-1, 2);
		}
		else if(x-1>=0 && tate[x-1][y]=='1'){  //行き止まり　上に
			cout<<"U";
			test(x-1, y, 1);
		}
	}
	else if(j==3){                             //下に動いた時
		if(y+1<5 && yoko[x][y]=='1'){          //行き止まり　右に
			cout <<"R";
			test(x, y+1, 0);
		}
		else if(x+1<5 && tate[x][y]=='1'){     //下に行けるなら
			cout <<"D";
			test(x+1, y, 3);
		}
		else if(y-1>=0 && yoko[x][y-1]=='1'){  //行き止まり　左に
			cout <<"L";
			test(x, y-1, 2);
		}
	}
}

int main(){

	for(int i=0;i<4;i++){
		cin >> yoko[i][0] >> yoko[i][1] >> yoko[i][2] >> yoko[i][3];
		cin >> tate[i][0] >> tate[i][1] >> tate[i][2] >> tate[i][3] >> tate[i][4];
	}
	cin >> yoko[4][0] >> yoko[4][1] >> yoko[4][2] >> yoko[4][3];
	
	cout << "R";
	test(0, 1, 0);
	
	cout<<endl;
	
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <string>
using namespace std;
#define rep2(x,from,to) for(int x = (from); x < (to); ++(x))
#define rep(x,to) rep2(x,0,to) 
int r[7][6];
int c[6][7];

void turn() {
	int nr[7][6], nc[6][7];
	rep(i,6) {
		rep(j,7) {
			nc[i][j] = r[6-j][i];
		}
	}
	rep(i,7) {
		rep(j,6) {
			nr[i][j] = c[5-j][i];
		}
	}
	rep(i,7) {
		rep(j,6) {
			r[i][j] = nr[i][j];
		}
	}
	rep(i,6) {
		rep(j,7) {
			c[i][j] = nc[i][j];
		}
	}
}

int main() {
	rep(i,7) {
		rep(j,6) {
			r[i][j] = 0;
		}
	}
	rep(i,6) {
		rep(j,7) {
			c[i][j] = 0;
		}
	}
	string s;
	rep2(i,1,5) {
		cin >> s;
		rep2(j,1,5) {
			
			r[i][j] = s[j-1] - '0';
		}
		cin >> s;
		rep2(j,1,6) {
			c[i][j] = s[j-1] - '0';
		}
	}
	cin >> s;
	rep2(i,1,5) {
		r[5][i] = s[i-1] - '0';
	}

	int x = 0, y = 0;
	int gx = 0, gy = 0;
	char d[4] = {'R', 'U', 'L', 'D'};
	int dir = 0;
	while(1) {		
		if(!c[y][x+1] && !c[y+1][x+1] && !r[y+1][x+1]) {
			// turn 180
			dir = (dir+2) % 4;
			cout << d[dir];
			rep(i,2) turn();
			x = 4 - x;
			y = 4 - y;
			gx = 4 - gx;
			gy = 4 - gy;
			x++;
			if(x == gx && y == gy) break;
			continue;
		}
		if(c[y][x+1] == 1) {
			// turn left
			dir = (dir+1) % 4;
			cout << d[dir];
			turn();
			swap(y = 4 - y, x);
			swap(gy = 4 - gy, gx);
			x++;
			if(x == gx && y == gy) break;

			continue;
		} 
		if(r[y+1][x+1] == 1) {
			// go straight
			cout << d[dir];
			x++;
			if(x == gx && y == gy) break;
			continue;
		}
		if(c[y+1][x+1] == 1) {
			// turn right
			dir = (dir+3) % 4;
			cout << d[dir];
			rep(i,3) turn();
			swap(x = 4 - x, y);
			swap(gx = 4 - gx, gy);
			x++;
			if(x == gx && y == gy) break;

		}
	}
	cout << endl;
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

char d[10][6];

void solve(int y,int x,int way){
  if(y==0&&x==0)return;
  if(way==1){
    cout<<"R";
    if(y>0&&d[y-1][x+1]=='1')solve(y-1,x+1,2);
    else if(x<4&&d[y][x+1]=='1')solve(y,x+1,1);
    else if(y<8&&d[y+1][x+1]=='1')solve(y+1,x+1,4);
    else if(x>0&&d[y][x-1]=='1')solve(y,x-1,3);
  }
  else if(way==2){
    cout<<"U";
    if(x>0&&d[y-1][x-1]=='1')solve(y-1,x-1,3);
    else if(y>1&&d[y-2][x]=='1')solve(y-2,x,2);
    else if(x<4&&d[y-1][x]=='1')solve(y-1,x,1);
    else if(y<7&&d[y+2][x]=='1')solve(y+2,x,4);
  }
  else if(way==3){
    cout<<"L";
    if(y<8&&d[y+1][x]=='1')solve(y+1,x,4);
    else if(x>0&&d[y][x-1]=='1')solve(y,x-1,3);
    else if(y>0&&d[y-1][x]=='1')solve(y-1,x,2);
    else if(x<4&&d[y][x+1]=='1')solve(y,x+1,1);
  }
  else if(way==4){
    cout<<"D";
    if(x<4&&d[y+1][x]=='1')solve(y+1,x,1);
    else if(y<7&&d[y+2][x]=='1')solve(y+2,x,4);
    else if(x>0&&d[y+1][x-1]=='1')solve(y+1,x-1,3);
    else if(y>1&&d[y-2][x]=='1')solve(y-2,x,2);
  }
}

int main(void){
  for(int i=0;i<9;i++){
    cin>>d[i];
  }
  cout<<"R";
  solve(0,1,1);
  cout<<endl;
  return 0;
}