/* A Muddle of Maps */ #include <stdio.h> #define DEBUG (1 == 1) #define FALSE (1 != 1) #define ROW 10 #define COLUMN 10 int StillOnMap(int i, int j, int numRows, int numCols) { // begin FUNCTION StillOnMap return ((i > 0) && (i <= numRows) && (j > 0) && (j <= numCols)); } // begin FUNCTION StillOnMap int main(void) { int NumMaps = 0, // Number of map numRows, // number of rows in current map numCols, // number of colums in current map beginRow, // row to begin at beginCol, // column to begin at endRow, // row to end at endCol, // column to end at numMoves, // number of movees for a map direction, // In what direction the Dr. is moving steps, // number of steps onMap, // Still on Map GoodMapCnt = 0, // Count the number of good maps i, // The count for map loop j, // The count for row/move loop k, // The count for column/move loop r, // The row for the movement in the map c; // The column for the movement in the map scanf(" %d", &NumMaps); if (DEBUG) printf("There are %d maps to be explored today.\n\n", NumMaps); for (i=0;i < NumMaps;i++) { // loop for each map scanf(" %d %d", &numRows, &numCols); if (DEBUG) printf("The map is %dx%d.\n", numRows, numCols); scanf(" %d %d", &beginRow, &beginCol); scanf(" %d %d", &endRow, &endCol); if (DEBUG) printf("The beginning coordinates are (%d,%d) and ending at (%d,%d).\n",beginRow, beginCol, endRow, endCol); r = beginRow; c = beginCol; scanf(" %d",&numMoves); for (k=0; k < numMoves; k++) { // loop through moves onMap = StillOnMap(r,c,numRows,numCols); scanf("%d %d", &direction, &steps); if (DEBUG) printf("Currently at [%d,%d]\tdirection: %d\tsteps: %d\n",r,c, direction, steps); switch (direction) { // switch case 8: r = r + steps; break; case 2: r = r - steps; break; case 4: c = c - steps; break; case 6: c = c + steps; break; } // switch } // loop through moves if (DEBUG) printf("Currently at [%d,%d]\n",r,c); if (onMap) { // no bad moves if ((r == endRow) && (c == endCol)) { // success printf("Good Map\n"); GoodMapCnt = GoodMapCnt + 1; } // success else { // did not reach end printf ("Bad Map\n"); } // did not reach end } // no bad moves else { // fell off the map printf ("Bad Map\n"); } // fell off the map } // loop for each map printf ("%d Good Maps detected.\n",GoodMapCnt); return 0; } Return to Problem Index |