#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main(){
  int n;
  char s,t,b;
  while(true){

    //input
    cin >> n;
    if(!n)
      break;
    cin >>s >>t >>b;
    int room[n+1][9];
    for(int i=0;i<9;i++)
      room[0][i] = 0;
    room[0][(int)(s-'A')] = 1;
    
    //wall
    int wall[5][5];
    for(int y=0;y<5;y++){
      for(int x=0;x<5;x++){
	if (x==0||x==4||y==0||y==4)
	  wall[x][y]=1;
	else
	  wall[x][y]=0;
      }
    }
    wall[(int)(b-'A')%3+1][(int)(b-'A')/3+1] = 1;

    for(int i=0;i<n;i++){
      for(int j=0;j<9;j++)
	room[i+1][j] = 0;
      for(int pos=0;pos<9;pos++){
	if(wall[pos%3+1][pos/3])
	  room[i+1][pos]+=room[i][pos];
	else
	  room[i+1][pos-3]+=room[i][pos];
	if(wall[pos%3][pos/3+1])
	  room[i+1][pos]+=room[i][pos];
	else
	  room[i+1][pos-1]+=room[i][pos];
	if(wall[pos%3+2][pos/3+1])
	  room[i+1][pos]+=room[i][pos];
	else
	  room[i+1][pos+1]+=room[i][pos];
	if(wall[pos%3+1][pos/3+2])
	  room[i+1][pos]+=room[i][pos];
	else
	  room[i+1][pos+3]+=room[i][pos];
      }
    }
    printf ("%.8f\n",((double)(room[n][(int)(t-'A')])/pow(4,(double)n)));
  }
  return 0;
}