#include<stdio.h>
#include<string.h>
#include<math.h>

int m[4]={3,-3,1,-1},s,t,b,dp[9][16];

int d(int p,int n)
{
	int i,r=0;
	if(dp[p][n]!=-1)
		return dp[p][n];
	if(n==0&&p==t)
		return 1;
	else if(n>0&&(abs(p/3-t/3)+abs(p%3-t%3))<=n)
	{
		for(i=0;i<4;i++)
		{
			if(p+m[i]>=0 && p+m[i]<9 && p+m[i]!=b)
				r+=d(p+m[i],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);
	}
}