Codeforces Round #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.
  2. Shoot the cannon in the row 2.
  3. Shoot the cannon in column 3.

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 1e5.

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

比较简单的思维题,对一个合法的位置,有且仅有一下几种情况:

  • 它的下方和右方有1
  • 它在最下方或者最右方

AC代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
char g[100][100];
int judge(int x,int y){if(x+1<n && g[x+1][y]=='1') return 1;if(y+1<n && g[x][y+1]=='1') return 1;if(x==n-1 || y==n-1) return 1;return 0;
}
main(){int t;cin>>t;while(t--){cin>>n;int flag=1;for(int i=0;i<n;i++){for(int j=0;j<n;j++)cin>>g[i][j];}for(int i=0;i<n;i++){for(int j=0;j<n;j++){if(g[i][j]=='1'){if(judge(i,j)==0) flag=0;}}}if(flag) puts("YES");else puts("NO");}
}

Codeforces Round #644 (Div. 3) E.Polygon相关推荐

  1. Codeforces Round #644 (Div. 3) D.Buying Shovels

    Codeforces Round #644 (Div. 3) D.Buying Shovels 题目链接 Polycarp wants to buy exactly n shovels. The sh ...

  2. Codeforces Round #644 (Div. 3) F.Spy-string

    Codeforces Round #644 (Div. 3) F.Spy-string 题目链接 You are given n strings a1,a2,-,an: all of them hav ...

  3. Codeforces Round #644 (Div. 3) G.A/B Matrix

    Codeforces Round #644 (Div. 3) G.A/B Matrix 题目链接 You are given four positive integers n, m, a, b (1≤ ...

  4. Codeforces Round #644 (Div. 3)(A-E)

    这场的A-E都是水题,就简单记录一下吧. Minimal Square CodeForces - 1360A 思路:我们令b=max(a,b),a=min(a,b). 如果b>=2*a的话,最终 ...

  5. Codeforces Round #644(Div. 3) A-H

    A - Minimal Square 题意 给两个完全一样的矩形(平行且不重叠) 求能覆盖两个矩形的最小正方形的面积 思路 只有两种摆放方式 将两个矩形上下并列或者左右并列 得到的新图形 长或者宽是之 ...

  6. Codeforces Round #644 (Div. 3) H.Binary Median

    题目链接 Consider all binary strings of length m (1≤m≤60). A binary string is a string that consists of ...

  7. Codeforces Round #198 (Div. 2)A,B题解

    Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...

  8. Codeforces Round 250(Div. 2)

    layout: post title: Codeforces Round 250 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  9. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

最新文章

  1. UA OPTI544 量子光学14 量子电动力学基础
  2. IBM技术大会2005
  3. Windows 下音频数据采集和播放
  4. 计算机组成原理txt,计算机组成原理.TXT(Computer composition principle. TXT).doc
  5. 快速修改本機局域網IP信息
  6. Maya: Motion Graphics Workflow with MASH Maya教程:运动图形工作流程与MASH Lynda课程中文字幕
  7. IDEA几款不错的基于Darcula的深色主题
  8. c语言小游戏编程弹珠游戏,c语言 弹弹球小游戏
  9. MATLAB-数据插值
  10. 人工智能如何被用于造福人类?
  11. mt6573芯片资料汇总
  12. 【Python】base64解码报错 Incorrect padding
  13. uni-app上传安卓应用市场的坑
  14. sql 内连接,左连接,右连接,全连接
  15. 如何换主题不被搜索降权
  16. 基于vmware16 和 ubuntu20.04, 搭建单节点 kubernetes 1.22.2
  17. 目前最火的人工神经网络,神经网络未来发展趋势
  18. 电力监控系统在智能建筑电气节能中的应用
  19. 百度地图java sdk_百度地图-地图SDK常用方法总结(一)
  20. UE4消除后期对透明材质的影响

热门文章

  1. 安装部署WSUS服务器
  2. size和sizeof的区别
  3. 出差忘带电脑脑袋炸裂?鼓捣了下个人云,真香
  4. Web全栈开发1+x(中级)PHPMySQL知识
  5. 计算机专业jsp项目,可练手
  6. win10下完美卸载ubuntu
  7. 简单使用SAXReader解析xml数据
  8. 利用OpenGL设计贪吃蛇游戏
  9. mybatis中resultMap和resultType的详细用法
  10. 人工智能 - A*算法解决迷宫问题 附源码和可视化显示