#include "bits/stdc++.h"
using namespace std;
using ld = long double;
const int dx[4] = { -1,0,1,0 };
const int dy[4] = { 0,1,0,-1 };
int main() {
	while (1) {
		int N; cin >> N;
		if (!N)break;
		char s, t, b; cin >> s >> t >> b;
		int start, elec, goal;
		start = s - 'A';
		elec = t - 'A';
		goal = b - 'A';
		vector<vector<ld>>pers(N + 1, vector<ld>(9));
		pers[0][start] = 1;
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < 9; ++j) {
				int x = j / 3;
				int y = j % 3;
				for (int way = 0; way < 4; ++way) {
					int nextx = x + dx[way];
					int nexty = y + dy[way];
					if (nextx >= 0 && nextx <= 2 && nexty >= 0 && nexty <= 2) {
						int nextj = nextx * 3 + nexty;
						if (nextj == goal) {
							pers[i + 1][j] += pers[i][j] / 4;
						}
						else {
							pers[i + 1][nextj] += pers[i][j] / 4;
						}
					}
					else {
						pers[i + 1][j] += pers[i][j] / 4;
					}
				}
			}
		}
		cout <<setprecision(10)<<fixed<< pers[N][elec] << endl;
	}
	return 0;
}