1204#POJ2941 Homogeneous

摘要
-给定大小为n的方阵,判断其其是否满足给定的条件,条件为 任选不同行列的n个数,他们的和始终相同。
原题目摘要
- Homogeneous Squares
http://poj.org/problem?id=2941

-

Description

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, y2n, are called “independent” if they occupy different rows and different columns, that is,x1x2 and y1y2. More generally, n positions are called independent if they are pairwise independent. It follows that there aren! 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 inn 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 nextn 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-1个数仍然要满足要求才行,这样想想 就剩下最后的4个方块,那么剩下的这四个方块又可能是任意的,如果是相邻的呢? 如下图 黄圈和绿圈都满足要求(a+f==e+b,b+g==c+f),看看这东西好像具有传递性 也就是可以得到 a+g==e+c;这样就是任意的四个格子的满足了;

注意
-
日期
-2017 12 4 
附加
-
代码

#include <algorithm>
#include <cstdio>
#include <iostream>
#define MAX 1005
using namespace std;

int n;
int M[MAX][MAX];

bool solve(){
    bool state = true;
    for(int i=0;state&&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]){
                state = false;
                break;
            }
        }
    }
    return state;
}

int main(){
    freopen("in","r",stdin);
    while(scanf("%d",&n)&&n){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++)
                scanf("%d",&M[i][j]);
        }
        printf("%s\n",solve()?"homogeneous":"not homogeneous");
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值