#include <iostream>
#include <cstdio>
using namespace std;
int mp[3][3],dx[] = {0,0,-1,1},dy[] = {1,-1,0,0};
int n;
char s,t,b;
int memo[16][3][3],visited[16][3][3];
void saiki(int x,int y,int bat){
  visited[bat][x][y] = 1;
  if(n == bat) {
    if((t-'A')%3==x&&(t-'A')/3==y)memo[bat][x][y]=1;
    return;
  }
  for(int i=0;i<4;i++){
    int nx = x+dx[i],ny = y+dy[i];
    if(((b-'A')%3==nx && (b-'A')/3==ny)||nx<0||ny <0||nx>=3||ny>=3) {
      if(visited[bat+1][x][y]==1) memo[bat][x][y] += memo[bat+1][x][y];
      else saiki(x,y,bat+1),memo[bat][x][y] += memo[bat+1][x][y];
    }
    else {
      if(visited[bat+1][nx][ny]==1) memo[bat][x][y] += memo[bat+1][nx][ny];
      else saiki(nx,ny,bat+1),memo[bat][x][y] += memo[bat+1][nx][ny];
    }
  }
}
int main() {
  while(1) {
    cin >> n;
    if(n==0) break;
    cin >> s >> t >> b;
    for(int i=0;i<=n;i++)
      for(int j=0;j<3;j++)
	for(int k=0;k<3;k++) memo[i][j][k] = visited[i][j][k]=0;
    saiki((s-'A')%3,(s-'A')/3,0);
    int ans = memo[0][(s-'A')%3][(s-'A')/3];
    printf("%.8f\n",1.0*ans/(1<<(2*n)));
  }
  return 0;
}