/* move(car, d, amount) attempts to move car by amount in the direction of d, returning the actual number of grid cells the car is able to move in that direction. The requested movement is performed (and the carpark updated to reflect the movement) if the corresponding car can perform the requested move, otherwise no movement of the car is performed (and zero is returned). The requested movement is also constrained to ensure the car does not collide with any other car and that the car remains within the bounds of the carpark. */ static int moveCar(char car, DIRECTION d, int amount) { int move = 1; if(amount > carpark.nRows && amount > carpark.nCols) // check amount is not greater than carpark size { return 0; } for(int i = 0; i < amount; i++) { CARINFO c = findCar(car); // update CARINFO with new car location moveCarOneBay(car, d, c); // move the car move++; // count users moves } return move; }
int main(void) { int totCars, maxFloor, lenSeq; scanf("%d %d %d", &totCars, &maxFloor, &lenSeq); std::vector<int> seq = std::vector<int>(); for(int i=0; i<lenSeq; i++) { int a; scanf("%d", &a); //a--; seq.push_back(a); } //myfloor = std::vector<int>(); int floorSize = 0; int shelf = 0; for(int i=0; i<seq.size(); i++) { int car = seq[i]; if(onFloor(car, maxFloor)) continue; if(floorSize < maxFloor && !onFloor(car, floorSize)) { //printf("Floor has space left\n"); myfloor[floorSize] = car; cars[car-1] = i; //myfloor.push_back(car); //printf("size: %d\n", myfloor.size()); floorSize++; shelf++; continue; } //Car not on floor and full floor int max = 0; int ind = -1; //printf("Looking at car: %d\n", car); /*printf("On the floor now: "); for(int k=0; k<maxFloor; k++) {printf("%d ", myfloor[k]);} printf("\n"); */ for(int j=0; j<maxFloor; j++) { int car2 = myfloor[j]; if(cars[car2-1] < i && cars[car2-1] != -2) { cars[car2-1] = findCar(car2, seq, i); } if(cars[car2-1] == -2) { max = cars[car2-1]; ind = j; //printf("%d does not appear again\n", car2); break; } //printf("Next occurence of %d is %d\n", car2, cars[car2-1]); if(cars[car2-1] > max) { max = cars[car2-1]; ind = j; } } //printf("Swapped %d at pos: %d with %d\n\n", myfloor[ind], max, car); myfloor[ind] = car; //myfloor.erase(myfloor.begin()+ind, myfloor.begin()+ind+1); //myfloor.push_back(car); shelf++; } //printf("Mommy has to take minimum this # of cars: %d\n", shelf); printf("%d\n", shelf); return 0; }