#include <stdio.h>
#include <stdlib.h>
////////////////////////////////
// Global variable
int nMap[1100][100];// Max map buff
int nMaxArea=0;
// final output
int nTmax=0;
// save temp max area
int nWidth=0,nHeight=0;
////////////////////////////////
// Function protocol
int SeedFill(int, int);// mian algorithm
////////////////////////////////
// mian
int main(int argc, char *argv[])
{
char ct;
FILE *in,*out;
in=fopen("satpix.in","r");
out=fopen("satpix.out","w");
// Map ini
// map description:
// 0: not usable
1: usable
2:already counted
fscanf(in,"%d%d",&nWidth,&nHeight);
printf("%d %d\n",nWidth,nHeight);
fscanf(in,"%c",&ct);// be careful of '\n'
for(int i=0;i<nHeight;i++)
for (int j=0;j<=nWidth;j++)// be careful of '\n'
{
fscanf(in,"%c",&ct);
if (ct=='.') nMap[i][j]=0;
if (ct=='*') nMap[i][j]=1;
}
//print Map
for(int i=0;i<nHeight;i++)
{
for (int j=0;j<nWidth;j++)
{
printf("%d",nMap[i][j]);
}
printf("\n");
}
// Main algorithm here
for (int i=0;i<nHeight;i++)
for (int j=0;j<nWidth;j++)
{
SeedFill(i,j);
if(nTmax>nMaxArea) nMaxArea=nTmax;
nTmax=0;
}
//print Map
printf ("\n");
for(int i=0;i<nHeight;i++)
{
for (int j=0;j<nWidth;j++)
{
printf("%d",nMap[i][j]);
}
printf("\n");
}
printf ("%d\n",nMaxArea);
fprintf(out,"%d",nMaxArea);
system("PAUSE");
fclose(in);// discard files
fclose(out);
return 0;
}
/////////////////////////////////
// Functions
int SeedFill(int width, int height)
{
// if this position is avaliable and not counted!
if (nMap[width][height]!=1) return 0;
nTmax++;//temp area +1
nMap[width][height]=2;//flag here countec
// if not the most right
if (width<(nWidth-1))
SeedFill(width+1,height);
// if not the most left
if (width>0)
SeedFill(width-1,height);
// if not the most up
if (height>0)
SeedFill(width,height-1);
// if not the most down
if (height<(nHeight-1))
SeedFill(width,height+1);
return 0;
}
[[it] 本帖最后由 SNAKEQX 于 2008-4-29 12:08 编辑 [/it]]