#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,s,t,b;
int kai[16][9];
bool visited[16][9];
int dx[]={0,1,0,-1},dy[]={-3,0,3,0};

void dfs(int p,int e){
  visited[e][p]=1;
  if(e==n){
    if(p==t)kai[e][p]=1;
    return;
  }
  for(int i=0;i<4;i++){
    int np=p+dx[i]+dy[i];
    if(dx[i]+p%3<0||dx[i]+p%3>=3||np<0||9<=np||np==b)np=p;
    if(!visited[e+1][np])dfs(np,e+1);
    kai[e][p]+=kai[e+1][np];
  }
}

int main(){
  while(cin>>n&&n!=0){
    char sc,tc,bc;
    for(int i=0;i<16;i++)for(int j=0;j<9;j++)kai[i][j]=0,visited[i][j]=0;
    //   memset(kai,0,16*9);
    //   memset(visited,0,16*9);
    cin>>sc>>tc>>bc;
    s=sc-'A',t=tc-'A',b=bc-'A';
    dfs(s,0);
    printf("%.8f\n",1.0*kai[0][s]/(1<<(n*2)));
  }
  return 0;
}