#include <iostream>
#include <cmath>
#include <strings.h>

using namespace std;

int global_block;
int global_agents[16][9];

int north(int x) {
  if (x < 3 || global_block == x - 3)
    return x;
  else
    return x - 3;
}
int east(int x) {
  if (x % 3 == 2 || global_block == x + 1)
    return x;
  else
    return x + 1;
}
int south(int x) {
  if (x >= 6 || global_block == x + 3)
    return x;
  else
    return x + 3;
}
int west(int x) {
  if (x % 3 == 0 || global_block == x - 1)
    return x;
  else
    return x - 1;
}

double compute(int energy, int start, int target, int block)
{
  global_block = block;
  bzero(global_agents, sizeof(int) * 16 * 9);
  global_agents[0][start] = 1;

  for (int i=0; i < energy; i++) {
    for (int pos=0; pos < 9; pos++) {
      if (global_agents[i][pos] != 0) {
        global_agents[i+1][north(pos)] += global_agents[i][pos];
        global_agents[i+1][west (pos)] += global_agents[i][pos];
        global_agents[i+1][south(pos)] += global_agents[i][pos];
        global_agents[i+1][east (pos)] += global_agents[i][pos];
      }
    }
  }
  
  return pow(0.25, energy) * global_agents[energy][target];
}

int main(void) 
{
  while (true) {

    int energy;
    cin >> energy;
    if (energy == 0) break;
    char start, target, block;
    cin >> start >> target >> block;
    int s = start  - 'A';
    int t = target - 'A';
    int b = block  - 'A';
    //cout << "start = " << s << ", target = " << t << ", block = " << b << endl;
    cout << compute(energy, s, t, b) << endl;
  }
  
  return 0;
}