#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
char room[5][5] = {{1, 1, 1, 1, 1},
                   {1, 0, 0, 0, 1},
                   {1, 0, 0, 0, 1},
                   {1, 0, 0, 0, 1},
                   {1, 1, 1, 1, 1}};
void solve(char n, char m[5][5]) {
  char m_[5][5] = {0};
  for (; n; n--) {
    for (int y = 1; y <= 3; y++) {
      for (int x = 1; x <= 3; x++) {
        if (m[y][x]) {
          for (int i = 0; i < 4; i++) {
            int mx = x + dx[i];
            int my = y + dy[i];
            if (room[my][mx] == 0) {
              m_[my][mx] += m[y][x];
            }
          }
          m[y][x] = 0;
        }
      }
    }
    for (int y = 1; y <= 3; y++) {
      for (int x = 1; x <= 3; x++) {
        m[y][x] = m_[y][x];
      }
    }
  }
}
int main() {
  char fx[256] = {0}, fy[256] = {0};
  int n;
  char s, t, b;
  for (int i = 0; i < 9; i++) {
    fx[i + 'A'] = i % 3 + 1;
    fy[i + 'A'] = i / 3 + 1;
  }
  while (scanf("%d ", &n), n) {
    scanf("%c %c %c", &s, &t, &b);
    char m[5][5] = {0};
    for (int i = 1; i <= 3; i++) {
      for (int j = 1; j <= 3; j++) {
        room[i][j] = 0;
        m[i][j] = 0;
      }
    }
    room[fy[b]][fx[b]] = 1;
    m[fy[s]][fx[s]] = 1;
    if (n % 2 == 1 && (fy[s] * 3 + fx[s]) % 2 == (fy[t] * 3 + fx[t]) % 2) {
      ;
    } else if (n % 2 == 0 &&
               (fy[s] * 3 + fx[s]) % 2 != (fy[t] * 3 + fx[t]) % 2) {
      ;
    } else {
      solve(n, m);
    }
    printf("%.9f\n", m[fy[t]][fx[t]] / pow(4, n));
  }
}
