#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)(n); ++i)
#define FOREACH(it,c) for(typeof(c.begin()) it = c.begin(); it != c.end(); ++it)

double a[3][3];
int s,t,b;
#define A(v) (a[(v)%3][(v)/3])

int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};
bool go(int x, int y) {
  if (x < 0 || x > 2 || y < 0 || y > 2 || y*3+x == b) return false;
  return true;
}

void move() {
  double atmp[3][3] = {0};
  REP(x,3) REP(y,3) {
    double v = a[x][y] / 4.0;
    REP(d,4) {
      if(go(x+dx[d],y+dy[d])) atmp[x+dx[d]][y+dy[d]] += v;
      else atmp[x][y] += v;
    }
  }
  REP(x,3) REP(y,3) a[x][y] = atmp[x][y];
}

int main() {
  int n;
  cout.precision(8);
  cout<<fixed;
  while (cin>>n, n) {
    char ss,tt,bb; cin>>ss>>tt>>bb;
    s=ss-'A', t=tt-'A', b=bb-'A';
    memset(a,0,sizeof(a));
    A(s) = 1;
    REP(_,n) move();
    //REP(y,3) {REP(x,3) cout<<a[x][y]<<" "; cout<<endl;}
    cout<<A(t)<<endl;
  }
  return 0;
}