#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

while True:
    n = input()
    if n == 0:
        break
    s, t, b = raw_input().split()
    s = ord(s) - ord('A')
    t = ord(t) - ord('A')
    b = ord(b) - ord('A')
    lst = [0.0 for i in range(9)]
    lst[s] = 1.0
    for loop in range(n):
        lst2 = [0.0 for i in range(9)]
        for i in range(9):
            x = i % 3
            y = i / 3
            if y > 0 and (y - 1) * 3 + x != b:
                lst2[(y - 1) * 3 + x] += lst[y * 3 + x] / 4
            else:
                lst2[y * 3 + x] += lst[y * 3 + x] / 4
            if y < 2 and (y + 1) * 3 + x != b:
                lst2[(y + 1) * 3 + x] += lst[y * 3 + x] / 4
            else:
                lst2[y * 3 + x] += lst[y * 3 + x] / 4
            if x > 0 and y * 3 + x - 1 != b:
                lst2[y * 3 + x - 1] += lst[y * 3 + x] / 4
            else:
                lst2[y * 3 + x] += lst[y * 3 + x] / 4
            if x < 2 and y * 3 + x + 1 != b:
                lst2[y * 3 + x + 1] += lst[y * 3 + x] / 4
            else:
                lst2[y * 3 + x] += lst[y * 3 + x] / 4
        lst = lst2
    print "%.10f" % lst[t]
