Problem:


格子上の経路

上から見ると図 1 のような形の格子状の広場があります。この格子の各辺に「壁」があるかないかを 0 と 1 の並びで表します。点 A に立って壁に右手をつき、壁に右手をついたまま、矢印の方向に歩き続けて再び点 A に戻ってくるまでの経路を出力するプログラムを作成してください。

図1

Input

入力は９行からなり、以下の図 2 に示すように、壁がある場合を 1、ない場合を 0 として、以下の形式で与えられます。

1 行目は一番上の横線の壁の有無を左から 0 と 1 で表した文字列

2 行目はその下の縦線の壁の有無を左から0 と1 で表した文字列

3 行目は上から2 本目の横線の壁の有無を左から0 と1 で表した文字列

...

9 行目は一番下の横線の壁の有無を左から0 と1 で表した文字列

図2 (壁あるところを太線で表しています)　　(対応する数字のならび)

ただし、図１の太線に示すように、点 A から右に 1 区画分は必ず壁があるものとします。すなわち、1 行目の 1 文字目は常に 1 です。

Output

「図の左方向に一区画分進む」を 'L'、「図の右方向に一区画分進む」を 'R'、「図の上方向に一区画分進む」を 'U'、「図の下方向に一区画分進む」を 'D' で表わし、進む順に 'L', 'R', 'U', 'D' を１行に並べて出力します。

Sample Input

1111
00001
0110
01011
0010
01111
0010
01001
0111

Output for the Sample Input

RRRRDDDDLLLUUURRDDLURULLDDDRRRUUUULLLL



Sample input:
1111
00001
0110
01011
0010
01111
0010
01001
0111


Sample output:
RRRRDDDDLLLUUURRDDLURULLDDDRRRUUUULLLL

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def make_destination(wx, wy):
    data = [[] for _ in range(5)]

    x1 = wx[0]
    y1 = wy[0]
    append = data[0].append
    append(('R' if x1[0] == '1' else '.') +
           ('D' if y1[0] == '1' else '.') + '..')
    for i in range(3):
        append(('R' if x1[i+1] == '1' else '.') +
               ('D' if y1[i+1] == '1' else '.') +
               ('L' if x1[i] == '1' else '.') + '.')
    append('.' + ('D' if y1[4] == '1' else '.') +
           ('L' if x1[3] == '1' else '.') + '.')

    for j in range(1, 4):
        y0 = y1
        x1 = wx[j]
        y1 = wy[j]
        append = data[j].append
        append(('R' if x1[0] == '1' else '.') +
               ('D' if y1[0] == '1' else '.') + '.' +
               ('U' if y0[0] == '1' else '.'))
        for i in range(3):
            append(('R' if x1[i+1] == '1' else '.') +
                   ('D' if y1[i+1] == '1' else '.') +
                   ('L' if x1[i] == '1' else '.') +
                   ('U' if y0[i+1] == '1' else '.'))
        append('.' + ('D' if y1[4] == '1' else '.') +
               ('L' if x1[3] == '1' else '.') +
               ('U' if y0[4] == '1' else '.'))

    y0 = y1
    x1 = wx[4]
    append = data[4].append
    append(('R' if x1[0] == '1' else '.') + '..' +
           ('U' if y0[0] == '1' else '.'))
    for i in range(3):
        append(('R' if x1[i+1] == '1' else '.') + '.' +
               ('L' if x1[i] == '1' else '.') +
               ('U' if y0[i+1] == '1' else '.'))
    append('..' + ('L' if x1[3] == '1' else '.') +
           ('U' if y0[4] == '1' else '.'))

    return data


def walk(data):
    destination = {'R': (1, 0), 'D': (0, 1), 'L': (-1, 0), 'U': (0, -1)}
    permutation = {'R': (3, 0, 1, 2), 'D': (0, 1, 2, 3),
                   'L': (1, 2, 3, 0), 'U': (2, 3, 0, 1)}

    print('R', end='')

    x = 1
    y = 0
    move = 'R'

    while x != 0 or y != 0:
        ways = data[y][x]
        for per in permutation[move]:
            m = ways[per]
            dest = destination.get(m)
            if dest is not None:
                break
        x += dest[0]
        y += dest[1]
        move = m
        print(move, end='')
    print()


def main():
    wall = stdin.read().split()
    walk(make_destination(wall[::2], wall[1::2]))
    exit(0)


main()

Submission status: Accepted

Solution code:
wx=[]
wy=[]

def readdata():
    it=0
    x=[]
    y=[]
    while True:
        try: tmp = map(int,raw_input())
        except:
            return x,y
        if it%2==0:
            x.append(tmp)
        else:
            y.append(tmp)
        it +=1

def nextpath(p, dir0):
    tmp = ispath(p, (dir0+3)%4)
    if tmp!=[]: return tmp
    tmp = ispath(p, dir0)
    if tmp!=[]: return tmp
    tmp = ispath(p, (dir0+1)%4)
    if tmp!=[]: return tmp
    tmp = ispath(p, (dir0+2)%4)
    if tmp!=[]: return tmp
    return tmp

def ispath(p,dir1):
    y,x = p
    if dir1==0:
        if y>0 and wy[y-1][x]==1: return [-1,0]
        else: return []
    elif dir1==1:
        if x<len(wx[0]) and wx[y][x]==1: return [0,1]
        else: return []
    elif dir1==2:
        if y<len(wy[0])-1 and wy[y][x]==1: return [1,0]
        else: return []
    elif dir1==3:
        if x>0 and wx[y][x-1]==1: return [0,-1]
        else: return []
    return []

def f0037():
    p =[0,0]
    direction = 1
    msg = []
    while True:
        dp = nextpath(p, direction)
        p[0] += dp[0]
        p[1] += dp[1]
        if dp == [0,1]:
            direction = 1
            msg.append("R")
        elif dp == [0,-1]:
            direction = 3
            msg.append("L")
        elif dp == [1,0]:
            direction = 2
            msg.append("D")
        elif dp == [-1,0]:
            direction = 0
            msg.append("U")
        if p==[0,0]: break            
    return msg

wx, wy = readdata()
msg = f0037()    
print "".join(msg)

Submission status: Accepted

Solution code:
wx=[]
wy=[]

def readdata():
    z=[]
    try:
        while True:
            z.append(raw_input())
    except: return z

def nextpath(p, dir0):
    for e in [3,0,1,2]:
        dp = ispath(p, (dir0+e)%4)
        if dp!=[]: break
    return dp

def ispath(p, dir1):
    y,x = p
    dp = []
    if   dir1==0 and y>0            and wy[y-1][x]=="1": dp= 0
    elif dir1==1 and x<len(wx[0])   and wx[y][x]  =="1": dp = 1
    elif dir1==2 and y<len(wy[0])-1 and wy[y][x]  =="1": dp = 2
    elif dir1==3 and x>0            and wx[y][x-1]=="1": dp = 3
    return dp

def f0037():
    p =[0,0]
    dp = 1
    msg = []
    while True:
        dp = nextpath(p, dp)
        p[0]+=[-1,0,1,0][dp]
        p[1]+=[0,1,0,-1][dp]
        msg.append("URDL"[dp])
        if p==[0,0]: break 
    return msg

z = readdata()
wx = z[0::2]
wy = z[1::2]
msg = f0037()    
print "".join(msg)

Submission status: Accepted

Solution code:
wx=[]
wy=[]

def readdata():
    z=[]
    try:
        while True:
            z.append(raw_input())
    except: return z

def nextpath(p,dir0):
    for e in [3,0,1,2]:
        d = (dir0+e)%4
        if ispath(p,d): return d
    return None

def ispath(p,d):
    y,x = p
    f=""
    try:
        if   d==0 and y>0: f = wy[y-1][x]
        elif d==1 and x<len(wy): f = wx[y][x]
        elif d==2 and y<len(wx): f = wy[y][x]
        elif d==3 and x>0: f = wx[y][x-1]
    except: pass
    return f=="1"

def f0037():
    p =[0,0]
    dp = 1
    msg = []
    while True:
        dp = nextpath(p, dp)
        p[0] += [-1,0,1,0][dp]
        p[1] += [0,1,0,-1][dp]
        msg.append("URDL"[dp])
        if p==[0,0]: break
    return "".join(msg)

z = readdata()
wx = z[0::2]
wy = z[1::2]
print f0037()

Submission status: Accepted

Solution code:
import sys
wx=[]
wy=[]

def readdata():
    global wx,wy
    i=1
    A="0"
    wx=[""]
    wy=[""]
    for s in sys.stdin:
        s=s[:-1]
        if i: wx.append(A+s+A)
        else: wy.append(A+s)
        i=1-i
    wx[0]=A*len(wx[1])
    z=A*len(wy[1])
    wy[0]=z
    wy.append(z)
    return

readdata()
y = 1
x = 1
d = 1
msg = ""
while 1:
    A=[3,0,1,2,3,0,1][d:d+4]
    wall=[wy[y-1][x],wx[y][x],wy[y][x],wx[y][x-1]]
    for e in A:
        if wall[e]=="1": break
    d = e
    y += [-1,0,1,0][d]
    x += [0,1,0,-1][d]
    msg += "URDL"[d]
    if [x,y]==[1,1]: break
print msg

Submission status: Accepted

Solution code:
import sys
wx=[]
wy=[]
i=1
A="0"
wx=[A]
wy=[A]
for s in sys.stdin:
    s=s[:-1]
    if i: wx.append(A+s+A)
    else: wy.append(A+s)
    i=1-i
wx[0]=A*len(wx[1])
z=A*len(wy[1])
wy[0]=z
wy.append(z)

y = 1
x = 1
d = 1
msg = ""
while 1:
    A=[3,0,1,2,3,0,1][d:d+4]
    wall=[wy[y-1][x],wx[y][x],wy[y][x],wx[y][x-1]]
    for e in A:
        if wall[e]=="1": break
    d = e
    y += [-1,0,1,0][d]
    x += [0,1,0,-1][d]
    msg += "URDL"[d]
    if [x,y]==[1,1]: break
print msg

Submission status: Accepted

Solution code:
def wall(grid,x,y,d):
	#(0,1,2,3) => (R,D,L,U)
    if d == 0: return x < 4 and grid[y*2][x] == '1'
    if d == 1: return y < 4 and grid[y*2+1][x] == '1'
    if d == 2: return x > 0 and grid[y*2][x-1] == '1'
    if d == 3: return y > 0 and grid[y*2-1][x] == '1'
 
def move(x,y,d):
    if d == 0: return x+1,y,'R'
    if d == 1: return x,y+1,'D'
    if d == 2: return x-1,y,'L'
    if d == 3: return x,y-1,'U'
 
ans = ""
x=y=d=0
grid=[raw_input() for i in range(9)]
while x or y or d < 2:
    d = (d+3)%4
    while not wall(grid,x,y,d):
        d = (d+1)%4
    x,y,c = move(x,y,d)
    ans += c
print ans

Submission status: Accepted

Solution code:
dx, dy, s = [1, 0, -1, 0], [0, 1, 0, -1], 'RDLU'


def f(x, y, d):
    x -= int(d == 2)
    y = y * 2 + dy[d]
    if not(0 <= y < len(a) and 0 <= x < len(a[y])):
        return False
    return a[y][x] == '1'

a = [raw_input() for i in range(9)]
x = y = d = 0
t = ''
while True:
    x += dx[d]
    y += dy[d]
    t += s[d]
    if x == 0 and y == 0:
        break
    for i in [(d + j + 3) % 4 for j in range(3)]:
        if f(x, y, i):
            d = i
            break
    else:
        d = (d + 2) % 4
print t

Submission status: Accepted

Solution code:
mat = [['0', '0', '0', '0', '0']]
for i in range(9):
    if i%2 ==0:
        tmpc = '0' + input() + '0'

    else:
        tmpc = input()

    mat.append(list(tmpc))
mat.append(['0', '0', '0', '0', '0'])

pos = [0,1]
move = ['R']
while True:
    mdict = {'U': [2*pos[0], pos[1]], 'R': [2*pos[0]+1, pos[1]+1], 'D': [2*pos[0]+2, pos[1]], 'L': [2*pos[0]+1, pos[1]]}
    if move[-1] == 'R':
        belst = ['U', 'R', 'D', 'L']

    elif move[-1] == 'D':
        belst = ['R', 'D', 'L', 'U']

    elif move[-1] == 'L':
        belst = ['D', 'L', 'U', 'R']

    else:
        belst = ['L', 'U', 'R', 'D']
            
    for i in belst:
        if mat[mdict[i][0]][mdict[i][1]] == '1':
            be = i
            break
    
    if be == 'U':
        pos[0] -= 1
    elif be == 'R':
        pos[1] += 1
    elif be == 'D':
        pos[0] += 1
    else:
        pos[1] -= 1
    
    move.append(be)

    if pos == [0, 0]:
        break

print(''.join(move))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
write = sys.stdout.write
def solve():
    N = 5
    MP = [[[0]*4 for i in range(N)] for j in range(N)]
    dd = ((-1, 0), (0, -1), (1, 0), (0, 1))
    ds = "LURD"
    for i in range(N):
        s = readline().strip()
        for j in range(N-1):
            if s[j] == '1':
                MP[i][j][2] = MP[i][j+1][0] = 1
        if i < 4:
            s = readline().strip()
            for j in range(N):
                if s[j] == '1':
                    MP[i][j][3] = MP[i+1][j][1] = 1
    x = 1; y = 0; cd = 2
    de = [3, 0, 1, 2]
    ans = [2]
    while not x == y == 0:
        e = MP[y][x]
        for d in de:
            if e[cd + d - 4]:
                cd = (cd + d) % 4
                ans.append(cd)
                dx, dy = dd[cd]
                x += dx; y += dy
                break
    write("".join(map(ds.__getitem__, ans)))
    write("\n")
solve()

Submission status: Accepted

Solution code:
table = [input() for i in range(9)]
def isWall(x,y,dir):
	# 右
    if dir == 0:
        return x < 4 and table[y][x] == '1'
    # 下
    elif dir == 1:
        return y < 8 and table[y + 1][x] == '1'
	# 左
    elif dir == 2:
        return x > 0 and table[y][x - 1] == '1'
	# 上
    elif dir == 3:
        return y > 0 and table[y - 1][x] == '1'
    
ans = ""
x = y = dir = 0

while 1:
    # 正面の壁
    dir = (dir + 3) % 4
    while not isWall(x,y,dir):
        # 伝う壁
        dir = (dir + 1) % 4
    # 移動
    if dir == 0:
        x += 1
        ans += 'R'
    elif dir == 1:
        y += 2
        ans += 'D'
    elif dir == 2:
        x -= 1
        ans += 'L'
    elif dir == 3:
        y -= 2
        ans += 'U'
    if x == 0 and y == 0:
        break
print(ans)

Submission status: Accepted

Solution code:
path = [[[] for i in range(5)] for i in range(5)]
for i in range(9):
    if i%2 == 0:
        a = [int(i) for i in list(input())]
        for j in range(4):
            if a[j] == 1:
                path[int(i/2)][j].append("R")
                path[int(i/2)][j+1].append("L")
    else:
        a = [int(i) for i in list(input())]
        for j in range(5):
            if a[j] == 1:
                path[int(i/2)][j].append("D")
                path[int(i/2)+1][j].append("U")

prev = "R"
p = "R"
x = 0
y = 0
while(1):
    if prev == "R":
        if "U" in path[x][y]:
            x -= 1
            prev = "U"
            p += "U"
        elif "R" in path[x][y]:
            y += 1
            prev = "R"
            p += "R"
        elif "D" in path[x][y]:
            x += 1
            prev = "D"
            p += "D"
        elif "L" in path[x][y]:
            y -= 1
            prev = "L"
            p += "L"
    elif prev == "L":
        if "D" in path[x][y]:
            x += 1
            prev = "D"
            p += "D"
        elif "L" in path[x][y]:
            y -= 1
            prev = "L"
            p += "L"
        elif "U" in path[x][y]:
            x -= 1
            prev = "U"
            p += "U"
        elif "R" in path[x][y]:
            y += 1
            prev = "R"
            p += "R"
    elif prev == "U":
        if "L" in path[x][y]:
            y -= 1
            prev = "L"
            p += "L"
        elif "U" in path[x][y]:
            x -= 1
            prev = "U"
            p += "U"
        elif "R" in path[x][y]:
            y += 1
            prev = "R"
            p += "R"
        elif "D" in path[x][y]:
            x += 1
            prev = "D"
            p += "D"
    elif prev == "D":
        if "R" in path[x][y]:
            y += 1
            prev = "R"
            p += "R"
        elif "D" in path[x][y]:
            x += 1
            prev = "D"
            p += "D"
        elif "L" in path[x][y]:
            y -= 1
            prev = "L"
            p += "L"
        elif "U" in path[x][y]:
            x -= 1
            prev = "U"
            p += "U"
    if x == 0 and y == 0:
        break
print(p[1:])

Submission status: Accepted

Solution code:
def solve():
    pipes = [[0 for _ in range(6)]]
    pipes.extend([list(map(int, '0{0}0'.format(input()))) for _ in range(9)])
    pipes.extend([[0 for _ in range(6)]])
    last_dirction = 2
    x, y = 1, 1
    while True:
        next_direction = (last_dirction - 1) % 4
        while True:
            if next_direction == 0:
                nx, ny = x, 2*y - 2
                if pipes[ny][nx] == 1:
                    print('U', end='')
                    x, y = x, y - 1
                    break
            elif next_direction == 1:
                nx, ny = x, 2*y - 1
                if pipes[ny][nx] == 1:
                    print('R', end='')
                    x, y = x + 1, y
                    break
            elif next_direction == 2:
                nx, ny = x, 2*y
                if pipes[ny][nx] == 1:
                    print('D', end='')
                    x, y = x, y + 1
                    break
            else:
                nx, ny = x - 1, 2*y - 1
                if pipes[ny][nx] == 1:
                    print('L', end='')
                    x, y = x - 1, y
                    break
            next_direction = (next_direction + 1) % 4
        last_dirction = next_direction
        if x == 1 and y == 1:
            print()
            break

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
import sys

def xxx(position, direction):
    if direction == 'R' or direction == 'L':
        if not (0 <= position[tate] <= 8 and 0 <= position[yoko] <= 3):
            return 
    elif direction == 'U' or direction == 'D':
        if not (1 <= position[tate] <= 7 and 0 <= position[yoko] <= 4):
            return
    if (position[tate] == 0 and position[yoko] == 0) and direction == 'L' or \
       (position[tate] == 1 and position[yoko] == 0) and direction == 'U':
        result.append(direction)
        print_result()
    if mapp[position[tate]][position[yoko]] == 1:
        result.append(direction)
        kyoto(position, direction)
    elif mapp[position[tate]][position[yoko]] == 0:
        return

def kyoto(position, direction):
    if direction == 'R':
        #????????????????´¢
        check_position = position[tate], position[yoko]+1
        direction = 'R'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate]-1, position[yoko]+1
        direction = 'U'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate]+1, position[yoko]+1
        direction = 'D'
        xxx(check_position, direction)
    elif direction == 'U':
        #????????????????´¢
        check_position = position[tate]-2, position[yoko]
        direction = 'U'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate]-1, position[yoko]-1
        direction = 'L'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate]-1, position[yoko]
        direction = 'R'
        xxx(check_position, direction)
    elif direction == 'L':
        #????????????????´¢
        check_position = position[tate], position[yoko]-1
        direction = 'L'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate]+1, position[yoko]
        direction = 'D'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate]-1, position[yoko]
        direction = 'U'
        xxx(check_position, direction)
    elif direction == 'D':
        #????????????????´¢
        check_position = position[tate]+2, position[yoko]
        direction = 'D'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate]+1, position[yoko]
        direction = 'R'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate]+1, position[yoko]-1
        direction = 'L'
        xxx(check_position, direction)
        
def print_result():
    print ''.join(result)
    sys.exit()

mapp = []
for i in range(9):
    mapp.append([int(char) for char in raw_input()])
    
tate = 0
yoko = 1
result = []
xxx((0,0), 'R')

Submission status: Runtime Error

Solution code:
import sys

def xxx(position, direction):
    if direction == 'R' or direction == 'L':
        if not (0 <= position[tate] <= 8 and 0 <= position[yoko] <= 3):
            return 
    elif direction == 'U' or direction == 'D':
        if not (1 <= position[tate] <= 7 and 0 <= position[yoko] <= 4):
            return
    if (position[tate] == 0 and position[yoko] == 0) and direction == 'L' or \
       (position[tate] == 1 and position[yoko] == 0) and direction == 'U':
        result.append(direction)
        print_result()
    if mapp[position[tate]][position[yoko]] == 1:
        result.append(direction)
        kyoto(position, direction)
    elif mapp[position[tate]][position[yoko]] == 0:
        return

def kyoto(position, direction):
    if direction == 'R':
        #????????????????´¢
        check_position = position[tate]-1, position[yoko]+1
        direction = 'U'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate], position[yoko]+1
        direction = 'R'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate]+1, position[yoko]+1
        direction = 'D'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate], position[yoko]
        direction = 'L'
        xxx(check_position, direction)
    elif direction == 'U':
        #????????????????´¢
        check_position = position[tate]-1, position[yoko]-1
        direction = 'L'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate]-2, position[yoko]
        direction = 'U'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate]-1, position[yoko]
        direction = 'R'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate], position[yoko]
        direction = 'D'
        xxx(check_position, direction)
    elif direction == 'L':
        #????????????????´¢
        check_position = position[tate]+1, position[yoko]
        direction = 'D'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate], position[yoko]-1
        direction = 'L'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate]-1, position[yoko]
        direction = 'U'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate], position[yoko]
        direction = 'R'
        xxx(check_position, direction)
    elif direction == 'D':
        #????????????????´¢
        check_position = position[tate]+1, position[yoko]
        direction = 'R'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate]+2, position[yoko]
        direction = 'D'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate]+1, position[yoko]-1
        direction = 'L'
        xxx(check_position, direction)
        #????????????????´¢
        check_position = position[tate], position[yoko]
        direction = 'U'
        xxx(check_position, direction)
        
def print_result():
    print ''.join(result)
    sys.exit()

mapp = []
for i in range(9):
    mapp.append([int(char) for char in raw_input()])
    
tate = 0
yoko = 1
result = []
xxx((0,0), 'R')

Submission status: Runtime Error

Solution code:
def xxx(position, direction):
    if direction == 'R' or direction == 'L':
        if not (0 <= position[tate] <= 8 and 0 <= position[yoko] <= 3):
            return 
    elif direction == 'U' or direction == 'D':
        if not (1 <= position[tate] <= 7 and 0 <= position[yoko] <= 4):
            return
    if (position[tate] == 0 and position[yoko] == 0) and direction == 'L' or \
       (position[tate] == 1 and position[yoko] == 0) and direction == 'U':
        result.append(direction)
        print ''.join(result)
        return True
    if mapp[position[tate]][position[yoko]] == 1:
        result.append(direction)
        if kyoto(position, direction): return True
    elif mapp[position[tate]][position[yoko]] == 0:
        return

def kyoto(position, direction):
    if direction == 'R':
        #????????????????´¢
        check_position = position[tate]-1, position[yoko]+1
        direction = 'U'
        if xxx(check_position, direction): return True
        #????????????????´¢
        check_position = position[tate], position[yoko]+1
        direction = 'R'
        if xxx(check_position, direction): return True
        #????????????????´¢
        check_position = position[tate]+1, position[yoko]+1
        direction = 'D'
        if xxx(check_position, direction): return True
        #????????????????´¢
        check_position = position[tate], position[yoko]
        direction = 'L'
        if xxx(check_position, direction): return True
    elif direction == 'U':
        #????????????????´¢
        check_position = position[tate]-1, position[yoko]-1
        direction = 'L'
        if xxx(check_position, direction): return True
        #????????????????´¢
        check_position = position[tate]-2, position[yoko]
        direction = 'U'
        if xxx(check_position, direction): return True
        #????????????????´¢
        check_position = position[tate]-1, position[yoko]
        direction = 'R'
        if xxx(check_position, direction): return True
        #????????????????´¢
        check_position = position[tate], position[yoko]
        direction = 'D'
        if xxx(check_position, direction): return True
    elif direction == 'L':
        #????????????????´¢
        check_position = position[tate]+1, position[yoko]
        direction = 'D'
        if xxx(check_position, direction): return True
        #????????????????´¢
        check_position = position[tate], position[yoko]-1
        direction = 'L'
        if xxx(check_position, direction): return True
        #????????????????´¢
        check_position = position[tate]-1, position[yoko]
        direction = 'U'
        if xxx(check_position, direction): return True
        #????????????????´¢
        check_position = position[tate], position[yoko]
        direction = 'R'
        if xxx(check_position, direction): return True
    elif direction == 'D':
        #????????????????´¢
        check_position = position[tate]+1, position[yoko]
        direction = 'R'
        if xxx(check_position, direction): return True
        #????????????????´¢
        check_position = position[tate]+2, position[yoko]
        direction = 'D'
        if xxx(check_position, direction): return True
        #????????????????´¢
        check_position = position[tate]+1, position[yoko]-1
        direction = 'L'
        if xxx(check_position, direction): return True
        #????????????????´¢
        check_position = position[tate], position[yoko]
        direction = 'U'
        if xxx(check_position, direction): return True

while True:
    try:      
        mapp = []
        for i in range(9):
            mapp.append([int(char) for char in raw_input()])
        tate = 0
        yoko = 1
        result = []
        xxx((0,0), 'R')
    except EOFError:
        break

Submission status: Runtime Error

Solution code:
dist = ['R','D','L','U']
dict = [(1,0),(0,1),(-1,0),(0,-1)]


result = ''
sell = [[False]*13]*2
for i in xrange(9):
    l = [False,False]
    s = raw_input()
    if len(s) == 4:
        for j in s:
            l.append(False)
            l.append(bool(int(j)))
        l += [False] * 3
    else:
        for j in s:
            l.append(bool(int(j)))
            l.append(False)
        l += [False] * 2
    sell.append(l)
sell += [[False]*13]*2

my = (2,2,0)

while(True):
    if sell[my[0]+dict[my[2]][0],my[1]+dict[my[2]][1]]:
        my = (my[0]+dict[my[2]][0]*2,my[1]+dict[my[2]][1]*2,my[2])
        if my[0] == 2 and my[1] == 2:
            break
        else:
            result += dist[my[2]]
    else:
        my = (my[0],my[1],(my[2]+1)%4)
print result

Submission status: Runtime Error

Solution code:

import sys


class Grid:
    def __init__(self, lines):
        lines = map((lambda line: list(line)), lines)
        l = []
        for i in range(len(lines)):
            if i % 2 == 0:
                l.append(intersperse2(lines[i], '+'))
            else:
                l.append(intersperse(lines[i], 'x'))
        l.insert(0, ['0','x','0','x','0','x','0','x','0'])
        l.append(['0','x','0','x','0','x','0','x','0'])
        l = map(lambda x: self._outer(x), l)
        self.grid = l

    def show(self):
        for l in self.grid:
            print l
        return self

    def _outer(self, lis):
        if lis[0] == '0' or lis[0] == '1':
            lis.insert(0, 'x')
        else:
            lis.insert(0, '0')
        if lis[-1] == '0' or lis[-1] == '1':
            lis.append('x')
        else:
            lis.append('0')
        return lis


class Walker:
    def __init__(self, grid, x, y):
        self.grid = grid
        self.x = x
        self.y = y
        self.dir = 'R'

    def walk(self):
        if self.left() == '1':
            return self.to_left()
        elif self.ahead() == '1':
            return self.to_ahead()
        elif self.right() == '1':
            return self.to_right()
        else:
            return self.to_back()

    def left(self):
        x = self.x
        y = self.y
        d = self.dir
        if d == 'R':
            return self.grid.grid[y-1][x]
        elif d == 'L':
            return self.grid.grid[y+1][x]
        elif d == 'U':
            return self.grid.grid[y][x-1]
        else:   # d == 'D'
            return self.grid.grid[y][x+1]

    def ahead(self):
        x = self.x
        y = self.y
        d = self.dir
        if d == 'R':
            return self.grid.grid[y][x+1]
        elif d == 'L':
            return self.grid.grid[y][x-1]
        elif d == 'U':
            return self.grid.grid[y-1][x]
        else:   # d == 'D'
            return self.grid.grid[y+1][x]

    def right(self):
        x = self.x
        y = self.y
        d = self.dir
        if d == 'R':
            return self.grid.grid[y+1][x]
        elif d == 'L':
            return self.grid.grid[y-1][x]
        elif d == 'U':
            return self.grid.grid[y][x+1]
        else:   # d == 'D'
            return self.grid.grid[y][x-1]

    def to_left(self):
        d = self.dir
        if d == 'R':
            self.y -= 2
            self.dir = 'U'
            return 'U'
        elif d == 'L':
            self.y += 2
            self.dir = 'D'
            return 'D'
        elif d == 'U':
            self.x -= 2
            self.dir = 'L'
            return 'L'
        else:   # d == 'D'
            self.x += 2
            self.dir = 'R'
            return 'R'

    def to_ahead(self):
        d = self.dir
        if d == 'R':
            self.x += 2
            return 'R'
        elif d == 'L':
            self.x -= 2
            return 'L'
        elif d == 'U':
            self.y -= 2
            return 'U'
        else:   # d == 'D'
            self.y += 2
            return 'D'

    def to_right(self):
        d = self.dir
        if d == 'R':
            self.y += 2
            self.dir = 'D'
            return 'D'
        elif d == 'L':
            self.y -= 2
            self.dir = 'U'
            return 'U'
        elif d == 'U':
            self.x += 2
            self.dir = 'R'
            return 'R'
        else:   # d == 'D'
            self.x -= 2
            self.dir = 'L'
            return 'L'

    def to_backt(self):
        d = self.dir
        if d == 'R':
            self.x -= 2
            self.dir = 'L'
            return 'L'
        elif d == 'L':
            self.x += 2
            self.dir = 'R'
            return 'R'
        elif d == 'U':
            self.y -= 2
            self.dir = 'D'
            return 'D'
        else:   # d == 'D'
            self.y += 2
            self.dir = 'U'
            return 'U'



def intersperse(lis, s):
    r = []
    for a in lis:
        r.append(a)
        r.append(s)
    r.pop()
    return r

def intersperse2(lis, s):
    r = []
    for a in lis:
        r.append(s)
        r.append(a)
    r.append(s)
    return r



lines = sys.stdin.readlines()
lines = map(lambda x: x.rstrip("\r\n"), lines)
grid = Grid(lines)
#grid.show()
walker = Walker(grid, 1, 1)
path = []
while True:
    path.append(walker.walk())
    if (walker.x, walker.y) == (1,1):
        break
print ''.join(path)