#include<cstdio>
using namespace std;

int main(){
  int n;
  char s,t,b;
  double p[16][9];

  for(;;){
    scanf(" %d ",&n);
    if(!n)break;

    scanf(" %c %c %c ",&s,&t,&b);

    for(int i=0;i<=n;i++)
      for(int j=0;j<9;j++)p[i][j] = 0.0;

    p[0][s-'A'] = 1.0;

    for(int i=0;i<n;i++){
      for(int j=0;j<9;j++){
	if(j>=3 && j-3!=b-'A')p[i+1][j-3] += p[i][j]/4;
	else p[i+1][j] += p[i][j]/4;

	if(j%3!=2 && j+1!=b-'A')p[i+1][j+1] += p[i][j]/4;
	else p[i+1][j] += p[i][j]/4;

	if(j<6 && j+3!=b-'A')p[i+1][j+3] += p[i][j]/4;
	else p[i+1][j] += p[i][j]/4;

	if(j%3 && j-1!=b-'A')p[i+1][j-1] += p[i][j]/4;
	else p[i+1][j] += p[i][j]/4;
      }
    }

    printf("%.8lf\n",p[n][t-'A']);
  }
}