#include #include #include #define TRUE 1 #define FALSE 0 void main (void) { FILE *infile, *outfile; char buffer[10]; int safecolumn[10]; int madeit; int i,row,col, velocity; int direction; /* Open the input and output files */ infile = stdin; outfile = stdout; /* Process the input sets */ while (1) { /* We'll break after an unsuccessful read attempt */ /* Assume that Frogger can make it starting in any column */ for (i=0; i<10; i++) { safecolumn[i]=TRUE; } /* Read the header for this input set */ fscanf(infile,"%s",buffer); if (feof(infile)) break; if (strcmp(buffer,"START")!=0) { fprintf(stderr,"\"%s\" != \"START\"",buffer); exit(1); } /* Process the rows */ for (row=1; row<=8; row++) { /* Process this car (if it exists) */ for (col=0; col<=9; col++) { /* Read the car's velocity */ fscanf(infile," %d ",&velocity); if (velocity < 0 || velocity > 9) { fprintf(stderr,"\"%d\" is out of range.",velocity); exit(1); } /* If there's no car present, skip it */ if (velocity == 0) continue; /* Determine the car's direction */ if (row <=4) { direction=-1; } else { direction=1; } /* Eliminate columns for this row * Note that I add 100 to my calculated position before * performing my modulus function since modulus only works * on positive values. Since the max speed is 9, the max * negative position is -72, so adding 100 is sure to * result in a positive operand to the modulus */ for (i = ((row-1)*velocity*direction + col); i != ((row)*velocity*direction + col); i += direction) { safecolumn[(i + 100) % 10]=FALSE; } safecolumn[((row)*velocity*direction + col + 100) % 10]=FALSE; } /* Done with this row */ /* This section of code will help illustrate which columns are * dangerous */ /* for (i=0; i<10; i++) { if (safecolumn[i]==TRUE) fprintf(outfile,"O"); else fprintf(outfile,"X"); } fprintf(outfile,"\n"); */ } /* Done with the freeway */ /* Read the footer for this input set */ fscanf(infile," %s ",buffer); if (strcmp(buffer,"END")!=0) { fprintf(stderr,"\"%s\" != \"END\"",buffer); exit(1); } /* Print the solution for this input set */ madeit=FALSE; for (i=0; i<10; i++) { if (safecolumn[i] == TRUE) { madeit=TRUE; break; } } if (madeit==TRUE) { fprintf(outfile,"LEFTOVER POSSUM\n"); } else { fprintf(outfile,"FROGGER\n"); } } /* Clean up */ fclose(infile); fclose(outfile); }