by htl » Sun Jul 21, 2002 5:55 pm
This is my new code. It still get WA.
[c]
#include<stdio.h>
#include<string.h>
#define YES 1
#define NO 0
int table[100][100],row,maxcol,species;
char monkey[100][100];
void s(int,int);
void output(void);
void main(void)
{
int x,y,exit,len;
char buffer[300];
while(1)
{
row=maxcol=0;
exit=YES;
while(fgets(buffer,300,stdin)!=NULL)
{
len=strlen(buffer)-1;
buffer[len]='\0';
if(len==1 && buffer[0]=='%')
{
exit=NO;
break;
}
for(x=0,y=0;buffer[x]!='\0';x++)
if(buffer[x]>='a' && buffer[x]<='z' || buffer[x]>='A' && buffer[x]<='Z')
monkey[row][y++]=buffer[x];
if(y>maxcol)
maxcol=y;
row++;
}
for(x=0;x<row;x++)
for(y=0;y<maxcol;y++)
table[x][y]=0;
species=0;
for(x=0;x<row;x++)
for(y=0;y<maxcol;y++)
if(!table[x][y])
{
species++;
table[x][y]=species;
s(x,y);
}
output();
if(exit)
break;
}
}
void s(int x,int y)
{
if(x-1>=0 && x-1<row && y-1>=0 && y-1<maxcol && monkey[x-1][y-1]==monkey[x][y] && !table[x-1][y-1])
{
table[x-1][y-1]=species;
s(x-1,y-1);
}
if(x-1>=0 && x-1<row && y>=0 && y<maxcol && monkey[x-1][y]==monkey[x][y] && !table[x-1][y])
{
table[x-1][y]=species;
s(x-1,y);
}
if(x-1>=0 && x-1<row && y+1>=0 && y+1<maxcol && monkey[x-1][y+1]==monkey[x][y] && !table[x-1][y+1])
{
table[x-1][y+1]=species;
s(x-1,y+1);
}
if(x>=0 && x<row && y-1>=0 && y-1<maxcol && monkey[x][y-1]==monkey[x][y] && !table[x][y-1])
{
table[x][y-1]=species;
s(x,y-1);
}
if(x>=0 && x<row && y+1>=0 && y+1<maxcol && monkey[x][y+1]==monkey[x][y] && !table[x][y+1])
{
table[x][y+1]=species;
s(x,y+1);
}
if(x+1>=0 && x+1<row && y-1>=0 && y-1<maxcol && monkey[x+1][y-1]==monkey[x][y] && !table[x+1][y-1])
{
table[x+1][y-1]=species;
s(x+1,y-1);
}
if(x+1>=0 && x+1<row && y>=0 && y<maxcol && monkey[x+1][y]==monkey[x][y] && !table[x+1][y])
{
table[x+1][y]=species;
s(x+1,y);
}
if(x+1>=0 && x+1<row && y+1>=0 && y+1<maxcol && monkey[x+1][y+1]==monkey[x][y] && !table[x+1][y+1])
{
table[x+1][y+1]=species;
s(x+1,y+1);
}
}
void output(void)
{
int x,y,t,max,column[100];
for(y=0;y<maxcol;y++)
{
for(x=0,max=0;x<row;x++)
{
if(table[x][y]<10)
t=1;
else if(table[x][y]>=10 && table[x][y]<100)
t=2;
else if(table[x][y]>=100 && table[x][y]<1000)
t=3;
else if(table[x][y]>=1000 && table[x][y]<10000)
t=4;
else if(table[x][y]>=10000 && table[x][y]<=32767)
t=5;
if(t>max)
max=t;
}
column[y]=max;
}
for(x=0;x<row;x++)
{
for(y=0;y<maxcol;y++)
if(y==0)
printf("%*d",column[y],table[x][y]);
else
printf(" %*d",column[y],table[x][y]);
printf("\n");
}
printf("%%\n");
}
[/c]
I have a question: will the chars except alphabets appear?