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

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

int dp[9][16];

int d(int pos,int buttery)
{
	int i,n=0;
	if(dp[pos][buttery]!=-1)
		return dp[pos][buttery];
	if(buttery==0&&pos==t)
		return 1;
	else if(buttery>0&&(abs(pos/3-t/3)+abs(pos%3-t%3))<=buttery)
	{
		for(i=0;i<4;i++)
		{
			if(pos+m[i]>=0 && pos+m[i]<9 && pos+m[i]!=b)
				n+=d(pos+m[i],buttery-1);
			else
				n+=d(pos,buttery-1);
		}
	}
	return dp[pos][buttery]= n;
}

int main()
{
	int buttery,ans,tmp1;
	double tmp2;
	for(;scanf("%d",&buttery);ans=0)
	{
		if(buttery==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,buttery);
		tmp1=1<<(buttery*2);
		tmp2=(double)ans/tmp1;
		printf("%.8lf\n",tmp2);
	}
}