Problem Description
Given an n×n matrix A and a 3×3 matrix K. These two matrices are very special : they are both non-negative matrices and the sum of all elements in matrix K is 1 (In order to avoid floating-point error, we will give matrix K in a special way in input).
Now we define a function C(A,K), the value of C(A,K) is also a n×n matrix and it is calculated below(we use C to abbreviate C(A,K)):
Cx,y=∑min(n−x+1,3)i=1∑min(n−y+1,3)j=1Ax+i−1,y+j−1Ki,j
Now we define Cm(A,K)=C(Cm−1(A,K),K) and C1(A,K)=C(A,K), Kanade wants to know limt→∞Ct(A,K)
It's guaranteed that the answer exists and is an integer matrix.Input
There are T test cases in this problem.
The first line has one integer T.
Then for every test case:
The first line has one integer n.
Then there are n lines and each line has n non negative integers. The j-th integer of the i-th row denotes Ai,j
Then there are 3 lines and each line has 3 non negative integers. The j-th integer of the i-th row denotes K′i,j
Then K could be derived from K′ by the following formula:Ki,j=K′i,j/(∑x=13∑y=13K′x,y)
1≤T≤100
3≤n≤50
0≤Ai,j≤1000
0≤K′i,j≤1000
∑3x=1∑3y=1K′x,y>0Output
For each test case, output the answer matrix by using the same format as the matrix A in input.
Sample Input
2 3 1 2 3 4 5 6 7 8 9 3 0 0 0 0 0 0 0 0 3 1 2 3 4 5 6 7 8 9 1 0 0 0 1 0 0 0 0
Sample Output
1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0
分析:如果矩阵k只有一个非零元且位于k[1][1],那么矩阵a将没有变化,否则无限次运算后a终将被0覆盖
AC代码
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=55;
int a[N][N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>a[i][j];
bool f=0;
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
{
int a;
cin>>a;
if(a)
{
if(i==1&&j==1)
f=1;
else
f=0;
}
}
if(f)
for(int i=1;i<=n;i++)
{
printf("%d",a[i][1]);
for(int j=2;j<=n;j++)
printf(" %d",a[i][j]);
puts("");
}
else
for(int i=1;i<=n;i++)
{
printf("0");
for(int j=2;j<=n;j++)
printf(" 0");
puts("");
}
}
}