#include <iostream>
#include <cstdio>
using namespace std;

int main(){
		int n;
	char s,t,b;
	while(cin>>n,n){
		double dp[16][9]={0.0};
		int floor[9][4]={
			{0,1,3,0},{1,2,4,0},{2,2,5,1},
			{0,4,6,3},{1,5,7,3},{2,5,8,4},
			{3,7,6,6},{4,8,7,6},{5,8,8,7}
		};
		cin>>s>>t>>b;
		dp[0][s-'A']=1;
		for(int i=0;i<9;i++)for(int j=0;j<4;j++)if(floor[i][j]==b-'A')floor[i][j]=i;
		for(int i=0;i<n;i++){
			for(int j=0;j<9;j++){
				for(int k=0;k<4;k++){
					dp[i+1][floor[j][k]]+=dp[i][j]/4;
				}
			}
		}
		printf("%.8lf\n",dp[n][t-'A']);
	}
	return 0;
}