2020CCPC网络赛 杭电 6898 3x3 Convolution(题解+代码)

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6898
题解: Cx,y计算公式其实是从 (x,y) 开始,最多扩大3个位置
然后两个矩阵对应位置相乘,取和即为结果

由于计算次数接近无限大,并且最后的结果保证为整数
那么我们知道对于每个位置 (x,y) 而言,如果k矩阵都为分数
那么一直乘之后,每个位置都会趋向于0,即最后结果为0
因为 分数*分数 ,最后的结果会越来越小

又由于k矩阵的结果总和为1
那么要想当前位置有贡献,那么 k1,1 必须为1
因为每个位置 本身 都是和 k1,1 相乘的
所以,只要 k1,1 为1,那么最后结果即为原来的矩阵

代码及注释如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<utility>
#include<algorithm>
#define ll long long
#define INF 0x3f3f3f3f
#define fi first
#define se second
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
const int MAXN = 1e3+5;
int a[MAXN][MAXN],k[5][5];
int main() {
    fast;
    int t;
    cin>>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];
            }
        }
        int cnt = 0;
        for(int i = 1;i <= 3;i++) {
            for(int j = 1;j <= 3;j++) {
                cin>>k[i][j];
                if(k[i][j]) cnt++;
            }
        }
        //如果只有一个位置有值,并且在位置(1,1),那么说明k1,1值为1
        int flag = cnt==1&&k[1][1];
        for(int i = 1;i <= n;i++) {
            for(int j = 1;j <= n;j++) {
                if(j!=1) cout<<" ";
                cout<<(flag?a[i][j]:0);//值为1则为原来数组,否则为0
            }
            cout<<"\n";
        }
    }
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值