#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <numeric>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<((x))<<endl
#define fi first
#define se second

#define INF 2147483600
#define long long long

int main(){
  int n;
  while(cin>>n, n){
    char a,b,c; // ????§????????????????????????????
    scanf(" %c %c %c", &a, &b, &c);
    a-='A'; b-='A'; c-='A';
    vector<vector<double> > prev(3, vector<double>(3,0)), nxt(3,vector<double>(3));
    prev[a/3][a%3]=1;
    rep(i,n){
      rep(j,3) fill(all(nxt[j]), 0);
      rep(x,3) rep(y,3) if(prev[x][y]>0){
        const int dx[]={-1,0,0,1}, dy[]={0,-1,1,0};
        rep(k,4){
          int nx = x+dx[k], ny=y+dy[k];
          if(nx<0 || ny<0 || nx>=3 || ny>=3 || nx*3+ny==c) nxt[x][y] += prev[x][y]/4.;
          else nxt[nx][ny] += prev[x][y]/4.;
        }
      }
      swap(prev, nxt);
    }

    printf("%.6f\n", prev[b/3][b%3]);
  }
  return 0;
}