#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;

double p[20][9];
int n;
char s, t, b;

vector<vector<int>> adj = {
    {1,3,0,0},
    {0,2,4,1},
    {1,5,2,2},
    {0,4,6,3},
    {1,3,5,7},
    {2,4,8,5},
    {3,7,6,6},
    {4,6,8,7},
    {5,7,8,8}
};

int main() {
    while (cin >> n, n) {
        cin >> s >> t >> b;
        for (int i=0; i<20; ++i) {
            for (int j=0; j<9; ++j) {
                p[i][j] = 0;
            }
        }
        p[0][s - 'A'] = 1;
        for (int i=0; i<n; ++i) {
            for (int j=0; j<9; ++j) {
                for (int k=0; k<4; ++k) {
                    if (adj[j][k] == b - 'A') {
                        p[i+1][j] += p[i][j] / 4.0;
                    } else {
                        p[i+1][adj[j][k]] += p[i][j] / 4.0;
                    }
                }
            }
        }
        cout.width(8);
        cout << fixed << p[n][t - 'A'] << endl;
    }
    return 0;
}