x = []
for y in range(9):
        x += [[chr(65+y),y]]
Room = dict(x)

By = [[0,0,1,3],[0,1,2,4],[1,2,2,5],[0,3,4,6],[1,3,5,7]]
By += [[2,4,5,8],[3,6,6,7],[4,6,7,8],[5,7,8,8]]

while 1:
        BatP = input()
        if BatP == 0:
                break
        Info = raw_input().split()
        Prob = [0 for x in range(9)]
        Prob[Room[Info[0]]] = 1

        while 0 < BatP:
                BatP -= 1
                AftI = Prob[:]
                for x in range(9):
                        Prob[x] = 0
                        if x == Room[Info[2]]:
                                for y in range(4):
                                        Prob[By[x][y]] += AftI[By[x][y]] *0.25
                        else:
                                for y in range(4):
                                        Prob[x] += AftI[By[x][y]] * 0.25

        print(Prob[Room[Info[1]]])