#include <bits/stdc++.h>
using namespace std;
void func(int, int, int);
int n, k, data[3][3], memo[16][3][3];
int dy[3 + 1] = {-1, 0, 1, 0};
int dx[3 + 1] = {0, 1, 0, -1};
bool visited[16][3][3];
char s, t, b;
int main() {
  while (1) {
    cin >> n;
    if (!n) break;
    cin >> s >> t >> b;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        data[i][j] = 0;
        for (int k = 0; k < 16; k++) {
          memo[k][i][j] = 0;
          visited[k][i][j] = false;
        }
      }
    }
    data[(t - 'A') / 3][(t - 'A') % 3] = 1;
    data[(b - 'A') / 3][(b - 'A') % 3] = -1;
    visited[1][(s - 'A') / 3][(s - 'A') % 3] = true;
    func((s - 'A') / 3, (s - 'A') % 3, 0);
    k = pow(4, n);
    printf("%.8f\n", 1.0 * memo[0][(s - 'A') / 3][(s - 'A') % 3] / k);
  }
  return 0;
}
void func(int y, int x, int d) {
  if (d == n) {
    if (data[y][x] == 1) memo[d][y][x] = 1;
    return;
  }
  for (int i = 0; i < 3 + 1; i++) {
    int ny = y + dy[i], nx = x + dx[i];
    if (ny < 0 || nx < 0 || 3 < ny || 3 < nx || data[ny][nx] == -1) {
      if (visited[d + 1][y][x] == false) {
        visited[d + 1][y][x] = true;
        func(y, x, d + 1);
      }
      memo[d][y][x] += memo[d + 1][y][x];
    } else {
      if (visited[d + 1][ny][nx] == false) {
        visited[d + 1][y][x] = true;
        func(ny, nx, d + 1);
      }
      memo[d][y][x] += memo[d + 1][ny][nx];
    }
  }
}
