127 lines
2.9 KiB
C
127 lines
2.9 KiB
C
// Laboratorio 7 - Esercizio 1
|
|
// Matteo Schiff - s295565
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#define NC 50
|
|
#define NR 50
|
|
#define FILENAME "mappa.txt"
|
|
|
|
struct regione
|
|
{
|
|
int x, y, b, h;
|
|
};
|
|
|
|
void leggiMatrice(int mat[][NC], int maxR, int *nrp, int *ncp);
|
|
int riconosciRegione(int mat[][NC],int nr,int nc,int r,int c,int *b,int *h);
|
|
void analizzaRegioni(int mat[][NC],int nr,int nc);
|
|
|
|
void leggiMatrice(int mat[][NC], int maxR, int *nrp, int *ncp) {
|
|
FILE *fp_read;
|
|
|
|
fp_read = fopen(FILENAME, "r");
|
|
|
|
if (fp_read == NULL)
|
|
{
|
|
printf("Errore nell'apertura del file\n");
|
|
return;
|
|
}
|
|
|
|
fscanf(fp_read, "%d %d\n", nrp, ncp);
|
|
|
|
// Controlla dimensione matrice
|
|
if (*nrp < 0 || *ncp < 0 || *nrp > maxR || *ncp > NC) {
|
|
printf("Dimensione della matrice non valida\n");
|
|
fclose(fp_read);
|
|
return;
|
|
}
|
|
|
|
// Leggi matrice
|
|
for (int i = 0; i < *nrp; i++) {
|
|
for (int j = 0; j < *ncp; j++) {
|
|
fscanf(fp_read, "%d ", &mat[i][j]);
|
|
}
|
|
}
|
|
|
|
fclose(fp_read);
|
|
}
|
|
|
|
int riconosciRegione(int mat[][NC],int nr,int nc,int r,int c,int *b,int *h) {
|
|
int x, y;
|
|
|
|
// controlla se è un vertice
|
|
if (r > 0 && mat[r-1][c] == 1 || c > 0 && mat[r][c-1] == 1) {
|
|
return 0;
|
|
}
|
|
|
|
// misura estensione orizzontale
|
|
for (x = r; x < nr; x++) {
|
|
if (mat[x][c] != 1) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
// misura estensione verticale
|
|
for (y = c; y < nc; y++) {
|
|
if (mat[r][y] != 1) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
// calcola base ed altezza
|
|
*h = x-r;
|
|
*b = y-c;
|
|
|
|
return 1;
|
|
}
|
|
|
|
void analizzaRegioni(int mat[][NC],int nr,int nc) {
|
|
int b, h;
|
|
|
|
struct regione H, B, A;
|
|
|
|
int maxH = 0, maxB = 0, maxA = 0;
|
|
|
|
for (int r = 0; r < nr; r++){
|
|
for (int c = 0; c < nc; c++){
|
|
if (riconosciRegione(mat,nr,nc,r,c,&b,&h)) {
|
|
if (h > maxH) {
|
|
maxH = h;
|
|
H.x = r;
|
|
H.y = c;
|
|
H.b = b;
|
|
H.h = h;
|
|
}
|
|
|
|
if (b > maxB) {
|
|
maxB = b;
|
|
B.x = r;
|
|
B.y = c;
|
|
B.b = b;
|
|
B.h = h;
|
|
}
|
|
|
|
if (h*b > maxA) {
|
|
maxA = h*b;
|
|
A.x = r;
|
|
A.y = c;
|
|
A.b = b;
|
|
A.h = h;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
printf("Max altezza: estremo=(%d,%d), altezza=%d, larghezza=%d, area=%d\n", H.x, H.y, H.h, H.b, H.h * H.b);
|
|
printf("Max larghezza: estremo=(%d,%d), altezza=%d, larghezza=%d, area=%d\n", B.x, B.y, B.h, B.b, B.h * B.b);
|
|
printf("Max area: estremo=(%d,%d), altezza=%d, larghezza=%d, area=%d\n", A.x, A.y, A.h, A.b, A.h * A.b);
|
|
}
|
|
|
|
int main() {
|
|
int M[NR][NC];
|
|
int nr, nc;
|
|
|
|
leggiMatrice(M,NR,&nr,&nc);
|
|
analizzaRegioni(M, nr, nc);
|
|
return 0;
|
|
} |