牛客第四场 D Another Distinct Values

链接:https://www.nowcoder.com/acm/contest/142/D
来源:牛客网
 

题目描述

Chiaki has an n x n matrix. She would like to fill each entry by -1, 0 or 1 such that r1,r2,...,rn,c1,c2, ..., cn are distinct values, where ri be the sum of the i-th row and ci be the sum of the i-th column.

输入描述:

There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 200), indicating the number of test cases. For each test case:
The first line contains an integer n (1 ≤ n ≤ 200) -- the dimension of the matrix.

输出描述:

For each test case, if no such matrix exists, output ``impossible'' in a single line. Otherwise, output ``possible'' in the first line. And each of the next n lines contains n integers, denoting the solution matrix. If there are multiple solutions, output any of them.

示例1

输入

复制

2
1
2

输出

复制

impossible
possible
1 0
1 -1

题目大意:用0 1 -1 构造一个n*n的矩阵使每行每列的值都不相同。

思路:n*n的矩阵 可以用0 1 -1 构造出2*n+1个值 无论怎么选端点的值一定能够取到。

先把每行都赋值为1或-1呈三角形,再挑出位置赋值0

#include<iostream>
#include<cstdio>
using namespace std;
#define sca(x) scanf("%d",&x)
#define rep(i,j,k) for(int i=j;i<=k;i++)
int a[205][205];
int main()
{
    int t;
    sca(t);
    while(t--)
    {
        int n;sca(n);
        if(n&1)printf("impossible\n");
        else
        {
            printf("possible\n");
            rep(i,1,n)
            rep(j,1,n)
            {
                if(j<=i)a[i][j]=1;
                else a[i][j]=-1;
                if(j==i&&i&1)a[i][j]=0;
            }
            rep(i,1,n)
            rep(j,1,n)
            {
                printf("%d%c",a[i][j],j==n?'\n':' ');
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40894017/article/details/81267864