#P15563. [CCPC 2025 哈尔滨站] 网格避障

[CCPC 2025 哈尔滨站] 网格避障

Problem Description

Given an n×mn \times m grid, the rows are numbered from 11 to nn, and the columns are numbered from 11 to mm. Except for the leftmost column 11 and the rightmost column mm, each column contains at most one obstacle. The leftmost column and the rightmost column are guaranteed to have no obstacles.

You start from any cell in the leftmost column (you may choose the row), and your goal is to reach any cell in the rightmost column (you may choose the row). Suppose you are currently at row ii, column jj. In each step, you may choose one of the following three operations:

  • Move right: from (i,j)(i,j) to (i,j+1)(i,j+1).
  • Move up: from (i,j)(i,j) to (i1,j)(i-1,j).
  • Move down: from (i,j)(i,j) to (i+1,j)(i+1,j).

Moving left is not allowed. At any time, you cannot enter an obstacle cell, and you also cannot move outside the grid.

There are kk obstacles (after sorting by column index in increasing order, they are numbered from i=0i=0 to k1k-1). For each obstacle, you must choose either “bypass from above” or “bypass from below”.

If the ii-th obstacle is at column cic_i and row rir_i:

  • If you choose “bypass from above”, then when you are in column cic_i, your row index must always be <ri< r_i.
  • If you choose “bypass from below”, then when you are in column cic_i, your row index must always be >ri> r_i.

Choices for different columns are independent, so there are 2k2^k possible plans.

Your task: for each plan, compute the minimum number of steps to go from some row in the leftmost column to some row in the rightmost column while satisfying all constraints of that plan. If there is no feasible path under that plan, output 1-1.

Input Format

This problem contains multiple test cases. The first line contains an integer TT (1T50001 \le T \le 5000), representing the number of test cases.

Then each test case is given as follows:

The first line contains two integers n,mn, m (1n100,2m1001 \leq n \leq 100, 2 \leq m \leq 100), representing the number of rows and columns of the grid.

The second line contains an integer kk (0kmin(m2,10)0 \leq k \leq \min(m - 2, 10)), representing the number of obstacles.

The next kk lines each contain two integers ri,cir_i, c_i (1rin,1cim1 \le r_i \le n, 1 \le c_i \le m), meaning there is an obstacle at row rir_i, column cic_i. It is guaranteed that 1<c1<c2<<ck<m1 < c_1 < c_2 < \ldots < c_k < m.

For all test cases, it is guaranteed that nm5000\sum n \cdot m \leq 5000. Note that there is no constraint on the sum of kk over all test cases.

Output Format

For each test case, output one line containing 2k2^k integers, in order, representing the minimum steps for each plan with id from 00 to 2k12^k-1. Adjacent numbers should be separated by a single space. If a plan has no solution, output 1-1.

Plan id description: each plan can be seen as a binary number of length kk. From low bit to high bit, the bits correspond to obstacles 0,1,...,k10, 1, ..., k-1. A bit value of 00 means bypass that obstacle from above, and a bit value of 11 means bypass that obstacle from below.

For example, if k=3k = 3, then there are 23=82^3 = 8 plans, corresponding to:

$$\begin{array}{|c|c|c|} \hline Plan\ ID & Binary\ (low\ bit \to high\ bit) & Bypass\ choice\ (obstacle\ 0,\ obstacle\ 1,\ obstacle\ 2) \\ \hline 0 & 000 & above,\ above,\ above \\ \hline 1 & 100 & below,\ above,\ above \\ \hline 2 & 010 & above,\ below,\ above \\ \hline 3 & 110 & below,\ below,\ above \\ \hline 4 & 001 & above,\ above,\ below \\ \hline 5 & 101 & below,\ above,\ below \\ \hline 6 & 011 & above,\ below,\ below \\ \hline 7 & 111 & below,\ below,\ below \\ \hline \end{array}$$

The program should output the minimum number of steps for each plan in increasing order of plan id.

3
3 6
2
3 4
2 5
3 4
1
1 2
3 6
2
3 2
1 5
5 -1 -1 -1
-1 3
-1 -1 5 -1

Hint

Translated by ChatGPT 5