import copy def flip_board(n, m, source_board): target_board = [[source_board[i][j] for i in range(n)] for j in range(m)] return target_board def loopover(mixed_up_board, solved_board): n = len(solved_board) m = len(solved_board[0]) is_flip = False if m % 2 == 1: is_flip = True mixed_up_board = flip_board(n, m, mixed_up_board) solved_board = flip_board(n, m, solved_board) n, m = m, n alpha_mapping = {} for i in range(n): for j in range(m): alpha_mapping[solved_board[i][j]] = (i, j) for i in range(n): for j in range(m): mixed_up_board[i][j] = alpha_mapping[mixed_up_board[i][j]] res = [] flip_dir = {'L': 'U', 'U': 'L', 'R': 'D', 'D': 'R'} def Move(dir, idx): if dir == 'L': mixed_up_board[idx] = mixed_up_board[idx][1:] + [mixed_up_board[idx][0]] elif dir == 'R': mixed_up_board[idx] = [mixed_up_board[idx][-1]] + mixed_up_board[idx][:-1] elif dir == 'U': tmp_v = mixed_up_board[0][idx] for o in range(1, n): mixed_up_board[o - 1][idx] = mixed_up_board[o][idx] mixed_up_board[n - 1][idx] = tmp_v else: tmp_v = mixed_up_board[n - 1][idx] for o in range(n - 1, 0, -1): mixed_up_board[o][idx] = mixed_up_board[o - 1][idx] mixed_up_board[0][idx] = tmp_v if is_flip: dir = flip_dir[dir] res.append(dir + str(idx)) def FancyMove(dir, idx): p = (idx + m - 2) % m q = (p + 1) % m r = idx invdir = 'R' if dir == 'L' else 'L' Move('U', p) Move(dir, n - 1) Move('D', p) Move(dir, n - 1) Move('U', p) Move(invdir, n - 1) Move(invdir, n - 1) Move('D', p) for i in range(n - 1): for j in range(m): x, y = i, j for k in range(i, n): for l in range(m): if mixed_up_board[k][l] == (i, j): x, y = k, l if (x, y) == (i, j): continue if x == i: Move('D', y) Move('R', x + 1) Move('U', y) x = i + 1 y = (y + 1) % m if y == j: Move('L', x) y = (y + m - 1) % m dcnt = x - i for k in range(dcnt): Move('D', j) while y != j: Move('R', x) y = (y + 1) % m for k in range(dcnt): Move('U', j) if m == 2: if mixed_up_board[n - 1][0] != (n - 1, 0): Move('R', n - 1) else: i = n - 1 for j in range(m - 2): y = j for l in range(j, m): if mixed_up_board[i][l] == (i, j): y = l break if y == j: continue while y > j + 1: FancyMove('L', y) y -= 2 if y == j + 1: FancyMove('L', (j + 2) % m) FancyMove('L', (j + 2) % m) j = m - 2 if mixed_up_board[i][j] == (i, j): pass else: if m % 2 == 1: res = None else: while j != 0: FancyMove('L', j) j -= 2 Move('L', n - 1) bres = res r2 = [] for x in res: if x[0] == 'D': r2.append(['C', int(x[1]), 1]) elif x[0] == 'U': r2.append(['C', int(x[1]), 3]) elif x[0] == 'L': r2.append(['R', int(x[1]), 3]) elif x[0] == 'R': r2.append(['R', int(x[1]), 1]) res = [copy.copy(r2[0])] for i in range(1, len(r2)): if r2[i][0] == res[-1][0] and r2[i][1] == res[-1][1]: res[-1][2] = (res[-1][2] + r2[i][2]) % 4 else: while res[-1][2] == 0: res = res[:-1] res.append(copy.copy(r2[i])) while res[-1][2] == 0: res = res[:-1] return res source_ = list(map(list, ['FHLE','MOIC','DNPB','KAGJ'])) target_ = list(map(list, ['ABCD','EFGH','IJKL','MNOP'])) source_ = [] for i in range(4): source_.append(input()) source_ = list(map(list, source_)) # print(source_) res = loopover(source_, target_) print(len(res)) for x in res: print(x[0], x[1], x[2])