#include <iostream>
#include <string.h>
#include <cmath>
#include <cstdio>

using namespace std;

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

int main(void)
{
	int n;
	char s,t,b;
	int dp[5][5];
	int i1,i2,i3,i4,i5,i6;
	double ans;
	int temp[5][5];

	while(1){
		cin>>n;
		if(n==0) break;
		cin>>s>>t>>b;

		memset(dp,-1,sizeof(dp));
		memset(temp,0,sizeof(temp));
		for(i1=1;i1<=3;i1++){
			for(i2=1;i2<=3;i2++){
				dp[i1][i2]=0;
			}
		}
		dp[((s-'A')/3)+1][((s-'A')%3)+1] = 1;
		dp[((b-'A')/3)+1][((b-'A')%3)+1] = -1;
		temp[((b-'A')/3)+1][((b-'A')%3)+1] = -1;

		for(i1=0;i1<n;i1++){

			for(i2=1;i2<=3;i2++){
				for(i3=1;i3<=3;i3++){
					
					if(dp[i2][i3]>0){
						for(i4=0;i4<4;i4++){
							if(dp[i2+dy[i4]][i3+dx[i4]]==-1) temp[i2][i3]+=dp[i2][i3];
							else temp[i2+dy[i4]][i3+dx[i4]]+=dp[i2][i3];
						}
					}
				}
			}

			for(i5=1;i5<=3;i5++){
				for(i6=1;i6<=3;i6++){
					dp[i5][i6]=temp[i5][i6];
				}
			}
			memset(temp,0,sizeof(temp));
			temp[((b-'A')/3)+1][((b-'A')%3)+1] = -1;
		}

		ans=double(dp[((t-'A')/3)+1][((t-'A')%3)+1])/pow(4.0,n);
		printf("%.8lf\n",ans);


		/*for(i1=1;i1<=3;i1++){
			for(i2=1;i2<=3;i2++){
				cout<<dp[i1][i2]<<" ";
			}
			cout<<endl;
		}*/
	}
	return 0;
}