HDU 6898 3x3 Convolution

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>0

 

Output

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("");
			}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Double.Qing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值