Dr. Asimov, a robotics researcher, loves to research, but hates houseworks and his house were really dirty. So, he has developed a cleaning robot.
As shown in the following figure, his house has 9 rooms, where each room is identified by an alphabet:
The robot he developed operates as follows:
If the battery runs down, the robot stops.
If not so, the robot chooses a direction from four cardinal points with the equal probability, and moves to the room in that direction. Then, the robot clean the room and consumes 1 point of the battery.
However, if there is no room in that direction, the robot does not move and remains the same room. In addition, there is a junk room in the house where the robot can not enter, and the robot also remains when it tries to enter the junk room. The robot also consumes 1 point of the battery when it remains the same place.
A battery charger for the robot is in a room. It would be convenient for Dr. Asimov if the robot stops at the battery room when its battery run down.
Your task is to write a program which computes the probability of the robot stopping at the battery room.
Constraints
Judge data includes at most 100 data sets.
n ≤ 15
s, t, b are distinct.
Input
The input consists of several datasets. Each dataset consists of:
n
s t b
n is an integer that indicates the initial battery point. s, t, b are alphabets respectively represent the room where the robot is initially, the battery room, and the junk room.
The input ends with a dataset which consists of single 0 for n. Your program should not output for this dataset.
Output
For each dataset, print the probability as a floating point number in a line. The answer may not have an error greater than 0.000001.
Example
Input
1
E A C
1
E B C
2
E A B
0
Output
0.00000000
0.25000000
0.06250000
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
There are 9 rooms identified by alphabets? The figure presumably shows arrangement? The description: "his house has 9 rooms, where each room is identified by an alphabet:" followed by . So we need to infer layout. Typically such problems have a 3x3 grid of rooms labelled by letters? Which letters? Probably A through I? Since example uses E, A, C, B. The rooms are identified by alphabets, so likely A to I. We need to know adjacency (which rooms are connected in the four cardinal directions). Since the figure is missing, we need to reconstruct based on typical problems: The house is a 3x3 grid, each room is a cell, identified by a letter (like A top-left, B top-middle, C top-right, D middle-left, E center, F middle-right, G bottom-left, H bottom-middle, I bottom-right). The robot can move north, south, east, west to adjacent rooms if exists. But there is a junk room b that robot cannot enter; if attempt to enter, stays in current room and consumes battery. Also if tries to move outside house (no room) stays and consumes battery.
So we have a set of 9 states (rooms) but b is effectively blocked (can't be entered; can robot start in b? s, t, b are distinct per constraints, so robot not start in junk. Also t and b distinct, so battery room is not junk.
Goal: starting from room s, battery n (initial battery points). At each step: if battery >0, then with equal probability (1/4) chooses direction N,S,E,W. Then determines the room that would be moved to based on the grid layout. If that target room exists (within the grid) and is not junk b, then robot moves to that room, cleans it (but cleaning irrelevant) and consumes 1 battery. If the target is invalid (outside grid or junk), robot stays in current room, and consumes 1 battery. The process continues until battery becomes 0 (i.e., after exactly n steps? Actually battery initially n, each action consumes 1, when battery runs down, robot stops. So after n steps, battery is 0. So it's a fixed-length process: exactly n steps (since no other termination condition). After n steps, robot stops in whatever room it's in. The probability that it stops at the battery room t is required.
Thus, it's a Markov chain with deterministic battery consumption; we need probability distribution over rooms after n steps, given starting room s, with transitions as described, and junk room b unreachable (blocked). So compute probability vector after n steps, and output probability of t.
We need to compute for up to n ≤ 15, which is small, and up to 100 datasets, so simple DP is fine.
First we must define the graph adjacency: 3x3 grid with coordinates (row, col) from (0,0) top-left to (2,2) bottom-right. Let's assign letters: typical order: A (0,0), B (0,1), C (0,2), D (1,0), E (1,1), F (1,2), G (2,0), H (2,1), I (2,2). But is this exactly the mapping? We need to confirm from examples.
Example 1:
Input:
1
E A C
Interpretation: n=1, s=E, t=A, b=C. Output: 0.00000000.
So with 1 step from E, probability to stop at A is 0. That seems plausible if from E you cannot reach A in one step? Actually from center E, possible moves: N to B, S to H, E to F, W to D. So can't go to A in one step, so 0. So fits.
Example 2:
1
E B C
Output: 0.25000000.
From E, with one step, probability to end at B is 0.25? Which move goes to B? North from E goes to B (since grid: row0 col1 is B). So yes probability 1/4 = 0.25. So mapping: B is top-middle. That matches.
Example 3:
2
E A B
Output: 0.0625 = 1/16.
Let's test: Starting E, want to end at A after 2 steps. With junk room b = B? Wait example: n=2, s=E, t=A, b=B. So B is junk, cannot be entered. So from E, possible moves: N -> B (but B is junk, so staying at E?), Actually if tries to move to junk, it stays in current room (E) and consumes battery. So that move results in staying at E. Similarly other moves: S->H, E->F, W->D. So from E, with one step, probabilities: to H (1/4), F (1/4), D (1/4), and staying E (when chooses N to B) (1/4). So distribution after 1 step: P(E)=1/4, P(D)=1/4, P(F)=1/4, P(H)=1/4. Then we need to go to A in second step. Need to compute probabilities from each state to reach A in one step, considering B is junk.
From D (1,0): possible moves? D is middle-left. Its N -> A? Actually coordinates: A (0,0), B (0,1), C (0,2), D (1,0), E (1,1), F (1,2), G (2,0), H (2,1), I (2,2). So D (1,0): N -> (0,0) = A; S -> (2,0) = G; E -> (1,1) = E; W -> (outside?) left edge, so no room, stays at D. Also junk B is (0,1) not relevant. So from D, moves: N to A (allowed), S to G, E to E, W (invalid) stays D. So probability to go to A from D in one step = 1/4. But also if A is junk? No A is target, not junk. So OK.
From F (1,2): N -> (0,2)=C; S -> (2,2)=I; W -> (1,1)=E; E -> outside -> stays F. So probability to A from F: 0 (no move leads to A). Because from F cannot reach A in one step.
From H (2,1): N -> (1,1)=E; S -> outside stays H; E -> (2,2)=I; W -> (2,0)=G. So probability to A: 0.
From E (1,1) after first step: from E, same as initial but junk B blocked. So from E, we computed distribution after another step? Need probability to go from E to A in one step. From E: N -> B (junk) => stay E; S -> H; E -> F; W -> D. So none go to A directly, so probability 0.
Thus total probability: from D: (1/4)*(1/4)=1/16 =0.0625. Others 0. So matches.
Thus mapping is consistent with the 3x3 grid with letters in row-major order (A top-left to I bottom-right).
So we confirm: The rooms are arranged in a 3x3 grid, rows: top row: A B C, middle row: D E F, bottom row: G H I. This is typical.
Now the problem: Compute probability after n steps (0 <= n <= 15) that robot is in room t. n can be 0? But constraints: n is given, might be >=1? The input terminates with n=0. So datasets have n>=1? Probably n can be 0? They didn't say n≥1; but they said n ≤ 15 and ends with 0. Possibly n=0 can appear as dataset? Since they said the input ends with a dataset which consists of single 0 for n; other datasets have n>0. But I'll handle n=0 just in case: If n=0, robot stops immediately at s; probability of being at t is 1 if s==t else 0. But s, t, b distinct? Not necessarily? Constraints say s, t, b are distinct. So s != t, so probability 0 if n=0. But we only need to process until 0, not output for that final dataset. So ignore.
Thus algorithm: For each dataset:
Map letters to coordinates (0-8 or row,col).
DP: state is room index (0..8), step count from 0 to n.
dp[k][room] = probability after k steps.
Initial: dp[0][s] = 1.
For step from 0 to n-1: For each room r where dp[k][r] > 0, compute transitions:
For each direction (N,S,E,W) with probability 1/4:
Determine target room r2: if move valid (within grid) and not junk b, then r2 = that room; else r2 = r (stay).
Add dp[k][r] * 0.25 to dp[k+1][r2].
After n steps, answer = dp[n][t].
But careful: There's also junk room b: robot cannot enter. So if the move would go to b, treat as invalid and stays. Also if move goes outside grid, stays.
Thus DP straightforward.
But note: The junk room is not just blocked but also? The robot can never be in junk room because it cannot enter. Starting room s is not junk. So DP states only need to consider non-junk rooms? Actually junk room can't be entered, but we might inadvertently compute probabilities for junk room if we treat as valid? We'll ensure we don't include junk in state set. Since transitions from a non-junk room never lead to junk because we treat moves to junk as staying. Also from junk we never start. So we can ignore junk state entirely.
Thus DP dimension: 9 rooms, but we could exclude junk from state vector to save small cost. Not needed.
Implement mapping: letter -> index 0..8: ord(letter)-ord('A')? But letters only A-I, so index = letter - 'A'.
Now complexity: n ≤ 15, 9 states, 100 datasets => trivial.
But wait: there might be nuance: The robot consumes battery even when stays. That's already accounted by step count. So fine.
Thus solution is simple.
But is there any possibility that the junk room is not part of the grid? It is one of the rooms, so it's just blocked. So it's as above.
Check constraints: n ≤ 15. So DP fine.
Edge cases: If junk room is adjacent, moves to it cause staying. That's included.
Now verify examples:
Example1: n=1, s=E(4), t=A(0), b=C(2). DP:
Step0: dp[0][4]=1.
Transitions from 4:
N: to B (1) -> valid? B not junk, so move to 1. dp[1][1]+=0.25
S: to H (7) -> valid, dp[1][7]+=0.25
E: to F (5) -> valid, dp[1][5]+=0.25
W: to D (3) -> valid, dp[1][3]+=0.25
After step1, dp[1][0]=0 -> answer 0.
Example2: n=1, s=E, t=B, b=C. Then from E N to B (1) valid, so dp[1][1]=0.25. Others not B. So answer 0.25.
Example3: n=2, s=E, t=A, b=B. As computed yields 0.0625.
Works.
Now implement.
Implementation details:
Read lines, strip, skip empty.
While True:
line = read n as integer. If n==0 break.
Next line: s t b three letters separated by space.
Process.
Mapping: coordinates: For index i (0-8), row = i // 3, col = i % 3.
For each state r (index), compute its transition list: for each direction, compute new row, col, check if within 0..2, if yes then target index = new_row*3+new_col. If target index equals junk index, treat as stay (r). Also if out of bounds, stay.
But note: If the move would go to junk, we treat as staying. That's correct because "robot also remains when it tries to enter the junk room."
We could precompute transitions for each room (9) and for each direction, considering junk b (which varies per dataset). Since b changes per dataset, we need to recompute transitions for each dataset. But n small, recompute each step fine.
Simplify: In DP loop, for each room r, for each direction, compute target. But that repeats many times; but steps <=15, states 9, directions 4, datasets 100 -> total operations negligible.
Alternatively, precompute neighbor coordinates for each room (without considering junk) and then adjust for junk.
But it's fine.
We'll compute DP using two arrays of size 9 (float). Initialize dp[ s_idx ] = 1.0.
For step in range(n):
newdp = [0.0]9
for r in range(9):
if dp[r]==0: continue
p = dp[r]
for each direction:
nr, nc = row(r) + dr, col(r) + dc
if 0<=nr<3 and 0<=nc<3:
nidx = nr3+nc
if nidx == b_idx: # junk
nidx = r # stay
else:
nidx = r
newdp[nidx] += p * 0.25
dp = newdp
After loop, answer = dp[t_idx].
Print with appropriate precision, e.g., print("{:.8f}".format(answer)) or just print(answer) with default? Problem says "The answer may not have an error greater than 0.000001." So printing with 8 decimal places should be safe.
But caution: Floating-point accumulation errors could be slight but fine.
Now confirm: The robot "cleans the room and consumes 1 point of the battery" when moving, also consumes when stays. So same consumption.
Now confirm the grid mapping: Are we sure the letters are exactly in row-major order A-I? The example uses A, B, C, E, etc. It's consistent.
But we must consider that the figure might have a different arrangement? Since it says "identified by an alphabet", maybe not necessarily in row-major order. Could be arranged arbitrarily? However typical "Dr. Asimov's house" problems often have a specific layout: The figure presumably shows a 3x3 grid with letters placed in that order: A top-left, B top-center, C top-right, D middle-left, E center, F middle-right, G bottom-left, H bottom-center, I bottom-right. This is common. The examples confirm that E is adjacent to B (above), D (left), F (right), H (below). So it's center. So mapping is correct.
Thus we implement.
But wait: The input includes "n" then line "s t b". There might be multiple spaces? We'll split.
Now also handle case n=0 termination, but not process.
Edge case: n can be 0? Possibly not.
We'll implement.
Now time to write solution in Python.
But let's check for potential pitfalls: The robot can also start in junk? Constraint says s,t,b are distinct, so s != b. So no.
Also, what about moving into junk and staying: that still consumes battery. Already accounted.
Now we produce final answer.
But could there be more than 9 rooms? The description says 9 rooms. So fixed 9.
Thus solution straightforward.
But I recall some similar problems: "Cleaning Robot" from AIZU or other online judge may have a different layout: maybe not all rooms are connected as a full grid? Perhaps some doors are missing? The description says "four cardinal points", meaning north, south, east, west. It doesn't specify that all adjacent rooms are connected; but typical house with rooms arranged in a grid, each room has doors in four directions to adjacent rooms if they exist. That is standard.
Thus I think it's correct.
Now produce final answer.
But double-check: The example output for second dataset: 0.25000000, we got 0.25. Good.
Third: 0.0625.
Now we need to ensure that when n=1 and t is the same as s? Could be? But s,t,b distinct, so not.
Now think about possible misinterpretation: The junk room is a room that robot cannot enter, but can it be left? Since robot never enters it, it's just blocked. That's fine.
Now confirm that the robot does not clean the junk room, but that's irrelevant.
Now what about the battery charger: The robot stops at the battery room when battery runs down. The probability computed is that after n steps, robot's position equals t.
Thus DP.
Thus code is simple.
But we might need to be careful about floating point precision: using double in Python is fine.
Now I'll produce final answer.
One more nuance: The problem statement: "If the battery runs down, the robot stops." and "the robot chooses a direction from four cardinal points with the equal probability, and moves to the room in that direction. Then, the robot clean the room and consumes 1 point of the battery." So if battery is 1 initially, robot will perform exactly one move (or stay) and then battery becomes 0 and stops. So DP steps = n.
Now I will code.
But perhaps there's a trick: The robot "cleans the room and consumes 1 point of the battery" when moving, and when it remains (because no room or junk) it also consumes 1 battery. So both consume 1. So step count is exactly n.
Thus solution done.
But confirm n can be as high as 15, and we have only 9 states, DP is O(n * 9 * 4) = trivial.
Now about input reading: The input has several datasets; each dataset: first line integer n, next line s t b. The last line is single "0". So we need to read lines accordingly.
We can use sys.stdin.read().split() to get tokens.
Alternatively read line by line.
Let's implement:
import sys
def main():
data = sys.stdin.read().strip().split()
# tokens: first n, then s, t, b, then next n, etc.
it = iter(data)
out_lines = []
for token in it:
n = int(token)
if n == 0:
break
s = next(it)
t = next(it)
b = next(it)
# map to indices
s_idx = ord(s) - ord('A')
t_idx = ord(t) - ord('A')
b_idx = ord(b) - ord('A')
# DP
dp = [0.0] * 9
dp[s_idx] = 1.0
for step in range(n):
newdp = [0.0] * 9
for r in range(9):
if dp[r] == 0.0:
continue
p = dp[r]
r_row, r_col = divmod(r, 3)
# directions: N, S, E, W
for dr, dc in [(-1,0),(1,0),(0,1),(0,-1)]:
nr, nc = r_row + dr, r_col + dc
if 0 <= nr < 3 and 0 <= nc < 3:
nidx = nr*3 + nc
if nidx == b_idx:
nidx = r # stay
else:
nidx = r
newdp[nidx] += p * 0.25
dp = newdp
out_lines.append("{:.8f}".format(dp[t_idx]))
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()
But check: When the robot tries to move into junk room, it stays and consumes battery. That's modeled as staying. However, if the robot is currently in a room adjacent to junk, and attempts to move into junk, it stays. But if it attempts to move into a valid room, it moves.
Now potential nuance: Is there any possibility that the robot "cleans the room" even when staying? It says when it remains, it consumes battery but does it clean? Probably not, but it's irrelevant.
Step0: dp[4]=1.
Step1:
r=4 (E)
p=1
N: (0,2?) Actually r=4 => row=1, col=1. N: (0,1) index 1 -> B, which is b, so nidx stays 4.
S: (2,1) index 7 -> H, valid not b -> 7
E: (1,2) index 5 -> F -> 5
W: (1,0) index 3 -> D -> 3
each 0.25. So newdp[4]=0.25, [7]=0.25, [5]=0.25, [3]=0.25.
Step2:
Compute from each.
r=3 (D) p=0.25
row=1, col=0.
N: (0,0) index 0 -> A (not b) -> 0
S: (2,0) index 6 -> G -> 6
E: (1,1) index 4 -> E -> 4
W: outside -> stays -> 3
each 0.0625 -> add to newdp: +0.0625 to idx0,6,4,3.
r=5 (F) p=0.25
row=1, col=2.
N: (0,2) idx2 -> C -> not b? b=B (idx1), so valid -> 2
S: (2,2) idx8 -> I -> 8
W: (1,1) idx4 -> E ->4
E: outside -> stay 5
contributions: each 0.0625 to 2,8,4,5.
r=7 (H) p=0.25
row=2, col=1.
N: (1,1) idx4 -> E ->4
S: outside -> stay 7
E: (2,2) idx8 -> I ->8
W: (2,0) idx6 -> G ->6
contributions: 0.0625 to 4,7,8,6.
r=4 (E) p=0.25
row=1, col=1.
N: (0,1) idx1 -> B (b) -> stay 4
S: (2,1) idx7 -> H ->7
E: (1,2) idx5 -> F ->5
W: (1,0) idx3 -> D ->3
contributions: 0.0625 to 4,7,5,3.
Now sum newdp after step2:
Initialize zero.
From r=3: idx0 +=0.0625, idx6+=0.0625, idx4+=0.0625, idx3+=0.0625.
From r=5: idx2+=0.0625, idx8+=0.0625, idx4+=0.0625, idx5+=0.0625.
From r=7: idx4+=0.0625, idx7+=0.0625, idx8+=0.0625, idx6+=0.0625.
From r=4: idx4+=0.0625, idx7+=0.0625, idx5+=0.0625, idx3+=0.0625.
Now compute totals:
idx0: 0.0625
idx1: 0
idx2: 0.0625
idx3: 0.0625+0.0625 = 0.125? Actually from r=3: idx3+=0.0625; from r=4: idx3+=0.0625 => total 0.125
idx4: 0.0625 (from r=3) +0.0625 (from r=5) +0.0625 (from r=7) +0.0625 (from r=4) = 0.25
idx5: 0.0625 (from r=5) +0.0625 (from r=4) = 0.125? Also from r=?? Wait from r=5: idx5+=0.0625 (stay); from r=4: idx5+=0.0625 (E to F). So 0.125. Also from others? None. So 0.125.
idx6: 0.0625 (from r=3) +0.0625 (from r=7) = 0.125
idx7: 0.0625 (from r=7) +0.0625 (from r=4) = 0.125
idx8: 0.0625 (from r=5) +0.0625 (from r=7) = 0.125
So dp[t]=dp[0]=0.0625 -> matches.
So code yields correct.
Now consider cases where junk room is something else; transitions will be blocked accordingly.
Thus solution is correct.
But hold on: There is nuance: The robot "consumes 1 point of the battery" when it stays due to no room or junk. So it's accounted. Good.
Now output formatting: The problem examples show 0.00000000, 0.25000000, 0.06250000. So printing 8 decimal places is fine.
But could there be cases with probability like 0.33333333? That would be printed as such.
Now final code.
But maybe there is a catch: The house has 9 rooms, but the letters might not be contiguous A-I? Could be any alphabets? Problem statement says "identified by an alphabet", but typical puzzles might use letters like A to I. However, the sample uses A, B, C, E. So likely A to I. So mapping is fine.
Now we produce final answer.
But maybe there is a twist: The junk room is also considered a room, but robot cannot enter, but what about starting room? It's not junk, so fine. Also the battery room might be junk? No distinct.
So answer.
Now I'll write final explanation and code.
Given the problem, I think the intended solution is dynamic programming or matrix exponentiation, but with n ≤ 15, DP is simpler.
Thus final.
But I recall similar problem from AIZU: "Cleaning Robot" might have more than 9 rooms? Actually I think I've seen a problem "Cleaning Robot" from ICPC where the house has 9 rooms. Yes.
Thus final.
Now produce final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
it = iter(data)
out = []
# directions: N, S, E, W
dirs = [(-1, 0), (1, 0), (0, 1), (0, -1)]
code
while True:
try:
token = next(it)
except StopIteration:
break
n = int(token)
if n == 0:
break
s = next(it)
t = next(it)
b = next(it)
# map letters A-I to indices 0..8
s_idx = ord(s) - ord('A')
t_idx = ord(t) - ord('A')
b_idx = ord(b) - ord('A')
# DP: probability of being in each room
prob = [0.0] * 9
prob[s_idx] = 1.0
for _ in range(n):
nxt = [0.0] * 9
for r in range(9):
if prob[r] == 0.0:
continue
p = prob[r] * 0.25
row, col = divmod(r, 3)
for dr, dc in dirs:
nr, nc = row + dr, col + dc
if 0 <= nr < 3 and 0 <= nc < 3:
nidx = nr * 3 + nc
if nidx == b_idx: # junk room → stay
nidx = r
else: # outside the house → stay
nidx = r
nxt[nidx] += p
prob = nxt
out.append("{:.8f}".format(prob[t_idx]))
sys.stdout.write("\n".join(out))