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;

void disp_room (vector <vector <int> > room )
{
	int size = room.size();
	int len  = room[0].size();
	int i, j;
	
	for (i = 0; i < size; ++i){
		for (j = 0; j < len; ++j){
			cout << setw(2) << room[i][j] << ' ';
		} // end for
		cout << endl;
	} // end for
	cout << endl;
}
#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 <int> > room((size+1)/2+1, vector <int> (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
	
//	disp_room (room );

	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

//	disp_room (room );

	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) );
//		cout << '(' << row << ',' << col << ')';
//		cout << " dir: " << dir;
//		cout << " res; " << res << endl; //res[res.length()-1] << endl;
		if (row == 1 && col == 0){
			++reach;
		} // end if
//	} while(row != 0 || col != 0);// end while
	} while (reach < 3);

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

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

string str[3000];
int len;
int fie[3001][3001];
int m;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
char s[4]={'R','D','L','U'};
int main(void){
	int n=0;
	while(cin >> str[n])n++;
	m=str[0].size();
	for(int i=0;i<n;i++){
		for(int j=0;j<str[i].size();j++){
			if(str[i][j]=='1'){
				if(i%2==0)fie[j*2+2][i+1]=1;
				if(i%2==1)fie[j*2+1][i+1]=1;
			}
		}
	}
	int x=2,y=0,dir=0;
	while(1){
		int nx=x+dx[(dir+1)%4],ny=y+dy[(dir+1)%4];
		if(nx>=0 && nx<=m*2+2 && ny>=0 && ny<=n+1){
			if(fie[nx][ny]==1)printf("%c",s[dir]);
		}
		nx=x+dx[dir],ny=y+dy[dir];
		if(nx>=0 && nx<=m*2+2 && ny>=0 && ny<=n+1){
			if(fie[nx][ny]==0){
				x+=dx[dir]*2;
				y+=dy[dir]*2;
				if(fie[x+dx[(dir+1)%4]][y+dy[(dir+1)%4]]==0){
					dir=(dir+1)%4;
				}
			}else{
				if(fie[x+dx[(dir+3)%4]][y+dy[(dir+3)%4]]==0){
					dir=(dir+3)%4;
					x+=dx[dir]*2;
					y+=dy[dir]*2;
					printf("%c",s[dir]);
				}else{
					dir=(dir+2)%4;
					x+=dx[dir]*2;
					y+=dy[dir]*2;
					printf("%c",s[(dir+1)%4]);
					printf("%c",s[dir]);
				}
			}
		}
		//printf("%d %d %d %d %d\n",x,y,dir,dx[dir],dy[dir]);
		if(x==0 && y==2)break;
	}
	printf("\n");
	return 0;
}

C++: #include<stdio.h>//////////AOJ0037
                               int
i,j,w[5][5];char*p,b[1<<7];;   int
F(int(v)){scanf("%s",b);for(   p=b
;*p;++p                 ){w[   p-b
+1-v][j   +v]|=((w[p-   b][j   ]|=
(*p==49   )<<3-v)&8>>   v)>>   2;}
return(   j+=v          );/*   */}
main(){   for(   ;4-F   (0);   F(1
));for(   p=b,   i=j=   (*b=   3)^
3;i+=""   "BA"          "BC"   [*b
]-66,j=   (*b)["ABCB"]-66+j,   *++
p="ULDR   "[*b],i+j;)for((*    b+=
1)&=~4;                        !(w
[i][j]&(1<<*b));(*b+=3)&=3);return
!puts(b+1);}/*May2018,tw:@siikya*/