#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
int main(){
  int n;
  while(cin>>n,n){
    vvi dp(3,vi(3));
    char a,b,c;
    cin>>a>>b>>c;
    a-='A';b-='A';c-='A';
    dp[a/3][a%3]=1;
    //    dp[c/3][c%3]=-1;
    int dx[]={0,0,1,-1};
    int dy[]={1,-1,0,0};
    rep(q,n){
      vvi nd(3,vi(3));
      rep(i,3)rep(j,3)rep(k,4){
	int x=i+dx[k];
	int y=j+dy[k];
	if(x<0||y<0||x>=3||y>=3||c/3==x&&c%3==y)
	  x-=dx[k],y-=dy[k];
	nd[x][y]+=dp[i][j];
      }
      dp=nd;
    }
    double out=dp[b/3][b%3];
    printf("%.9f\n",out/pow(4,n));
  }  
}