#include <bits/stdc++.h>
using namespace std;
int main() {
  int m[4] = {3, -3, 1, -1}, buttery, s, t, b, ans = 0;
  char cs, ct, cb;
  queue<pair<int, int> > que;
  for (; scanf("%d", &buttery); ans = 0) {
    if (buttery == 0) return 0;
    scanf("\n%c %c %c", &cs, &ct, &cb);
    s = cs - 65;
    t = ct - 65;
    b = cb - 65;
    que.push(pair<int, int>(buttery, s));
    while (!que.empty()) {
      pair<int, int> temp = que.front();
      que.pop();
      if (--temp.first < 0) {
        if (temp.second == t) ans++;
      } else {
        for (int i = 0; i < 4; i++) {
          if (temp.second + m[i] >= 0 && temp.second + m[i] < 9 &&
              temp.second + m[i] != b)
            que.push(pair<int, int>(temp.first, temp.second + m[i]));
          else
            que.push(pair<int, int>(temp.first, temp.second));
        }
      }
    }
    int tmp1 = 1 << (buttery * 2);
    double tmp2 = (double)ans / tmp1;
    printf("%.8lf\n", tmp2);
  }
}
