Codeforces Round 1012 (Div. 2)
B
Pushing Balls
Codeforces
1000
原题
← AC →
翻译中…

Ecrade has an $n \times m$ grid, originally empty, and he has pushed several (possibly, zero) balls in it.

翻译中…

Each time, he can push one ball into the grid either from the leftmost edge of a particular row or the topmost edge of a particular column of the grid.

翻译中…

When a ball moves towards a position:

翻译中…
  • If there is no ball originally at that position, the incoming ball will stop and occupy the position.
  • If there is already a ball at that position, the incoming ball will stop and occupy the position, while the original ball will continue moving to the next position in the same direction.
翻译中…

Note that if a row or column is full (i.e., all positions in that row or column have balls), he cannot push a ball into that row or column.

翻译中…

Given the final state of whether there is a ball at each position of the grid, you need to determine whether it is possible for Ecrade to push the balls to reach the final state.

翻译中…

Input

翻译中…

The first line contains an integer $t$ ($1 \le t \le 10\,000$) — the number of test cases. The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 50$).

翻译中…

This is followed by $n$ lines, each containing exactly $m$ characters and consisting only of $0$ and $1$, describing the final state of the grid. There is a ball at one position of the grid if and only if the corresponding position of the given input is $1$.

翻译中…

It is guaranteed that the sum of $n\cdot m$ over all test cases does not exceed $10\,000$.

翻译中…

Output

翻译中…

For each test case, output "Yes" (without quotes) if it is possible for Ecrade to push the balls to reach the final state, and "No" (without quotes) otherwise.

翻译中…

You can output "Yes" and "No" in any case (for example, strings "YES", "yEs" and "yes" will be recognized as a positive response).

翻译中…

Note

翻译中…

For simplicity, if Ecrade pushes a ball from the leftmost edge of the $i$-th row, we call the operation $\text{ROW}\ i$; if he pushes a ball from the topmost edge of the $i$-th column, we call the operation $\text{COL}\ i$.

翻译中…

For intuitive understanding, a non-zero number $x$ in the matrix given below represents the $x$-th ball that is pushed in.

翻译中…

In the first test case, a possible series of operations:

翻译中…

$\begin{pmatrix}0&0&0\\0&0&0\\0&0&0\end{pmatrix}\xrightarrow{\text{ROW}\ 3}\xrightarrow{\text{ROW}\ 3} \begin{pmatrix}0&0&0\\0&0&0\\2&1&0\end{pmatrix}\xrightarrow{\text{COL}\ 3}\xrightarrow{\text{COL}\ 3} \begin{pmatrix}0&0&4\\0&0&3\\2&1&0\end{pmatrix}$

翻译中…

In the second test case, a possible series of operations:

翻译中…

$\begin{pmatrix}0&0&0\\0&0&0\\0&0&0\end{pmatrix}\xrightarrow{\text{ROW}\ 2}\xrightarrow{\text{ROW}\ 2}\xrightarrow{\text{ROW}\ 2} \begin{pmatrix}0&0&0\\3&2&1\\0&0&0\end{pmatrix}\xrightarrow{\text{COL}\ 2}\xrightarrow{\text{COL}\ 2} \begin{pmatrix}0&5&0\\3&4&1\\0&2&0\end{pmatrix}$

翻译中…

In the third test case, a possible series of operations:

翻译中…

$\begin{pmatrix}0&0&0\\0&0&0\\0&0&0\end{pmatrix}\xrightarrow{\text{ROW}\ 1}\xrightarrow{\text{ROW}\ 2}\xrightarrow{\text{ROW}\ 3} \begin{pmatrix}1&0&0\\2&0&0\\3&0&0\end{pmatrix}\xrightarrow{\text{COL}\ 3}\xrightarrow{\text{COL}\ 3}\xrightarrow{\text{COL}\ 3} \begin{pmatrix}1&0&6\\2&0&5\\3&0&4\end{pmatrix}\xrightarrow{\text{ROW}\ 1}\xrightarrow{\text{ROW}\ 2}\xrightarrow{\text{ROW}\ 3} \begin{pmatrix}7&1&6\\8&2&5\\9&3&4\end{pmatrix}$

翻译中…

样例

Input #1
5
3 3
001
001
110
3 3
010
111
010
3 3
111
111
111
3 3
000
000
000
3 3
000
000
001
Output #1
YES
YES
YES
YES
NO
InputCopy OutputCopy
题目信息
题号
2090B
时间限制
1 s
内存限制
512 MB
标签
暴力
动态规划
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。