#K5906. Minimal Steps to Identify Faulty Starting Position
Minimal Steps to Identify Faulty Starting Position
Minimal Steps to Identify Faulty Starting Position
You are given a configuration of sections, move options, and safety status in a labyrinth. Two players, Emma and Jack, each believe they are in some starting section (denoted by indices E and J respectively). Every section has up to four possible moves (up, down, left, right), where a move value of -1 indicates that the corresponding direction is not available. In addition, each section is either safe or contains a trap, indicated by an array (1 for safe, 0 for trap).
Your task is to use a breadth-first search (BFS) strategy to compute the minimum number of steps required to definitively conclude which starting position is wrong. In each step, both Emma and Jack move simultaneously; if either of them enters a section with a trap (i.e. safety value 0), then that move confirms that the corresponding starting position was wrong. If no such conclusion can be reached after exploring all possibilities, output (\texttt{undecidable}).
Formally, let (S) denote the set of sections with indices (0 \le i < n). Let the move options from any section (i) be given by a 4-element vector (\textbf{m}i = [m{i,u}, m_{i,d}, m_{i,l}, m_{i,r}]), where a value of (-1) means that direction is not accessible. The safety of each section is defined by an array (\textbf{s}) where (s_i) is either 1 (safe) or 0 (trap). Starting from (E) and (J) for Emma and Jack respectively, we seek the smallest non-negative integer (k) such that after (k) steps, at least one player lands on a trap, thereby identifying the faulty starting assumption. If no such (k) exists, output (undecidable).
inputFormat
The input is read from standard input (stdin) with the following format:
n
— an integer representing the number of sections.
E J
— two integers representing the starting section indices of Emma and Jack respectively (0-indexed).
Then follow n
lines, each containing 4 space‐separated integers. Each line represents the move options from that section in the order: up, down, left, right (a move of -1 indicates no valid move in that direction).
Finally, there is a line containing n
space‐separated integers, where the i-th integer represents the safety status of section i (1 for safe and 0 for trap).
outputFormat
Output a single line to standard output (stdout):
If a conclusive minimal number of steps is found, output the integer representing the number of steps (note that when a trap is first encountered, the step count is incremented by 1 as specified in the problem). Otherwise, output the string undecidable
.## sample
4
0 1
1 2 -1 -1
-1 -1 2 3
0 -1 -1 -1
-1 -1 -1 -1
1 1 1 0
2