#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

int result[15][9];
int able[9][4];
char t;

int solve(int battery, char room, int dst)
{
	if(able[room-'A'][dst]){
		if(dst == 0)      room -= 3;
		else if(dst == 1) ++room;
		else if(dst == 2) room += 3;
		else              --room;
	}
	
	--battery;
	if(battery == 0){
		return room == t;
	}else{
		if(result[battery][room-'A'] != -1)
			return result[battery][room-'A'];
		
		result[battery][room-'A'] = 0;
		for(int i=0 ; i<4 ; ++i)
			result[battery][room-'A'] += solve(battery, room, i);
		
		return result[battery][room-'A'];
	}
}

int main()
{
	int n;
	char s;
	
	while(scanf("%d", &n), n){
		char b;
		scanf(" %c %c %c", &s, &t, &b);
		
		for(int i=0 ; i<9 ; ++i){
			for(int j=0 ; j<4 ; ++j)
				able[i][j] = 1;
			
			if(i<3)    able[i][0] = 0;
			if(i>5)    able[i][2] = 0;
			if(i%3==0) able[i][3] = 0;
			if(i%3==2) able[i][1] = 0;
		}
		
		b -= 'A';
		if(b>=3)   able[b-3][2] = 0;
		if(b<=5)   able[b+3][0] = 0;
		if(b%3!=0) able[b-1][1] = 0;
		if(b%3!=2) able[b+1][3] = 0;
		
		for(int i=0 ; i<15 ; ++i){
			for(int j=0 ; j<9 ; ++j)
				result[i][j] = -1;
		}
		
		int acc = 0;;
		for(int i=0 ; i<4 ; ++i)
			acc += solve(n, s, i);
		
		printf("%lf\n", acc/pow(4.0, n));
	}
	return 0;
}