您的位置:首页 > 其它

Okabe and Future Gadget Laboratory

2018-01-16 22:03 423 查看
Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square 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 tso 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!
InputThe 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).
OutputPrint "Yes" if the given lab is good and "No" otherwise.
You can output each letter in upper or lower case.
ExampleInput
3
1 1 2
2 3 1
6 4 1
Output
Yes
Input
3
1 5 2
1 1 1
1 2 3
Output
No
NoteIn the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is "Yes".
In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer is "No".

#include<stdio.h>
int main()
{
int n,i,j,a[55][55],s,t,x,y,flag;
while(scanf("%d",&n)!=EOF)
{
s=t=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i][j]!=1)
s++;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
flag=1;
for(x=0;x<n&&flag;x++)
{
if(x!=j)
for(y=0;y<n&&flag;y++)
if(y!=i)
if((a[y][j]+a[i][x])==a[i][j])
{
flag=0;
t++;
}
}
}
if(s==t)
printf("YES\n");
else
printf("NO\n");
}
return 0;
 } 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: