#include<iostream>
#include<sstream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<numeric>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>

#define rep(i,n) for(int i=0;i<n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define rp(i,c) rep(i,(c).size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf=1<<28;
const double INF=1e12,EPS=1e-9;

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

int main()
{
	int px[256],py[256];
	rep(i,3)rep(j,3)px['A'+i*3+j]=j,py['A'+i*3+j]=i;
	
	int n;
	while(cin>>n,n)
	{
		double p[20][3][3]; rep(i,n+1)rep(j,3)rep(k,3)p[i][j][k]=0;
		char s,t,b; cin>>s>>t>>b;
		int bx=px[b],by=py[b];
		
		p[0][py[s]][px[s]]=1;
		rep(i,n)rep(j,3)rep(k,3)rep(d,4)
		{
			int ny=j+dy[d],nx=k+dx[d];
			if(ny<0||nx<0||ny>=3||nx>=3 || ny==by&&nx==bx)ny=j,nx=k;
			p[i+1][ny][nx]+=p[i][j][k]/4;
		}
		
		printf("%.8f\n",p[n][py[t]][px[t]]);
	}
	
	return 0;
}