#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;
                 char cs,ct,cb;
                 for(;scanf("%d" ,&buttery);ans=0)
                {
                                 if(buttery==0)
                                                 return 0;
                                scanf( "\n%c %c %c",&cs,&ct,&cb);
                                memset(dp,-1, sizeof(dp));
                                s=cs-65;t=ct-65;b=cb-65;
                                ans=d(s,buttery);
                                 int tmp1=1<<(buttery*2);
                                 double tmp2=(double )ans/tmp1;
                                printf( "%.8lf\n",tmp2);
                }
}