Polygon is not only the best platform for developing problems but also a square matrix with side nn, 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 2n2n cannons were placed.Initial polygon for n=4n=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 ii, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell (i,1i,1) and ends in some cell (i,ji,j);if a cannon stands in the column jj, above the first row, and shoots with a 1, then the 1 starts its flight from the cell (1,j1,j) and ends in some cell (i,ji,j).For example, consider the following sequence of shots:1. Shoot the cannon in the row 22. 2. Shoot the cannon in the row 22. 3. Shoot the cannon in column 33.You have a report from the military training on your desk. This report is a square matrix with side length nn 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.InputThe first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Then tt test cases follow.Each test case starts with a line containing an integer nn (1≤n≤501≤n≤50) — the size of the polygon.This is followed by nn lines of length nn, 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 105105.OutputFor 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.ExampleinputCopy5
4
0010
0011
0000
0000
2
10
01
2
00
00
4
0101
1111
0101
0111
4
0100
1110
0101
0111
outputCopyYES
NO
YES
YES
NO
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell (1,11,1) flying out of any cannon would continue its flight further.
#include <bits/stdc++.h>
using namespace std;
const int N = 55;
typedef pair<int, int> PII;
int n;
char g[N][N];
bool st[N][N];
int dx[2] = {0, -1}, dy[2] = {-1, 0};
queue<pair<int, int>> q;
bool bfs()
{
for(int i = 1; i <= n; i ++ ) if(g[i][n] == '1')
{
q.push({i, n});
st[i][n] = true;
}
for(int i = 1; i <= n; i ++ ) if(g[n][i] == '1')
{
q.push({n, i});
st[n][i] = true;
}
while(q.size())
{
PII t = q.front();
q.pop();
int x = t.first, y = t.second;
for(int i = 0; i < 2; i ++ )
{
int xx = x + dx[i], yy = y + dy[i];
if(xx < 1 || yy < 1) continue;
if(g[xx][yy] == '0') continue;
if(!st[xx][yy])
{
st[xx][yy] = true;
q.push({xx, yy});
}
}
}
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= n; j ++ )
{
if(g[i][j] == '0') continue;
if(!st[i][j])
return false;
}
return true;
}
int main()
{
int t;
cin >> t;
while(t --)
{
memset(st, 0, sizeof st);
cin >> n;
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= n; j ++ )
cin >> g[i][j];
bool flag = bfs();
if(flag) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}