2001 LSU Computer Science High School Programming Contest

Sponsored by Texas Instruments

Novice and Veteran - Problem 1, Solution in cpp

/*
Phil Bordelon
CSC 3999.14
Mud Wars Solution Code

Structure-Based Solution
*/

#include <stdio.h>
#include <iomanip.h>

#define DEBUGIT (1 == 1)

#define BACK_LEFT_MUD 1
#define BACK_BALLOON 2
#define BACK_RIGHT_MUD 3
#define LEFT_BALLOON 4
#define SHOWER 5
#define RIGHT_BALLOON 6
#define FRONT_LEFT_MUD 7
#define FRONT_BALLOON 8
#define FRONT_RIGHT_MUD 9

#define IN_FILE stdin
#define OUT_FILE stdout

#define TRUE 1
#define FALSE 0

typedef struct vehicle_t
{
	int front_mud;
	int left_mud;
	int right_mud;
	int back_mud;
} vehicle_t;

int reBound (int& value, int min, int max);
int mudhole (vehicle_t& vehicle, int amount, char location);
int balloon (vehicle_t& vehicle, int amount, char location);
int shower (vehicle_t& vehicle, int time);
int endScore (vehicle_t& vehicle);
int initVehicle (vehicle_t& vehicle);
int runData (vehicle_t& vehicle, FILE * infile, FILE * outfile);

int reBound (int& value, int min, int max)
{
	int did_rebound;

	did_rebound = FALSE;

	if (value < min)
	{
		value = min;
		did_rebound = TRUE;
	}
	else
	{
		if (value > max)
		{
			value = max;
			did_rebound = TRUE;
		}
	}
	
	return (did_rebound);
}

int mudhole (vehicle_t& vehicle, int amount, char location)
{

	int did_overheat;

	did_overheat = FALSE;

    switch (location)
	{
		case BACK_LEFT_MUD: vehicle.back_mud += amount / 2;
			                vehicle.left_mud += amount / 2;
							break;
		case BACK_RIGHT_MUD: vehicle.back_mud += amount / 2;
			                 vehicle.right_mud += amount / 2;
						  	 break;
		case FRONT_LEFT_MUD: vehicle.front_mud += amount / 2;
			                 vehicle.left_mud += amount / 2;
						 	 break;
		case FRONT_RIGHT_MUD: vehicle.front_mud += amount / 2;
			                  vehicle.right_mud += amount / 2;
							  break;
		default: break;
	}
	reBound (vehicle.back_mud, 0, 100);
	reBound (vehicle.left_mud, 0, 100);
	reBound (vehicle.right_mud, 0, 100);
    if (TRUE == reBound (vehicle.front_mud, 0, 35))
	{
		did_overheat = TRUE;
	}

	return (did_overheat);
}

int balloon (vehicle_t& vehicle, int amount, char location)
{
	switch (location)
	{
		case BACK_BALLOON: vehicle.back_mud -= amount;
						   break;
		case LEFT_BALLOON: vehicle.left_mud -= amount;
						   break;
		case RIGHT_BALLOON: vehicle.right_mud -= amount;
							break;
		case FRONT_BALLOON: vehicle.front_mud -= amount;
							break;
		default: break;
	}
	reBound (vehicle.back_mud, 0, 100);
	reBound (vehicle.left_mud, 0, 100);
	reBound (vehicle.right_mud, 0, 100);
	reBound (vehicle.front_mud, 0, 35);

	return 0;
}

int shower (vehicle_t& vehicle, int time)
{

	vehicle.back_mud -= 2 * time;
	vehicle.left_mud -= 5 * time;
	vehicle.right_mud -= 5 * time;
	vehicle.front_mud -= 7 * time;

	reBound (vehicle.back_mud, 0, 100);
	reBound (vehicle.left_mud, 0, 100);
	reBound (vehicle.right_mud, 0, 100);
	reBound (vehicle.front_mud, 0, 35);

	return 0;
}
     
int endScore (vehicle_t& vehicle)
{
	int temp_score;

	temp_score = vehicle.back_mud + vehicle.front_mud +
	 vehicle.left_mud + vehicle.right_mud;

	if (0 == temp_score)
	{
		temp_score = 0;
	}
	else
	{
		reBound (temp_score, 2, 335);
	}
	return (temp_score);
}

int initVehicle (vehicle_t& vehicle)
{
	vehicle.back_mud = 0;
	vehicle.front_mud = 0;
	vehicle.left_mud = 0;
	vehicle.right_mud = 0;
	
	return 0;
}

int runData (vehicle_t& vehicle, FILE * infile, FILE * outfile)
{
	int dataset_count;
	int action_count;
	int curr_action;
	int curr_value;
	int i, j;
	int did_overheat;

	fscanf (infile, " %d", &dataset_count);
	for (i = 0; i < dataset_count; i ++)
	 { // process all games
		initVehicle (vehicle);
		did_overheat = FALSE;
		fscanf (infile, " %d", &action_count);
		if (DEBUGIT) printf("action_count just read: %d\n",action_count);
		for (j = 0; j < action_count; j ++)
		 { // process current game
			fscanf (infile, " %d %d", &curr_action, &curr_value);
			if (FALSE == did_overheat)
			 { // process current action
				switch (curr_action)
				 { // switch
					case BACK_LEFT_MUD:
					case BACK_RIGHT_MUD:
					case FRONT_LEFT_MUD:
					case FRONT_RIGHT_MUD: did_overheat = mudhole 
										   (vehicle, curr_value,
										   curr_action);
										  break;
					case BACK_BALLOON:
					case LEFT_BALLOON:
					case RIGHT_BALLOON:
					case FRONT_BALLOON: balloon (vehicle, curr_value,
									     curr_action);
									    break;
					case SHOWER: shower (vehicle, curr_value);
						         break;
					default: break;
				 } // switch
			 } // process current action
			if (DEBUGIT) printf("action: %d, amount: %d, [%d %d %d %d]\n",curr_action,curr_value,vehicle.front_mud,vehicle.right_mud,vehicle.back_mud,vehicle.left_mud);
		 } // process current game
		if (TRUE == did_overheat)
		 { // overheated
			fprintf (outfile, "GAME OVER, MAN\n");
		 } // overheated
		else
		 { // made it
			fprintf (outfile, "%d\n", endScore (vehicle));
		 } // made it
	} // process all games
	return 0;
}

int main (int argc, char* argv[])
{
	FILE * infile;
	FILE * outfile;
	vehicle_t vehicle;

	infile = IN_FILE;
	outfile = OUT_FILE;

	runData (vehicle, infile, outfile);
    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