2001 LSU Computer Science High School Programming Contest

Sponsored by Texas Instruments

Veteran - Problem 5, Solution in c

/*
	Treasure 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 map [ROW] [COLUMN],		// The map with stored values
        puzzleNum = 0,			// Number of map
        valuables,			// The sum of all valuables
        squares = 0,			// The number of squares in 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
        numSteps,			// The number of steps taken during game
        numMoves,			// The number of moves the Dr. is moving
        direction,			// In what direction the Dr. is moving 
        steps,				// number of steps
	onMap,				// Still on Map
        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", &puzzleNum);

	if (DEBUG) printf("There are %d maps to be explored today.\n\n", puzzleNum);

	for (i=0;i < puzzleNum;i++)
	 {
		scanf(" %d %d", &numRows, &numCols);
		if (DEBUG) printf("The map is %dx%d.\n", numRows, numCols);

		for (j=1; j <= numRows; j++)
		 {
			for (k=1; k <= numCols; k++)
			 {
				scanf(" %d", &map[j] [k]);
				if (DEBUG) printf(" map[%d,%d] = %d\n",j,k,map[j][k]);
			 }
		 }
		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;
		numSteps = 0;
		onMap = StillOnMap(r,c,numRows,numCols);
		if (onMap)
		 { // valid move
			valuables = map[r][c];
			map[r][c] = 0;
			numSteps = 1;
		 } // valid move
		scanf(" %d",&numMoves);
		if (DEBUG) printf("Expected moves: %d\n",numMoves);
		for (k=0; k < numMoves; k++)
		 { // loop through moves
			scanf("%d %d", &direction, &steps);
			if (DEBUG) printf("Currently at [%d,%d]\tvaluables: %d\tdirection: %d, steps: %d\n",r,c,valuables, direction, steps);
			if (onMap)
			 { // still on map
				switch (direction)
				{ // switch
				 case 8:
					for (j=0; j < steps; j++)
					 {
						r = r + 1;
						if (StillOnMap(r,c,numRows,numCols))
						 { // okay
							valuables = valuables + map[r] [c];
							map[r] [c] = 0;
							numSteps = numSteps + 1;
						 } // okay
						else
						 { // not okay
							onMap = FALSE;
						 } // not okay
					 }
					break;
				 case 2:
					for (j=0; j < steps; j++)
					  {
					 	r = r - 1;
						if (StillOnMap(r,c,numRows,numCols))
						 { // okay
						 	valuables = valuables + map[r] [c];
						 	map[r] [c] = 0;
						 	numSteps = numSteps + 1;
						 } // okay
						else
						 { // not okay
							onMap = FALSE;
						 } // not okay
					  }
					 break;
				 case 4:
					for (j=0; j < steps; j++)
					 {
						c = c - 1;
						if (StillOnMap(r,c,numRows,numCols))
						 { // okay
							valuables = valuables + map[r] [c];
							map[r] [c] = 0;
							numSteps = numSteps + 1;
						 } // okay
						else
						 { // not okay
							onMap = FALSE;
						 } // not okay
					 }
					break;
				case 6:
					for (j=0; j < steps; j++)
					 {
						c = c + 1;
						if (StillOnMap(r,c,numRows,numCols))
						 { // okay
							valuables = valuables + map[r] [c];
							map[r] [c] = 0;
							numSteps = numSteps + 1;
						 } // okay
						else
						 { // not okay
							onMap = FALSE;
						 } // not okay
					 }
					break;
	
       		     		 } // switch
			 } // still on map
		 } // loop through moves
	if (DEBUG) printf("Currently at [%d,%d]\tvaluables: %d\n",r,c,valuables);
	if (DEBUG) printf("Dr. Brownian walked in %d squares and found %d dollars worth of valuables.\n\n",numSteps ,valuables);
	if (onMap) 
	 { // no bad moves
		if ((r == endRow) && (c == endCol))
		 { // success
			printf("%d %d\n",numSteps, valuables);
		 } // 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

   }

    return 0;

}


Return to Problem Index


 

The statements and opinions included in these pages are those of 2001 LSU Computer Science High School Programming Contest only. Any statements and opinions included in these pages are not those of Louisiana State University or the LSU Board of Supervisors.
© 2000,2001 Isaac Traxler
Last modified: Friday, 01 July, 2011 16:27:53