// acmprog.cpp : Problem: Get out da way! // #include #include #include #include "string.h" const int NORTH = 0; const int EAST = 1; const int UP = 2; const int DOWN = 0; const int VER = 0; const int HOR = 1; const int LEFT = 0; const int RIGHT = 1; const int ABOVE = 0; const int BELOW = 1; const int MAX_GRID_SIZE = 31; const double BLOCK_HEIGHTH = .1; const double BLOCK_WIDTH = .1; const int TARGET_START_NORTH = 10; typedef char GridType[MAX_GRID_SIZE][MAX_GRID_SIZE]; void markGrid(GridType, unsigned int, unsigned int, unsigned int, unsigned int, bool&); int main(int argc, char* argv[]) { char line[MAX_GRID_SIZE]; //for input unsigned int numVectors = 0; double allVectors[10][3]; double currVector[3]; double targetVector[3]; char targetGrid[MAX_GRID_SIZE][MAX_GRID_SIZE]; unsigned int gridHeighth = 0; unsigned int gridWidth = 0; bool shot = false; double targetVelocity[3]; double bulletStartCoord[2]; double bulletHit[2]; double bulletHitCoord[2]; unsigned int bulletHitIndex[2]; unsigned int i,j; int numBullets; cin >> line; //Read all lines until end of input while (cin) { if (strcmp(line, "START") != 0) cout << "ERROR in input: START not found" << endl; //Read in # of bullets (this is not used) cin >> numBullets; //Initialize targetGrid for (i = 0; i < MAX_GRID_SIZE; i++) { for (j = 0; j < MAX_GRID_SIZE; j++) targetGrid[i][j] = ' '; targetGrid[i][MAX_GRID_SIZE-1] = '\0'; } //Initialize vectors for (i = 0; i < 3; i++) { currVector[i] = 0.0; targetVector[i] = 0.0; } gridHeighth = 0; gridWidth = 0; numVectors = 0; shot = false; cin.getline(line, MAX_GRID_SIZE); //Determine if current line is a vector //Assumption: if line contains comma, we //are reading in a vector while (strcmp(line, "END") != 0) { if (strchr(line,',')) { //Split vectors delimited by comma allVectors[numVectors][0] = atof(strtok(line,",")); allVectors[numVectors][1] = atof(strtok(NULL,",")); allVectors[numVectors][2] = atof(strtok(NULL,",")); numVectors++; } else { strncpy(targetGrid[gridHeighth],line, strlen(line)); gridHeighth++; //Retain longest line width if (strlen(line) > gridWidth) gridWidth = strlen(line); } cin.getline(line, MAX_GRID_SIZE); } //After reading input: //allVectors[1..numVectors-1][0..3] contain bullet vectors //allVectors[0][0..3] contains target vector //targetGrid[gridHeighth][gridWidth] contains target grid //targetVelocity[NORTH] = allVectors[0][NORTH]; //targetVelocity[EAST] = allVectors[0][EAST]; //targetVelocity[UP] = allVectors[0][UP]; targetVelocity[NORTH] = allVectors[0][NORTH]; targetVelocity[EAST] = allVectors[0][EAST]; targetVelocity[UP] = allVectors[0][UP]; //To keep it simple, coordinates here based on 0,0 in top left //corner of matrix bulletStartCoord[DOWN] = double(double(gridHeighth) / double(2.0)); bulletStartCoord[EAST] = double(double(gridWidth) / double(2.0)); //Process all the bullets for (unsigned int bullet = 1; bullet < numVectors; bullet++) { double bulletVelocity[3]; double timeToReachTarget; bulletVelocity[NORTH] = allVectors[bullet][NORTH]; bulletVelocity[EAST] = allVectors[bullet][EAST]; bulletVelocity[UP] = allVectors[bullet][UP]; double relativeVelocityNorth = 0.0; relativeVelocityNorth = bulletVelocity[NORTH] - targetVelocity[NORTH]; if (relativeVelocityNorth != 0) timeToReachTarget = TARGET_START_NORTH / relativeVelocityNorth; else timeToReachTarget = 0; //Make sure the bullet will eventually reach the target! if (timeToReachTarget > 0) { bulletHit[DOWN] = -bulletVelocity[UP] * timeToReachTarget; bulletHit[EAST] = bulletVelocity[EAST] * timeToReachTarget; bulletHitCoord[EAST] = bulletStartCoord[EAST] + (bulletHit[EAST] / BLOCK_WIDTH) - (timeToReachTarget * targetVelocity[EAST] / BLOCK_WIDTH); bulletHitCoord[DOWN] = bulletStartCoord[DOWN] + (bulletHit[DOWN] / BLOCK_HEIGHTH) - (timeToReachTarget * -targetVelocity[UP] / BLOCK_HEIGHTH); //Calculate index in grid bullet hit bulletHitIndex[EAST] = int(floor(bulletHitCoord[EAST])); bulletHitIndex[DOWN] = int(floor(bulletHitCoord[DOWN])); //Try to mark in grid markGrid(targetGrid,bulletHitIndex[DOWN],bulletHitIndex[EAST],gridHeighth,gridWidth,shot); } } if (shot) { for (i = 0; i < gridHeighth; i++) { for (j = 0; j < gridWidth; j++) cout << targetGrid[i][j]; cout << endl; } } else cout << "Got out da way!" << endl; cout << endl; cin >> line; } return 0; } void markGrid(GridType grid, unsigned int row, unsigned int col, unsigned int maxRow, unsigned int maxCol, bool &shot) { if (row >= 0 && row < maxRow && col >= 0 && col < maxCol && grid[row][col] != ' ') { shot = true; grid[row][col] = '*'; } }