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

int main()
{
  int battery;
  char s, t, b;

  while (cin >> battery)
  {
    double pmatrix[9][9] = {
      {0.5,  0.25, 0,    0.25, 0,    0,    0,    0,    0}, // 0 A
      {0.25, 0.25, 0.25, 0,    0.25, 0,    0,    0,    0}, // 1 B
      {0,    0.25, 0.5,  0,    0,    0.25, 0,    0,    0}, // 2 C
      {0.25, 0,    0,    0.25, 0.25, 0,    0.25, 0,    0}, // 3 D
      {0,    0.25, 0,    0.25, 0,    0.25, 0,    0.25, 0}, // 4 E
      {0,    0,    0.25, 0,    0.25, 0.25, 0,    0,    0.25}, // 5 F
      {0,    0,    0,    0.25, 0,    0,    0.5,  0.25, 0},    // 6 G
      {0,    0,    0,    0,    0.25, 0,    0.25, 0.25, 0.25}, // 7 H
      {0,    0,    0,    0,    0,    0.25, 0,    0.25, 0.5}   // 8 I
    };

    if (battery==0) return 0;
    cin >> s >> t >> b;

    int start = s-'A';
    int broom = t-'A';
    int junk = b-'A';

    for (int i=0;i<9;i++) pmatrix[junk][i]=pmatrix[i][junk]=0;
    switch (junk)
    {
    case 0:
      pmatrix[1][1]=pmatrix[3][3]=0.5;
      break;
    case 1:
      pmatrix[0][0]=pmatrix[2][2]=0.75;
      pmatrix[4][4]=0.25;
      break;
    case 2:
      pmatrix[1][1]=pmatrix[5][5]=0.5;
      break;
    case 3:
      pmatrix[0][0]=pmatrix[6][6]=0.75;
      pmatrix[4][4]=0.25;
      break;
    case 4:
      pmatrix[1][1]=pmatrix[3][3]=pmatrix[5][5]=pmatrix[7][7]=0.5;
      break;
    case 5:
      pmatrix[2][2]=pmatrix[8][8]=0.75;
      pmatrix[4][4]=0.25;
      break;
    case 6:
      pmatrix[3][3]=pmatrix[7][7]=0.5;
      break;
    case 7:
      pmatrix[6][6]=pmatrix[8][8]=0.75;
      pmatrix[4][4]=0.25;
      break;
    case 8:
      pmatrix[5][5]=pmatrix[7][7]=0.5;
      break;
    }

    double P[9][9], Q[9][9];

    for (int i=0;i<9;i++)
      for (int j=0;j<9;j++) P[i][j] = pmatrix[i][j];

    for (int i=1;i<battery;i++)
    {
      for (int i=0;i<9;i++)
      {
        for (int j=0;j<9;j++)
        {
          Q[i][j] = 0.0;
          for (int k=0; k<9;k++)
          {
            Q[i][j] += P[i][k] * pmatrix[k][j];
          }
        }
      }
      for (int i=0;i<9;i++)
        for (int j=0;j<9;j++) P[i][j] = Q[i][j];

    }
    // for (int i=0;i<9;i++)
    // {
    //   for (int j=0;j<9;j++) printf("%4f ", P[i][j]);
    //   cout << endl;
    // }

    printf("%.8f\n", P[start][broom]);
  }
}