#include<cstdio>
#include<cstring>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
double dp[16][9];

int main(){
	int n;
	char s, t, b;
	while( scanf("%d",&n), n ){
		scanf(" %c %c %c", &s, &t, &b);
		memset( dp, 0, sizeof(dp) );
		dp[0][s-'A'] = 1.0;
		rep(i,n){
			rep(j,9){
				if(j-3>=0 && b-'A'!=j-3)	dp[i+1][j-3] += dp[i][j]/4.0;
				else						dp[i+1][j]   += dp[i][j]/4.0;
				if(j%3!=0 && b-'A'!=j-1)	dp[i+1][j-1] += dp[i][j]/4.0;
				else						dp[i+1][j]   += dp[i][j]/4.0;
				if(j%3!=2 && b-'A'!=j+1)	dp[i+1][j+1] += dp[i][j]/4.0;
				else						dp[i+1][j]   += dp[i][j]/4.0;
				if(j+3< 9 && b-'A'!=j+3)	dp[i+1][j+3] += dp[i][j]/4.0;
				else						dp[i+1][j]   += dp[i][j]/4.0;
			}
		}
		printf("%.7lf\n",dp[n][t-'A']);
	}
	return 0;
}