#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章