#644 (Div. 3)E. Polygon(思维)

题目描述

Polygon is not only the best platform for developing problems but also a square matrix with side n, initially filled with the character 0.
On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly 2n cannons were placed.
在这里插入图片描述
Initial polygon for n=4.
Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.
More formally:
if a cannon stands in the row i, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell (i,1) and ends in some cell (i,j);
if a cannon stands in the column j, above the first row, and shoots with a 1, then the 1 starts its flight from the cell (1,j) and ends in some cell (i,j).
For example, consider the following sequence of shots:
在这里插入图片描述1. Shoot the cannon in the row 2. Shoot the cannon in the row 3. Shoot the cannon in column
You have a report from the military training on your desk. This report is a square matrix with side length n consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?
Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.

Input

The first line contains an integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.
Each test case starts with a line containing an integer n (1≤n≤50) — the size of the polygon.
This is followed by n lines of length n, consisting of 0 and 1 — the polygon matrix after the training.
The total area of the matrices in all test cases in one test does not exceed 105.

Output

For each test case print:
YES if there is a sequence of shots leading to a given matrix;
NO if such a sequence does not exist.
The letters in the words YES and NO can be printed in any case.

Example

input
5
4
0010
0011
0000
0000
2
10
01
2
00
00
4
0101
1111
0101
0111
4
0100
1110
0101
0111
output
YES
NO
YES
YES
NO

Note

The first test case was explained in the statement.
The answer to the second test case is NO, since a 1 in a cell (1,1) flying out of any cannon would continue its flight further.

题目大意

给你一个n*n的方阵,每行每列都有一个大炮。
最开始,方阵中全是0,炮弹发射数字1的炮弹。但是现在如果这个炮弹会一直向前发现,直到越到前面是方阵的边界后停下,或者他的前面一个数是1,则也停下。(如上图)
现在给你一个方阵中的情况,让你判断这个方阵是否可能由上面的条件所形成。

题目分析

这个题看起来挺吓人的。但其实并不是很难。
在方阵中,某一个大炮发射了一个炮弹,如果该炮弹一路上没有阻碍,那它会一直打到边界上。
如果一个炮弹没有打到边界上,那肯定是因为在该炮弹的路线上有障碍。
假设一个炮弹停在了a[i][j]位置上,那么a[i+1][j]或a[i][j+1]这两个位置上必定有一个位置上有炮弹。不然该炮弹不可能停留在该位置上。
因此我们只需要验证每一个非边界且有炮弹的位置上的下边或右边有没有炮弹即可。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <unordered_map>
#include <queue>
#include <vector>
#include <set> 
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
int const N=105;
char a[N][N];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		cin>>a[i][j];
		//验证每一个非边界且有炮弹的位置上的下边或右边有没有炮弹
		bool st=true;
		for(int i=1;i<n;i++)     //不用枚举边界
		for(int j=1;j<n;j++)
		if(a[i][j]=='1')
		{
			if(a[i+1][j]=='0'&&a[i][j+1]=='0') 
			{
				st=false;
				break;
			}
		}
		
		if(st) puts("YES");
		else puts("NO");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/li_wen_zhuo/article/details/106327632