#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <list>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define F first
#define S second

int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};


int main(){
  int n;

  while(cin>>n,n){
    long double heya[20][3][3]={{{0.0}}};

    char s,t,b;
    cin>>s>>t>>b;
    s-='A';
    t-='A';
    b-='A';

    heya[0][s/3][s%3]=1.0;
    //cout<<n<<" "<<s/3<<" "<<s%3<<endl;


    rep(k,n){

      rep(i,9){
	int cx=i/3,cy=i%3;

	rep(j,4){
	  int nx=cx+dx[j],ny=cy+dy[j];
	  if(nx<0 || 2<nx || ny<0 || 2<ny || (nx==b/3 && ny==b%3))heya[k+1][cx][cy]+=heya[k][cx][cy]/4.0;
	  else heya[k+1][nx][ny]+=heya[k][cx][cy]/4.0;
	}
      }
    }

    printf("%.10Lf\n",heya[n][t/3][t%3]);
  }
}
    