
fun main(args:Array<String>):Unit {
    val charToPoint = mapOf(Pair("A", Point(0, 0)), Pair("B", Point(1, 0)), Pair("C", Point(2, 0)),
            Pair("D", Point(0, 1)), Pair("E", Point(1, 1)), Pair("F", Point(2, 1)),
            Pair("G", Point(0, 2)), Pair("H", Point(1, 2)), Pair("I", Point(2, 2))
            )
    val points = (0 until 3).flatMap{y -> (0 until 3).map{Point(it, y)}}
    while(true){
        val n = readLine()!!.trim().toInt()
        if (n == 0) return
        val roomState = Array(3){Array(3){true}}
        val (s, t, b) = readLine()!!.trim().split(' ')
        val start = charToPoint[s]!!
        val buttery = charToPoint[t]!!
        roomState[charToPoint[b]!!.y][charToPoint[b]!!.x] = false
        val matrix = Array(9){Array(9){.0}}
        points.forEachIndexed { index, point ->
            point.neighbors().forEach {
                if (it.x in 0 until 3 && it.y in 0 until 3 && roomState[it.y][it.x]){
                    matrix[index][it.x + it.y * 3] += 0.25
                }else{
                    matrix[index][point.x + point.y * 3] += 0.25
                }
            }
        }
        val res = exp(matrix, n)
        println(res[buttery.x + buttery.y * 2][start.x + start.y * 3])
    }
}
operator fun Array<Array<Double>>.times(that:Array<Array<Double>>):Array<Array<Double>> {
    return this.map{line ->
        that.first().indices.map{column ->
            line.zip(that.map{it[column]}).sumByDouble{(a, b) -> a * b}
        }.toTypedArray()
    }.toTypedArray()
}
fun exp(base:Array<Array<Double>>, e:Int):Array<Array<Double>> {
    return when(e){
        0 -> Array(base.size){y -> Array(base.size){x -> if (x == y) 1.0 else .0}}
        1 -> base
        else -> exp(base, e / 2).let{if (e % 2 == 1) base * it * it else it * it}
    }
}
data class Point(val x:Int, val y:Int){
    operator fun plus(that:Point):Point = Point(x + that.x, y + that.y)
    fun neighbors():Iterable<Point> = listOf(Point(1, 0), Point(-1, 0), Point(0, 1), Point(0, -1)).map{this + it}
}
