#P15503. [ICPC 2025 APC] Boarding Queue
[ICPC 2025 APC] Boarding Queue
Description
You are on your way to compete in The 2025 ICPC Asia Pacific Championship. Unfortunately, you are in the process of the worst part of flying: waiting in the boarding queue.
You are in a queue with travelers, numbered from to ordered from the front of the queue to the back of the queue. The boarding area is represented by a grid of rows and columns, where the rows are numbered from to (top to bottom) and the columns are numbered from to (left to right). Each traveler occupies exactly one distinct cell in the grid. Two travelers are adjacent if the cells they are in share an edge. Traveler and traveler are guaranteed to be adjacent, for any .
For example, Figure L.1 illustrates a possible location of the travelers. In this example, traveler is adjacent to travelers and but not adjacent to traveler .
:::align{center}

Figure L.1: Example location of the travelers. :::
At each boarding step, all of the following happen simultaneously:
- The frontmost traveler in the queue, say traveler , boards the aircraft and leaves the boarding area.
- For each (), traveler takes the cell that traveler was occupying immediately before the step.
:::align{center}

Figure L.2: Location of the travelers after 1, 2, and 3 boarding steps respectively. :::
For example, Figure L.2 illustrates the locations of the travelers after the first three boarding steps from the initial location above.
You are traveler (that is, there are travelers in front of you). You know that your team coach is somewhere in the queue, but you do not know where. Assuming your team coach is equally likely to be any of travelers to (except ), you want to calculate the probability that you will be adjacent to your team coach at some point before you board the aircraft. Formally, you will be adjacent to traveler at some point before you board the aircraft if there exists an integer () such that traveler and traveler are adjacent after boarding steps.
Input Format
The first line of input contains four integers , , , and (; ; ). Each of the next lines contains integers. The -th integer in the -th line denotes (), where a non-zero of means that traveler initially occupies the cell at row and column of the boarding area, while a zero value of means that no traveler occupies the cell. Across all pairs , each of the integers to appears exactly once in . The input guarantees that traveler and traveler are adjacent, for all .
Output Format
Output a fraction in the format indicating the probability that you will be adjacent to your team coach at some point before you board the aircraft. The value of must be equal to . Note that there must not be spaces between the integers and the / delimiter.
4 5 11 2
11 0 0 0 0
10 1 2 0 0
9 0 3 4 0
8 7 6 5 0
3/10
1 7 7 6
1 2 3 4 5 6 7
2/6
Hint
Explanation for the sample input/output #1
This sample corresponds to the example given in the problem description above. You will be adjacent to your team coach at some point before you board the aircraft if your team coach is either traveler , , or .
Explanation for the sample input/output #2
You will be adjacent to your team coach at some point before you board the aircraft if your team coach is either traveler or . Note that the output is not accepted.
京公网安备 11011102002149号