#include <iostream>
using namespace std;

int main(){
    double dp[3][3][16];//[x][y][n]
    char s,t,b;
    int n;
    int dx[]={-1,0,1,0};
    int dy[]={0,1,0,-1};
    cout.precision(8);
    cout << fixed;
    while(cin>>n,n){
        fill(dp[0][0],dp[3][0],0);
        cin>>s>>t>>b;
        s-='A';
        t-='A';
        b-='A';
        dp[s%3][s/3][n]=1;
        for(;n>0;n--){
            for(int x=0;x<3;x++){
                for(int y=0;y<3;y++){
                    for(int i=0;i<4;i++){
                        if((0<=x+dx[i]&&x+dx[i]<3)&&(0<=y+dy[i]&&y+dy[i]<3)&&!((x+dx[i])==(b%3)&&(y+dy[i])==(b/3))){
                            dp[x+dx[i]][y+dy[i]][n-1]+=dp[x][y][n]/4;
                        }else{
                            dp[x][y][n-1]+=dp[x][y][n]/4;
                        }
                    }
                }
            }
        }
        cout << dp[t%3][t/3][0] << endl;
    }
    return 0;
}