题目链接 http://codeforces.com/problemset/problem/821/A
Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by nsquare grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every x, y such that 1 ≤ x, y ≤ n and ax, y ≠ 1, there should exist two indices s and t so that ax, y = ax, s + at, y, where ai, j denotes the integer in i-th row and j-th column.
Help Okabe determine whether a given lab is good!
The first line of input contains the integer n (1 ≤ n ≤ 50) — the size of the lab.
The next n lines contain n space-separated integers denoting a row of the grid. The j-th integer in the i-th row is ai, j (1 ≤ ai, j ≤ 105).
Print "Yes" if the given lab is good and "No" otherwise.
You can output each letter in upper or lower case.
3 1 1 2 2 3 1 6 4 1
Yes
3 1 5 2 1 1 1 1 2 3
No
题意不太好理解,就举一组测试数据说明题意吧。就按照题目的第一组测试数据:3 表示要输入3行3列的二维数组,数组中如果有非1的元素,比如说数组中的3:如果说元素3所在的行中某一个元素 和 元素3所在列中的某个元素中和正好等于3,则说明元素3是“好的”。如果说二维数组中的所有非1元素都是“好的”,则输出yes,否则输出No。
解题思路:
就是暴力,怎么想就怎么写。
特殊数据:
如果二维数组中元素全部为1,那么应该输出yes。
代码:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n,a[55][55];
while(cin>>n)
{
memset(a,1,sizeof(a));
int z,zz,s=0;
for(int i=0;i<=n-1;i++)
{
for(int j=0;j<=n-1;j++)
{
cin>>a[i][j];
if(a[i][j]!=1)
s=1;
}
}
if(s==0)
{
cout<<"Yes"<<endl;
continue;
}
for(int i=0;i<=n-1;i++)
{
int zzz=0;
for(int j=0;j<=n-1;j++)
{
if(a[i][j]!=1)
{
zzz=1;
z=0;
zz=1;
for(int p=0;p<=n-1;p++)
{
for(int q=0;q<=n-1;q++)
{
if(p==j||q==i)
continue;
if(a[i][p]+a[q][j]==a[i][j])
{
z=1;
break;
}
}
if(z==1)
break;
}
if(z==0)
{
zz=0;
break;
}
}
}
if(zz==0&&zzz==1)
break;
}
if(z==1)
cout<<"Yes"<<endl;
else
{
cout<<"No"<<endl;
}
}
return 0;
}