#include <iostream>
#include <iomanip>
#include <cstdlib>

int main(){
  while(true){
    int n;
    std::cin >> n;
    if (n == 0) return EXIT_SUCCESS;
    char s, t, b;
    std::cin >> s >> t >> b;
    double p[9];
    for(int i=0; i<9; i++) p[i] = 0.0;
    p[s-'A'] = 1.0;
    for(int i=0; i<n; i++){
      double newp[9];
      for(int j=0; j<9; j++) newp[j] = 0.0;
      for(int j=0; j<9; j++){
        double p4 = p[j]/4.0;
        //left
        if(j%3==0) newp[j]+=p4;
        else if(j-1 == b-'A') newp[j]+=p4;
        else newp[j-1]+=p4;
        //right
        if(j%3==2) newp[j]+=p4;
        else if(j+1 == b-'A') newp[j]+=p4;
        else newp[j+1]+=p4;
        //up
        if(j<3) newp[j]+=p4;
        else if(j-3 == b-'A') newp[j]+=p4;
        else newp[j-3]+=p4;
        //down
        if(j>=6) newp[j]+=p4;
        else if(j+3 == b-'A') newp[j]+=p4;
        else newp[j+3]+=p4;
      }
      //update
      for(int j=0; j<9; j++) p[j] = newp[j];
    }
    std::cout << std::fixed;
    std::cout << std::setprecision(8) << p[t-'A'] << std::endl;
  }
}
