#include <cstdio>
int initial, battery, junk;
int dr[] = { -1, 0, 1, 0 }, dc[] = { 0, -1, 0, 1 };
double memo[3][3][16];
double func(int r, int c, int n)
{
	if (n == 0) return (3*r+c == battery);
	double& res = memo[r][c][n];
	if (res > -0.5) return res;

	res = 0;
	for (int i=0; i<4; ++i) {
		int nr = r + dr[i], nc = c + dc[i];
		if (nr<0 || nr>=3 || nc<0 || nc>=3 || 3*nr+nc==junk)
			res += func(r, c, n-1) / 4;
		else
			res += func(nr, nc, n-1) / 4;
	}
	return res;
}
int main()
{
	int n;
	while (scanf("%d ", &n), n) {
		for (int i=0; i<3; ++i) for (int j=0; j<3; ++j) for (int k=0; k<16; ++k)
			memo[i][j][k] = -1;

		char s, t, b;
		scanf("%c %c %c ", &s, &t, &b);
		initial = s - 'A';
		battery = t - 'A';
		junk = b - 'A';
		printf("%.10f\n", func(initial/3, initial%3, n));
	}
	return 0;
}