codeforces round928-B

Vladislav has a binary square grid of n × n n \times n n×n cells. A triangle or a square is drawn on the grid with symbols 1 \texttt{1} 1. As he is too busy being cool, he asks you to tell him which shape is drawn on the grid.

  • A triangle is a shape consisting of k k k (KaTeX parse error: Expected 'EOF', got '&' at position 2: k&̲gt;1) consecutive rows, where the i i i-th row has 2 ⋅ i − 1 2 \cdot i-1 2i1 consecutive characters 1 \texttt{1} 1, and the central 1s are located in one column. An upside down triangle is also considered a valid triangle (but not rotated by 90 degrees).

Two left pictures contain examples of triangles: k = 4 k=4 k=4, k = 3 k=3 k=3. The two right pictures don’t contain triangles.

  • A square is a shape consisting of k k k (KaTeX parse error: Expected 'EOF', got '&' at position 2: k&̲gt;1) consecutive rows, where the i i i-th row has k k k consecutive characters 1 \texttt{1} 1, which are positioned at an equal distance from the left edge of the grid.

Examples of two squares: k = 2 k=2 k=2, k = 4 k=4 k=4.

For the given grid, determine the type of shape that is drawn on it.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 1t100) — the number of test cases.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 10 2 \leq n \leq 10 2n10) — the size of the grid.

The next n n n lines each contain n n n characters 0 \texttt{0} 0 or 1 \texttt{1} 1.

The grid contains exactly one triangle or exactly one square that contains all the 1 \texttt{1} 1s in the grid. It is guaranteed that the size of the triangle or square is greater than 1 1 1 (i.e., the shape cannot consist of exactly one 1).

Output

For each test case, output “SQUARE” if all the 1 \texttt{1} 1s in the grid form a square, and “TRIANGLE” otherwise (without quotes).

此题可以记录每一行的1的数量,因为正方形的每一行的1的数量都是一样的,而三角形的就不一样。

流加速千万不能写到循环里面,BYD写进去就错了,我刚刚才知道。

#include<iostream>
#include<cstring>
using namespace std;
const int N = 15;

char g[N][N];
int hang[N];
int n;
 
int main() {
	ios::sync_with_stdio(false);
    cin.tie(0);
    
    int t; cin >> t;
    
    while (t--) {
        memset(hang,0,sizeof hang);
        
 
        cin >> n;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                cin >> g[i][j];
            }
        }
        bool flag= 0;
 
        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= n;j++){
                if(g[i][j] == '1'){
                    hang[i]++;
                }
            }
            if(hang[i] == hang[i-1] && hang[i] != 0){
                flag = 1;
                break;
            }
        }
 
        if (flag) {
            cout << "SQUARE" << endl;
        }
        else {
            cout << "TRIANGLE" << endl;
        }
    }
    return 0;
}
  • 11
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值