#include #include #include #include #include #include #define ABS(X) ((X<0)?(-X):(X)) #define SQR(X) ((X)*(X)) #define MIN(a,b) (((a)<(b))?(a):(b)) #define INFILE "triangle.in" FILE *fin; int caseno=0; int *tri[100]; char s[200]; int width,height; int minimum (int a,int b,int c){ int tmp1=MIN(a,b); int tmp2=MIN(tmp1,c); return tmp2; } int solvecase(){ while(1){ //fscanf(fin,"%d",&height); fgets(s,300,fin); sscanf(s,"%d",&height); //printf("%d\n",height); if(height==0) return 0; else{ caseno++; int maxi=0; width=2*height-1; //read input and compute upside down for(int i=0; imaxi){ maxi=tri[i][j]; } //maxi=1; } // printf("%d",tri[i][j]); } } else{ for(int j=i; jmaxi){ maxi=tri[i][j]; } } // printf("%d",tri[i][j]); } } //else //printf("\n"); } //printf("max bis jetzt=%d\n",maxi); //read input and compute bottom up for(int i=height-1; i>=0; i--){ if(i==height-1){ if(tri[i][width-i-1]>0){ tri[i][width-i-1]=1; if(tri[i][width-i-1]>maxi){ maxi=tri[i][width-i-1]; } } // printf("%d",tri[i][width-i-1]); } else{ for(int j=i; jwidth-i-3)){ if(tri[i][j]>0){ tri[i][j]=1; if(tri[i][j]>maxi){ maxi=tri[i][j]; } } } else{ if(tri[i][j]>0){ int tmp=minimum(tri[i+1][j-1],tri[i+1][j], tri[i+1][j+1]); tri[i][j]=tmp+1; if(tri[i][j]>maxi){ maxi=tri[i][j]; } } } // printf("%d",tri[i][j]); } } //printf("\n"); } fgets(s,300,fin); printf("Triangle #%d\n",caseno); printf("The largest triangle area is %d.\n\n", (maxi*maxi)); }//else } } //int readcase(){ //} int main(){ for(int i=0; i<100; i++){ tri[i]=new int[199]; } fin=fopen(INFILE,"r"); solvecase(); fclose(fin); return 0; }