Ciao, ti lascio a seguire la soluzione al tuo problema:
#include <iostream>
using namespace std;
#include <iomanip>
//---------------------------------------------------------------------------
const unsigned int N= 5;
const unsigned int M= 2;
//---------------------------------------------------------------------------
bool CercaSottoMatrice(bool App[N][N],
const unsigned int,
const unsigned int,
int A[N][N],
int B[M][M]);
unsigned int Occorrenze(int A[N][N],
int B[M][M],
bool App[N][N]);
int main(int argc, char* argv[])
{
//int A[N][N]={{2,1,2,3,2},{0,3,4,-5,10},{4,-5,7,1,2},{10,9,1,3,4},{8,8,8,8,8}};
//int A[N][N]={{2,1,2,3,2},{0,3,4,-5,10},{4,3,4,-5,7},{10,9,7,-9,0},{8,8,8,8,8}};
//int A[N][N]={{2,1,2,3,2},{0,3,4,-5,10},{4,-5,7,1,2},{1,2,1,3,4},{3,4,8,8,8}};
//int B[M][M]={{1,2},{3,4}};
int A[N][N]={{1,0,0,1,1},{1,0,0,1,1},{1,0,0,0,0},{1,0,0,0,0},{1,1,1,1,1}};
int B[M][M]={{0,0},{0,0}};
unsigned int i,
j;
bool App[N][N];
cout <<"La matrice A:\n";
for (i= 0; i<N; i++)
{
for (j= 0; j<N; j++)
cout <<setw(6) <<A[i][j];
cout <<"\n";
}
cout <<"La matrice B:\n";
for (i= 0; i<M; i++)
{
for (j= 0; j<M; j++)
cout <<setw(6) <<B[i][j];
cout <<"\n";
}
for (i= 0; i<N; i++)
for (j= 0; j<N; j++)
App[i][j]= false;
cout <<"Il numero di occorrenza di B in A e': " <<Occorrenze(A,B,App) <<"\n";
cout <<"La matrice App:\n";
for (i= 0; i<N; i++)
{
for (j= 0; j<N; j++)
cout <<setw(6) <<App[i][j];
cout <<"\n";
}
system ("PAUSE");
return 0;
}
//---------------------------------------------------------------------------
bool CercaSottoMatrice(bool App[N][N],
const unsigned int r,
const unsigned int c,
int A[N][N],
int B[M][M])
{
unsigned int i,
j;
bool fine;
unsigned int count;
bool found;
i= 0;
fine= false;
count= 0;
found= false;
while ((!fine) && (i<M))
{
j= 0;
while ((!fine) && (j<M))
{
if ((!App[(r+i)][(c+j)]) && (A[(r+i)][(c+j)]==B[i][j]))
count++;
else
fine= true;
j++;
}
i++;
}
if (count==(M*M))
{
found= true;
for (i= 0; i<M; i++)
for (j= 0; j<M; j++)
App[(r+i)][(c+j)]= true;
}
return found;
}
//---------------------------------------------------------------------------
unsigned int Occorrenze(int A[N][N],
int B[M][M],
bool App[N][N])
{
int elem;
unsigned int count,
i,
j;
elem= B[0][0];
count= 0;
for (i= 0; i<=(N-M); i++)
for (j= 0; j<=(N-M); j++)
if (A[i][j]==elem)
if (CercaSottoMatrice(App,i,j,A,B))
count++;
return count;
}
Questa non è certamente la soluzione più brillante ma assolve allo scopo.
Spero d'esserti stato utile.
Buona serata.
[/color]