1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <stdio.h>
void affiche(int tableau[10][10])
{int i=0, j=0;
printf("\n");
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
printf("%4d",tableau[i][j]);
printf("\n");
}
printf("\n");}
void clustering(int tableau[10][10])
{int groupe_actuel=2, i=0, j=0, groupe, groupe_old, i2, j2, temp;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
if (tableau[i][j]==1)
{groupe=0;
if( (j>0)&&(i>0)&&(tableau[i][j-1]>1)&&(tableau[i-1][j]>1)&&(tableau[i-1][j]!=tableau[i][j-1]) )
{groupe_old=tableau[i][j-1];
groupe=tableau[i-1][j];
for(j2=0;j2<10;j2++)
if( tableau[i2][j2]==groupe_old)
tableau[i2][j2]=groupe;}
if(i>0)
{if( tableau[i-1][j]>0 )
groupe=tableau[i][j-1];}
if(j>0)
{if(tableau[i][j-1]>0)
groupe=tableau[i][j-1];}
if(groupe==0)
{groupe=groupe_actuel;
groupe_actuel++;}
tableau[i][j]=groupe;
}
}
i2=2;
j2=1;
temp=0;
while(i2 < groupe_actuel)
{
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
if (tableau [i][j]!=0)
{ if (tableau[i][j]==i2)
{tableau[i][j]=j2;temp++;}
}
}
if (temp!=0)
{j2++;temp=0;}
i2++;
}
}
main()
{int i;
int t[10][10]= {{0,1,0,1,0,0,0,0,0,0},
{0,1,0,1,0,0,1,1,1,0},
{0,1,1,1,0,0,1,1,1,0},
{0,0,0,0,0,1,1,1,1,0},
{0,0,0,0,1,1,1,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{1,1,1,0,0,0,0,1,0,0},
{1,1,1,0,0,0,1,1,1,0},
{0,1,0,1,1,0,0,1,0,0},
{0,0,0,1,1,0,0,0,0,0}};
affiche(t);
printf("--------------------------------\n");
clustering(t);
affiche(t);
} |
Partager