按照题目意思,先枚举卷积核的起始位置,然后再枚举卷积核中的元素进行卷积运算,
这两个过程可以用两重循环来实现,嵌套起来,一共是四重循环。
#include<bits/stdc++.h>
using namespace std;
using i64=long long;
void solve(){
int n,m; cin>>n>>m;
vector<vector<i64>>a(n+1,vector<i64>(n+1));
i64 b[m+1][m+1];
i64 c[m+1][m+1];
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
cin>>a[i][j];
for(int i=1;i<=m;++i)
for(int j=1;j<=m;++j)
cin>>b[i][j];
for(int i = 1; i <= m - n + 1; ++ i)
for(int j = 1; j <= m - n + 1; ++ j) {
i64 tot = 0;
for(int x = 1; x <= n; ++ x)
for(int y = 1; y <= n; ++ y)
tot += a[x][y] * b[i + x - 1][j + y - 1];
c[i][j]=tot;
}
for(int i=1;i<=m-n+1;++i)
for(int j=1 ;j<=m-n+1;++j)
// cout<<c[i][j]<<"\n"[j==n-m+1];
cout << c[i][j] << " \n"[j == m - n + 1];
return ;
}
int main(){
int T=1;
while(T--)
solve();
return 0;
}