In [40]:
def roll_dice(map_size, location, space, orders):
    dice_ew = [0,0]
    dice_ns = [0,0]
    dice_tb = [0,0]
    for order in orders:
        if order=="1":
            if location[1] < map_size[1] -1:
                location[1] += 1
                dice_ew.reverse()
                a = dice_ew
                dice_ew = dice_tb
                dice_tb = a
                if space[location[0]][location[1]] != 0:
                    dice_tb[1] = space[location[0]][location[1]]
                    space[location[0]][location[1]] = 0
                    print(dice_tb[0])
                else:
                    space[location[0]][location[1]] = dice_tb[1]
                    print(dice_tb[0])
            else:
                continue
        elif order == "2":
            if location[1] > 0:
                location[1] -= 1
                dice_tb.reverse()
                a = dice_ew
                dice_ew = dice_tb
                dice_tb = a
                if space[location[0]][location[1]] != 0:
                    dice_tb[1] = space[location[0]][location[1]]
                    space[location[0]][location[1]] = 0
                    print(dice_tb[0])
                else:
                    space[location[0]][location[1]] = dice_tb[1]
                    print(dice_tb[0])
            else:
                continue
        elif order == "3":
            if location[0] > 0 :
                location[0] -= 1
                dice_ns.reverse()
                a = dice_ns
                dice_ns = dice_tb
                dice_tb = a
                if space[location[0]][location[1]] != 0:
                    dice_tb[1] = space[location[0]][location[1]]
                    space[location[0]][location[1]] = 0
                    print(dice_tb[0])
                else:
                    space[location[0]][location[1]] = dice_tb[1]
                    print(dice_tb[0])
            else:
                continue
        elif order == "4":
            if location[0] < map_size[0]-1:
                location[0] += 1
                dice_tb.reverse()
                a = dice_ns
                dice_ns = dice_tb
                dice_tb = a
                if space[location[0]][location[1]] != 0:
                    dice_tb[1] = space[location[0]][location[1]]
                    space[location[0]][location[1]] = 0
                    print(dice_tb[0])
                else:
                    space[location[0]][location[1]] = dice_tb[1]
                    print(dice_tb[0])
            else:
                continue

In [39]:
first_input = list(map(int,input().split()))
map_size = [first_input[0],first_input[1]]
location = [first_input[2],first_input[3]]
space = []
for i in range(map_size[0]):
    sp = list(map(int,input().split()))
    space.append(sp)
orders = list(input().split())
roll_dice(map_size, location, space, orders)


2 2 0 0 16
0 2
3 4
4 4 4 4 1 1 1 1 3 3 3 3 2 2 2 2
0
[0, 0, 0, 0, 0, 3]
0
[0, 3, 0, 0, 0, 4]
0
[0, 3, 0, 4, 0, 2]
0
[2, 0, 0, 4, 0, 3]

In [41]:
first_input = list(map(int,input().split()))
map_size = [first_input[0],first_input[1]]
location = [first_input[2],first_input[3]]
space = []
for i in range(map_size[0]):
    sp = list(map(int,input().split()))
    space.append(sp)
orders = list(input().split())
roll_dice(map_size, location, space, orders)


4 2 0 0 8
0 2
3 4
5 6
7 8
4 4 4 1 3 3 3 2
0
0
3
0
0
8
6
3

In [42]:
first_input = list(map(int,input().split()))
map_size = [first_input[0],first_input[1]]
location = [first_input[2],first_input[3]]
space = []
for i in range(map_size[0]):
    sp = list(map(int,input().split()))
    space.append(sp)
orders = list(input().split())
roll_dice(map_size, location, space, orders)


3 3 1 1 9
1 2 3
4 0 5
6 7 8
1 3 2 2 4 4 1 1 3
0
0
0
3
0
1
0
6
0

In [43]:
first_input = list(map(int,input().split()))
map_size = [first_input[0],first_input[1]]
location = [first_input[2],first_input[3]]
space = []
for i in range(map_size[0]):
    sp = list(map(int,input().split()))
    space.append(sp)
orders = list(input().split())
roll_dice(map_size, location, space, orders)


3 3 0 0 16
0 1 2
3 4 5
6 7 8
4 4 1 1 3 3 2 2 4 4 1 1 3 3 2 2
0
0
0
6
0
8
0
2
0
8
0
2
0
8
0
2