#include<stdio.h>
#include<string.h>
#include<math.h>
#define c(v) ((v)>=0&&(v)<3)
int m[5]={0,1,0,-1,0},s,t,b,dp[9][16];

int d(int p,int n)
{
	int i,r=0,x,y;
	if(dp[p][n]!=-1)
		return dp[p][n];
	if(n==0)
	{
		if(p==t)
			return 1;
		else
			return 0;
	}
	else if(n>0&&(abs(p/3-t/3)+abs(p%3-t%3))<=n)
	{
		for(i=0;i<4;i++)
		{
			x=p%3+m[i];
			y=p/3+m[i+1];
			if(c(x)&&c(y)&&y*3+x!=b)
				r+=d(y*3+x,n-1);
			else
				r+=d(p,n-1);
		}
	}
	return dp[p][n]= r;
}

int main()
{
	int n,ans,tmp1;
	double tmp2;
	for(;scanf("%d",&n);ans=0)
	{
		if(n==0)
			return 0;
		scanf("\n%c %c %c",&s,&t,&b);
		memset(dp,-1,sizeof(dp));
		s-=65;t-=65;b-=65;
		ans=d(s,n);
		tmp1=1<<(n*2);
		tmp2=(double)ans/tmp1;
		printf("%lf\n",tmp2);
	}
	return 0;
}