1 Uma imagem em preto e branco, de tamanho m x n, pode ser representada por uma matriz cujos elementos assumem valores no conjunto {0,1}. Dado um padrão representado por uma matriz 33 também assumindo valores em {0,1}, escreva uma função, ou trecho de código, que determine se o padrão existe ou não na imagem.
(8 pontos)
<code>
using System;
using System.IO;
namespace Testes
{
class Program
{
static void Main(string[] args)
{
CriaMatriz("A.txt", 200, 350);
CriaMatriz("B.txt", 3, 4);
int[,] A = LerMatriz("A.txt");
int[,] B = LerMatriz("B.txt");
Console.WriteLine(NumeroDeOcorrencias(A, B));
}
static int NumeroDeOcorrencias(int[,] matriz, int[,] padrão)
{
int N = matriz.GetLength(0), M = matriz.GetLength(1);
int n = padrão.GetLength(0), m = padrão.GetLength(1);
int contador = 0;
///////////////////////////////////////////////////////
//
// Escreva seu código aqui
//
///////////////////////////////////////////////////////
return contador;
}
static int[,] LerMatriz(string fileName)
{
FileStream f = new FileStream(fileName, FileMode.Open);
StreamReader sr = new StreamReader(f);
string[] tamanho = sr.ReadLine().Split(' ');
int n = int.Parse(tamanho[0]);
int m = int.Parse(tamanho[1]);
int[,] ret = new int[n,m];
for(int i=0; i<n; ++i)
{
string linha = sr.ReadLine();
for (int j = 0; j < m; ++j)
ret[i, j] = linha[j] == '0' ? 0 : 1;
}
sr.Close();
return ret;
}
static void CriaMatriz(string fileName, int n, int m)
{
Random r = new Random();
FileStream f = new FileStream(fileName, FileMode.Create);
StreamWriter sw = new StreamWriter(f);
sw.Write($"{n} {m}\r\n");
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
sw.Write(r.Next(2) == 0 ? '0' : '1');
sw.Write("\r\n");
}
sw.Close();
f.Close();
}
}
}
</code>