/* Problem 6: Randy Appleton's Plane This counts the number of squares a woman visits as she walks along a plane. */ #include #include #include int main (int argc, char **argv); FILE *in, *out; int main (int argc, char **argv) { int P[161][161], n, count, x, y, le; /* The plane, the number of tests The number of visited squares, The current position, the loop exit flag. */ in = fopen ("prob6.in","r"); out = fopen ("prob6.out","w"); for (fscanf (in,"%d ",&n); n > 0; n--) { memset (P,0,sizeof P); /* Loop through the tests */ le = 0; x = y = 80; /* initialize */ P[x][y] = count = 1; while (1) { switch (getc (in)) { case 'N': y++; break; /* Change the position appropriately */ case 'S': y--; break; case 'E': x++; break; case 'W': x--; break; default: le = 1; /* exit loop when is hit */ } if (le) break; if (!P[x][y]) { /* If we haven't been there yet, update info */ P[x][y] = 1; count++; } } fprintf (out,"%d\n",count); } fclose (in); fclose (out); return EXIT_SUCCESS; }