Homogeneous Squares

Homogeneous Squares
Assume you have a square of size n that is divided into n × n positions just as a checkerboard. Two positions (x1, y1) and (x2, y2), where 1 ≤ x1, y1, x2, y2 ≤ n, are called “independent” if they occupy different rows and different columns, that is, x1 ≠ x2 and y1 ≠ y2. More generally, n positions are called independent if they are pairwise independent. It follows that there are n! different ways to choose n independent positions.

Assume further that a number is written in each position of such an n × n square. This square is called “homogeneous” if the sum of the numbers written in n independent positions is the same, no matter how the positions are chosen. Write a program to determine if a given square is homogeneous!

Input
The input contains several test cases.

The first line of each test case contains an integer n (1 ≤ n ≤ 1000). Each of the next n lines contains n numbers, separated by exactly one space character. Each number is an integer from the interval [−1000000, 1000000].

The last test case is followed by a zero.

Output
For each test case output whether the specified square is homogeneous or not. Adhere to the format shown in the sample output.

Sample Input
2
1 2
3 4
3
1 3 4
8 6 -2
-3 4 0
0
Sample Output
homogeneous
not homogeneous
假设您有一个大小为n的正方形,它被划分出n×n个位置,就像
一个棋盘。如果存在两个位置(x1,y1)和(x2,y2),其中
1≤x1,y1,x2,y2≤n,这两个位置占据不同的行和列,即x1≠x2并
且y1≠y2,则称两个位置是“独立的”。更一般地说,如果n个位
置两两间是独立的,则称这n个位置是独立的。因此有n!种不同的
选法选择n个独立的位置。
•设定在这样一个n×n的正方形的每个位置上都写有一个数。如果
不管位置如何选择,写在n个独立位置上的数的和相等,这个正
方形称为“homogeneous”。请您编写一个程序来确定一个给出
的正方形是否是“homogeneous”的。
输入
•输入包含若干个测试用例。
•每个测试用例的第一行给出一个整数n(1≤n≤1000)。接下来的n
行每行给出n个数字,数字之间用一个空格字符分隔。每个数字
都是在区间[−1000000,1000000]中的整数。
•在最后一个测试用例后面跟着一个0。
•输出
•对于每个测试用例,按样例输出中显示的格式,输出给出的正方
形是否“homogeneous”。

对于一个矩阵(方阵),要是其满足题述homogeneous得条件,则这个矩阵得任意一个二阶子方阵必须满足主、副对角线相加的数必须相等,即:若能找到任意一个子方阵对角线相加不等,则输出not homogeneous即可。

代码如下:

#include<iostream>
#include<cmath>
#include<string.h>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
#define N 100005
typedef long long ll;
int M[1005][1005];
int main(){
    int n;
    while(cin>>n&&n)
    {
        int f=0;
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        // cin>>M[i][j];        //不关掉同步流cin输入会超时,(关闭方法:std::iOS::sync_with_stdio(false))
        scanf("%d",&M[i][j]);

        for(int i=0;i<n-1;++i)
        {
            for(int j=0;j<n-1;++j)
            {
                if(M[i][j]+M[i+1][j+1]!=M[i][j+1]+M[i+1][j])
                {
                    f=1;
                    break;
                }
            }
            if(f) break;
        }
        if(!f)
        cout<<"homogeneous"<<endl;
        else cout<<"not homogeneous"<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值