Is it a fantastic matrix?
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 5 Accepted Submission(s) : 2
Problem Description
Given an n*m matrix, you are asked to judge if it's fantastic. The rule is: firstly, choose min (n, m) numbers from it, meanwhile you should make sure any two numbers you select will not at the same row or same column. Of course, you may have many different
ways to get the numbers. For every way, if the sum of all selected numbers is always same, you may say the matrix is fantastic.
Input
The first line contains an integer T, stands for the number of test cases. (1<=T<=100)
T cases follow, for every case:
The first line contains two integers n and m. (1 <= n, m <= 50)
Then n lines follows, each line contains m integers. Every number in the matrix will between -100000 and 100000.
T cases follow, for every case:
The first line contains two integers n and m. (1 <= n, m <= 50)
Then n lines follows, each line contains m integers. Every number in the matrix will between -100000 and 100000.
Output
For every case, if it is a fantastic matrix, output “YES” in one line, otherwise output “NO”.
Sample Input
3 2 2 1 3 2 4 2 3 1 1 1 1 1 1 2 1 2 3
Sample Output
YES YES NO
Source
hujie 测试专用(2)
这个题当初就思考了很久 可惜还是没有考虑 周全
当n<m的时候 每一行都必须相同
如
1 1 1 1
2 2 2 2
3 3 3 3
如果不相同显然反例可证明
n>m 时候同理
当时只考虑了n==m的情况
得到了等式 a[k][jk]+a[t][jt]=a[k][jt]+a[t][jk]; (利用行列式的方式描述一个元素的位置 a[1][j1])
当k=1 ,jk=1 的时候 a[1][1]+a[t][jt]=a[1][jt]+a[t][1];
当k=1 ,jk=1 的时候 a[1][1]+a[t][jt]=a[1][jt]+a[t][1];
同时可以写出另外3个
a[k][jk]+a[1][1]=a[k][1]+a[1][jk]
a[1][jk]+a[t][1]=a[1][1]+a[t][jk]
a[k][1]+a[1][jt]=a[k][jt]+a[1][1]
累加起来即为 a[k][jk]+a[t][jt]=a[k][jt]+a[t][jk]
所以我们只需判断 a[x][y]+a[1][1]==a[x][1]+a[1][y]即可
代码如下:
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <ctime> #include <algorithm> #include <iostream> #include <sstream> #include <string> #define oo 0x13131313 using namespace std; int MAP[60][60]; int main() { int T; cin>>T; while(T--) { int OK=1; int m,n; cin>>n>>m; if(n>m) //懒得写 if(n<m) //懒得写 for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&MAP[i][j]); if(n==1||m==1) { printf("NO ");continue;} for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(MAP[i][j]+MAP[1][1]!=MAP[1][j]+MAP[i][1]) OK=0; if(OK) cout<<"YES"<<endl; else cout<<"NO"<<endl; } }