#include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=(a);i<(b);++i)

const int dx[4] = {-1,0,1,0};
const int dy[4] = {0,-1,0,1};

int n;
char s, t, b;
double p[2][3][3];

int getX(char c) { return (c - 'A') % 3; }
int getY(char c) { return (c - 'A') / 3; }

void solve() {
	for_(y,0,3) for_(x,0,3) p[0][y][x] = 0.;
	p[0][getY(s)][getX(s)] = 1.0;
	
	for_(i,0,n) {
		for_(y,0,3) for_(x,0,3) p[1][y][x] = 0.;
		for_(y,0,3) for_(x,0,3) {
			for_(d,0,4) {
				int nx = x + dx[d], ny = y + dy[d];
				if ((nx < 0 || nx >= 3 || ny < 0 || ny >= 3) ||
					(ny == getY(b) && nx == getX(b))) {
					nx = x;
					ny = y;
				}
				p[1][ny][nx] += p[0][y][x] * 0.25;
			}
		}
		swap(p[0], p[1]);
	}
	
	cout << setprecision(12) << setiosflags(ios::fixed) << p[0][getY(t)][getX(t)] << endl;
}

int main() {
	while (cin >> n, n) {
		cin >> s >> t >> b;
		solve();
	}
}
