---
# System prepended metadata

title: '題解 zerojudge h027: 202001_2 矩陣總和 APCS'
tags: [APCS, c++]

---

###### tags: `APCS` `c++`
# 題解 zerojudge h027: 202001_2 矩陣總和 APCS
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main(){
	int s,t,n,m,r;
	cin>>s>>t>>n>>m>>r;
	int A[101][101],B[101][101];
	int sumA=0;
	for(int i=0;i<s;i++){
		for(int j=0;j<t;j++)	{
			cin>>A[i][j];
			sumA+=A[i][j];
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++)	cin>>B[i][j];
	}
	
	int cnt=0,minsum=1000000;
	for(int i=0;i<=n-s;i++){
		for(int j=0;j<=m-t;j++){
			int sumB=0,d=0;
			for(int k=0;k<s;k++){
				for(int q=0;q<t;q++){
					if(A[k][q]!=B[i+k][j+q])	d++;
					sumB+=B[i+k][j+q];
				}
			}
			if(d<=r){
				cnt++;
				minsum=min(minsum,abs(sumB-sumA));
			}
		}
	}
	if(cnt!=0){
		cout<<cnt<<"\n";
		cout<<minsum<<"\n";
	}
	else{
		cout<<"0\n";
		cout<<"-1\n";
	}
}

```