#include<cstdio>
#include<iostream>
using namespace std;

const int MAX = 16;
const int NUM = 9;
double f[MAX][NUM];

int main()
{	
	int n, s, t, b;
	char ts, tt, tb;
	while (cin >> n, n) {
		cin >> ts >> tt >> tb;
		s = ts - 'A';
		t = tt - 'A';
		b = tb - 'A';
		
		fill((double*)f, (double*)(f +  MAX) , 0);
		f[0][s] = 1.0;
		
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < NUM; j++) {
				if (j >= 3 && (j - 3) != b) f[i + 1][j - 3] += f[i][j] * 0.25;
				else f[i + 1][j] += f[i][j] * 0.25;
				if (j < 6 && (j + 3) != b) f[i + 1][j + 3] += f[i][j] * 0.25;
				else f[i + 1][j] += f[i][j] * 0.25;
				if (j % 3 != 0 && (j - 1) != b) f[i + 1][j - 1] += f[i][j] * 0.25;
				else f[i + 1][j] += f[i][j] * 0.25;
				if (j % 3 != 2 && (j + 1) != b) f[i + 1][j + 1] += f[i][j] * 0.25;
				else f[i + 1][j] += f[i][j] * 0.25;
			}
		}
		printf("%.8lf\n", f[n][t]);
	}
	
	return 0;
}