using System;
using System.Linq;
using System.Collections.Generic;


namespace Test1 {



    class ClassMain {


        public static void Main(String[] args) {
            string line;
            while ((line = Console.ReadLine()) != null) {
                //int[] ints = line.Split(' ').Select(int.Parse).ToArray();
                //int n = ints[0];
                int n = int.Parse(line);
                if(n==0){break;}

                //int[] ints = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                string[] strs = Console.ReadLine().Split(' ');
                char s = strs[0][0];
                char t = strs[1][0];
                char b = strs[2][0];


                double[,] yasiki = new double[3, 3];
                int indexS = s - 'A';
                yasiki[indexS / 3, indexS % 3] = 1.0;
                int indexB = b - 'A';
                yasiki[indexB / 3, indexB % 3] = 0.0;

                

                while (n-- > 0) {
                    double[,] newyasiki = new double[3, 3];
                    newyasiki[indexB / 3, indexB % 3] = -1.0;

                    if (newyasiki[0, 0] > -0.5) {
                        newyasiki[0, 0] = yasiki[0, 1] * 0.25 + yasiki[1, 0] * 0.25;
                        newyasiki[0, 0] += yasiki[0, 0] * 0.5;
                    }
                    if (newyasiki[0, 1] > -0.5) {
                        newyasiki[0, 1] = yasiki[0, 0] * 0.25 + yasiki[0, 2] * 0.25 
                            + yasiki[1, 1] * 0.25;
                        newyasiki[0, 1] += yasiki[0, 1] * 0.25;
                    }
                    if (newyasiki[0, 2] > -0.5) {
                        newyasiki[0, 2] = yasiki[0, 1] * 0.25 + yasiki[1, 2] * 0.25;
                        newyasiki[0, 2] += yasiki[0, 2] * 0.5;
                    }
                    if (newyasiki[1, 0] > -0.5) {
                        newyasiki[1, 0] = yasiki[0, 0] * 0.25 + yasiki[2, 0] * 0.25 + 
                            yasiki[1, 1] * 0.25;
                        newyasiki[1, 0] += yasiki[1, 0] * 0.25;
                    }
                    if (newyasiki[1, 1] > -0.5) {
                        newyasiki[1, 1] = yasiki[0, 1] * 0.25 + yasiki[1, 0] * 0.25
                            + yasiki[1, 2] * 0.25 + yasiki[2, 1] * 0.25;
                    }
                    if (newyasiki[1, 2] > -0.5) {
                        newyasiki[1, 2] = yasiki[0, 2] * 0.25 + yasiki[1, 1] * 0.25
                            + yasiki[2, 2] * 0.25;
                        newyasiki[1, 2] += yasiki[1, 2] * 0.25;
                    }
                    if (newyasiki[2, 0] > -0.5) {
                        newyasiki[2, 0] = yasiki[2, 1] * 0.25 + yasiki[1, 0] * 0.25;
                        newyasiki[2, 0] += yasiki[2, 0] * 0.5;
                    }
                    if (newyasiki[2, 1] > -0.5) {
                        newyasiki[2, 1] = yasiki[2, 0] * 0.25 + yasiki[2, 2] * 0.25
                            + yasiki[1, 1] * 0.25;
                        newyasiki[2, 1] += yasiki[2, 1] * 0.25;
                    }
                    if (newyasiki[2, 2] > -0.5) {
                        newyasiki[2, 2] = yasiki[2, 1] * 0.25 + yasiki[1, 2] * 0.25;
                        newyasiki[2, 2] += yasiki[2, 2] * 0.5;

                    }
                    
                    
                    if (indexB == 0) {
                        newyasiki[0, 1] += 0.25 * yasiki[0, 1];
                        newyasiki[1, 0] += 0.25 * yasiki[1, 0];
                    }
                    if (indexB == 1) {
                        newyasiki[0, 0] += 0.25 * yasiki[0, 0];
                        newyasiki[1, 1] += 0.25 * yasiki[1, 1];
                        newyasiki[0, 2] += 0.25 * yasiki[0, 2];
                    }
                    if (indexB == 2) {
                        newyasiki[0, 1] += 0.25 * yasiki[0, 1];
                        newyasiki[1, 2] += 0.25 * yasiki[1, 2];
                    }


                    if (indexB == 3) {
                        newyasiki[0, 0] += 0.25 * yasiki[0, 0];
                        newyasiki[1, 1] += 0.25 * yasiki[1, 1];
                        newyasiki[2, 0] += 0.25 * yasiki[2, 0];
                    }
                    if (indexB == 4) {
                        newyasiki[0, 1] += 0.25 * yasiki[0, 1];
                        newyasiki[1, 0] += 0.25 * yasiki[1, 0];
                        newyasiki[1, 2] += 0.25 * yasiki[1, 2];
                        newyasiki[2, 1] += 0.25 * yasiki[2, 1];
                    }
                    if (indexB == 5) {
                        newyasiki[0, 2] += 0.25 * yasiki[0, 2];
                        newyasiki[1, 1] += 0.25 * yasiki[1, 1];
                        newyasiki[2, 2] += 0.25 * yasiki[2, 2];
                    }
                    if (indexB == 6) {
                        newyasiki[2, 1] += 0.25 * yasiki[2, 1];
                        newyasiki[1, 0] += 0.25 * yasiki[1, 0];
                    }
                    if (indexB == 7) {
                        newyasiki[2, 0] += 0.25 * yasiki[2, 0];
                        newyasiki[1, 1] += 0.25 * yasiki[1, 1];
                        newyasiki[2, 2] += 0.25 * yasiki[2, 2];
                    }
                    if (indexB == 8) {
                        newyasiki[2, 1] += 0.25 * yasiki[2, 1];
                        newyasiki[1, 2] += 0.25 * yasiki[1, 2];
                    }

                    for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) {
                        yasiki[i, j] = newyasiki[i, j];
                        }
                    yasiki[indexB / 3, indexB % 3] = 0.0;
                   
                }

                int indexT = t - 'A';
                double prob = yasiki[indexT / 3, indexT % 3];
                Console.WriteLine(prob);


            }
            
        }

        
    }

    

    
}

Measured memory_bytes: 5728