#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	while ( true )
	{
		int n;
		char s, t, b;
		cin >> n;
		if ( n == 0 )
			break;
		cin >> s >> t >> b;

		vector<double> P(9);
		P[s-'A'] = 1;

		for ( int i=0; i<n; i++ )
		{
			vector<double> T = P;
			P = vector<double>(9);

			P[0] += T[0] / 4 * 2;
			P[1] += T[0] / 4 * 1;
			P[3] += T[0] / 4 * 1;
			P[0] += P[b-'A'],  P[b-'A'] = 0;

			P[1] += T[1] / 4 * 1;
			P[0] += T[1] / 4 * 1;
			P[2] += T[1] / 4 * 1;
			P[4] += T[1] / 4 * 1;
			P[1] += P[b-'A'],  P[b-'A'] = 0;

			P[2] += T[2] / 4 * 2;
			P[1] += T[2] / 4 * 1;
			P[5] += T[2] / 4 * 1;
			P[2] += P[b-'A'],  P[b-'A'] = 0;
			
			P[3] += T[3] / 4 * 1;
			P[0] += T[3] / 4 * 1;
			P[4] += T[3] / 4 * 1;
			P[6] += T[3] / 4 * 1;
			P[3] += P[b-'A'],  P[b-'A'] = 0;

			P[1] += T[4] / 4 * 1;
			P[3] += T[4] / 4 * 1;
			P[5] += T[4] / 4 * 1;
			P[7] += T[4] / 4 * 1;
			P[4] += P[b-'A'],  P[b-'A'] = 0;

			P[5] += T[5] / 4 * 1;
			P[2] += T[5] / 4 * 1;
			P[4] += T[5] / 4 * 1;
			P[8] += T[5] / 4 * 1;
			P[5] += P[b-'A'],  P[b-'A'] = 0;

			P[6] += T[6] / 4 * 2;
			P[3] += T[6] / 4 * 1;
			P[7] += T[6] / 4 * 1;
			P[6] += P[b-'A'],  P[b-'A'] = 0;
			
			P[7] += T[7] / 4 * 1;
			P[4] += T[7] / 4 * 1;
			P[6] += T[7] / 4 * 1;
			P[8] += T[7] / 4 * 1;
			P[7] += P[b-'A'],  P[b-'A'] = 0;

			P[8] += T[8] / 4 * 2;
			P[5] += T[8] / 4 * 1;
			P[7] += T[8] / 4 * 1;
			P[8] += P[b-'A'],  P[b-'A'] = 0;
		}

		printf( "%.20f\n", P[t-'A'] );
	}
}