题目描述
给输入一个 n₁×m₁ 的矩阵 a,和 n₂×m₂ 的矩阵 b,问 a 中是否存在子矩阵和 b 相等。若存在,输出所有子矩阵左上角的坐标:若不存在输出 "There is no answer"。
程序代码
#include<iostream>
using namespace std;
const int SIZE = 50;
int n1,m1,n2,m2,a[SIZE][SIZE],b[SIZE][SIZE];
int main()
{
int i,j,k1,k2;
bool good ,haveAns;
cin>>n1>>m1;
for(i=1;i<=n1;i++)
for(j=1;j<=m1;j++)
cin>>a[i][j];
cin>>n2>>m2;
for(i=1;i<=n2;i++)
for(j=1;j<=m2;j++)
①;
haveAns=false;
for(i=1;i<=n1-n2+1;i++)
for(j=1;j<= ②;j++){
③;
for(k1=1;k1<=n2;k1++)
for(k2=1;k2<= ④ ;k2++){
if(a[i+k1-1][j+k2-1]!=b[k1][k2])
good=false;
}
if(good){
cout<<i<<' '<<j<<endl;
⑤;
}
}
if(!haveAns)
cout<<"There is no answer"<<endl;
return 0;
}