#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ROWS 7
#define MAX_COLS 9
#define PATTERN_SIZE 3
#define MAX_PATTERNS 100
typedef struct {
char pattern[32];
int rows[MAX_ROWS];
int count;
} Pattern;
void findRepeatingPatterns(int matrix[MAX_ROWS][MAX_COLS], int rows, int cols, int patternSize) {
Pattern patterns[MAX_PATTERNS];
int patternCount = 0;
for (int row = 0; row < rows; row++) {
for (int col = 0; col <= cols - patternSize; col++) {
char pattern[32] = "";
char temp[8];
for (int i = 0; i < patternSize; i++) {
sprintf(temp, "%d-", matrix[row][col + i]);
strcat(pattern, temp);
}
int found = 0;
for (int i = 0; i < patternCount; i++) {
if (strcmp(patterns[i].pattern, pattern) == 0) {
patterns[i].rows[patterns[i].count++] = row;
found = 1;
break;
}
}
if (!found) {
strcpy(patterns[patternCount].pattern, pattern);
patterns[patternCount].rows[0] = row;
patterns[patternCount].count = 1;
patternCount++;
}
}
}
printf("Repeated Patterns Found:\n");
for (int i = 0; i < patternCount; i++) {
if (patterns[i].count > 1) {
printf("%s appears %d times in rows: ", patterns[i].pattern, patterns[i].count);
for (int j = 0; j < patterns[i].count; j++) {
printf("%d ", patterns[i].rows[j]);
}
printf("\n");
}
}
}
int main() {
int matrix[MAX_ROWS][MAX_COLS] = {
{1, 2, 3, 8, 9, 7, 4, 9, 6},
{1, 3, 2, 7, 8, 9, 4, 5, 6},
{1, 2, 3, 8, 6, 1, 4, 9, 8},
{1, 2, 3, 0, 8, 8, 4, 5, 9},
{1, 2, 3, 4, 5, 6, 7, 8, 9},
{1, 2, 3, 7, 0, 9, 4, 5, 7},
{1, 3, 2, 4, 5, 6, 7, 8, 9}
};
findRepeatingPatterns(matrix, MAX_ROWS, MAX_COLS, PATTERN_SIZE);
return 0;
}
/*
run:
Repeated Patterns Found:
1-2-3- appears 5 times in rows: 0 2 3 4 5
2-3-8- appears 2 times in rows: 0 2
1-3-2- appears 2 times in rows: 1 6
7-8-9- appears 3 times in rows: 1 4 6
9-4-5- appears 2 times in rows: 1 5
4-5-6- appears 3 times in rows: 1 4 6
5-6-7- appears 2 times in rows: 4 6
6-7-8- appears 2 times in rows: 4 6
*/