#coding: UTF-8 import sys import re import itertools from collections import deque """ defs """ def searching_forward(x,y,drct,unsearch,searched_point,maze): s = input() if (s=='Merry Christmas!'): return True elif (int(s)==20151224): while (s!='Merry Christmas!'): print('F') s = input() return True else: floor_count = int(s) if (drct==0): for c in range(1,floor_count+1): maze[y][x+c] = 1 if (searched_point[y][x+c]==0): unsearch.append((x+c,y)) elif (drct==1): for c in range(1,floor_count+1): maze[y-c][x] = 1 if (searched_point[y-c][x]==0): unsearch.append((y-c,x)) elif (drct==2): for c in range(1,floor_count+1): maze[y][x-c] = 1 if (searched_point[y][x-c]==0): unsearch.append((y,x-c)) elif (drct==3): for c in range(1,floor_count+1): maze[y+c][x] = 1 if (searched_point[y+c][x]==0): unsearch.append((y+c,x)) def search(x, y, drct,unsearch,searched_point,maze): end = searching_forward(x,y,drct,unsearch,searched_point,maze) if (end==True): return True print('L') drct = (drct+1) % 4 # for row in maze: # print(row) end = searching_forward(x,y,drct,unsearch,searched_point,maze) if (end==True): return True print('L') drct = (drct+1) % 4 # for row in maze: # print(row) end = searching_forward(x,y,drct,unsearch,searched_point,maze) if (end==True): return True print('L') drct = (drct+1) % 4 # for row in maze: # print(row) end = searching_forward(x,y,drct,unsearch,searched_point,maze) if (end==True): return True print('L') drct = (drct+1) % 4 # for row in maze: # print(row) return False def select_unserch_point(unsearch, searched_point): (x,y) = unsearch.popleft() searched_point[y][x] = 1 return x,y def move_unsearch_point(sx,sy,drct,dx,dy,maze): que = deque() route = list( [-2]*50 for _ in range(50) ) que.append((sx,sy,-1)) while True: (x,y,prevdrct) = que.popleft() if (route[y][x] != -2): continue route[y][x] = prevdrct if(x==dx and y==dy): break if(maze[y][x+1]==1): que.append((x+1,y,0)) if(maze[y-1][x]==1): que.append((x,y-1,1)) if(maze[y][x-1]==1): que.append((x-1,y,2)) if(maze[y+1][x]==1): que.append((x,y+1,3)) # for row in route: # print(row) action = deque() nx=dx ny=dy # print('sx->',sx,'sy->',sy) while True: if (nx==sx and ny==sy): break # print(nx,ny) nd=route[ny][nx] action.appendleft(nd) if(nd==0): nx -= 1 elif(nd==1): ny += 1 elif(nd==2): nx += 1 else: ny -= 1 # print(action) nx = sx ny = sy nd = drct while True: if(nx==dx and ny==dy): break ac = action.popleft() while (ac!=nd): print('L') nd = (nd+1)%4 print('F') if (nd==0): nx += 1 elif(nd==1): ny -=1 elif(nd==2): nx -= 1 elif(nd==3): ny += 1 return nx,ny,nd """ main """ maze = list( [0]*50 for _ in range(50) ) #search point searched_point = list( [0]*50 for _ in range(50) ) #unsearch point unsearch = deque() x=25 y=25 searched_point[y][x]=1 #direction 0..right, 1...up, 2...left, 3...down drct = 0 while True: goal = search(x,y,drct,unsearch,searched_point,maze) if (goal): break dx,dy = select_unserch_point(unsearch,searched_point) x,y,drct = move_unsearch_point(x,y,drct,dx,dy,maze)