Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Elevator 3
//Title of this code #include <iostream> #include <list> #include <stdlib.h> using namespace std; struct Request { int curLevel; int destLevel; Request(int cur, int dest) : curLevel(cur), destLevel(dest) {} }; class Elevator { int id; int curLevel; list<int> levelsToReach; public: Elevator(int i, int level) : id(i), curLevel(level) {} int getCurLevel() { return curLevel; } int getDirection() { // -1,0,1 if (levelsToReach.empty()) return 0; else if (curLevel > levelsToReach.front()) return -1; return 1; } bool isDirectionGood(Request& request) { // depends on requester level and requested level //if (!levelsToReach.empty()) // if (curLevel > request.front()) return true; } void addNewRequest(Request& request) { // not good need to be ordered , no duplicates //levelsToReach.push_back(request.curLevel); levelsToReach.push_back(request.destLevel); } void move() { if (!levelsToReach.empty()) { auto lit = levelsToReach.begin(); // we reached a destination level if (curLevel == *lit) { // not sure its ok levelsToReach.erase(levelsToReach.begin()); } if (!levelsToReach.empty()) { if (curLevel < *lit) ++curLevel; else --curLevel; } } } }; class ElevatorControl { list<Elevator> elevators; list<Request> requests; int numElevators; int numLevels; public: ElevatorControl(int numOfElevators, int maxLevel) { numLevels = maxLevel; numElevators = numOfElevators; // Set the Elevators for (int i = 0; i < numElevators; ++i) elevators.push_back(Elevator(i, (numLevels / numElevators + 1) * i)); // optimize level to reach (maxLevel/numOfElevators) * i } void step() { for (auto rit = requests.begin(); rit != requests.end(); ++rit) { int requestLevel = rit->curLevel; auto bestElevator = elevators.begin(); for (auto eit = elevators.begin(); eit != elevators.end(); ++eit) { //if 0 optimize if (eit->isDirectionGood(*rit) && abs(requestLevel - eit->getCurLevel()) < abs(requestLevel - bestElevator->getCurLevel()) ) bestElevator = eit; } bestElevator->addNewRequest(*rit); } requests.erase(requests.begin(), requests.end()); // here do something smart thing with elevators for (auto eit = elevators.begin(); eit != elevators.end(); ++eit) eit->move(); printStatus(); } void addRequest(int floor, int destination) { requests.push_back(Request(floor, destination)); } void printStatus() { cout << "( "; for (auto eit = elevators.begin(); eit != elevators.end(); ++eit) { cout << eit->getCurLevel() << " "; } cout << ")" << endl; for (int level = numLevels - 1; level >= 0; --level) { for (auto eit = elevators.begin(); eit != elevators.end(); ++eit) { if (eit->getCurLevel() == level) cout << "#"; else cout << "."; } cout << endl; } cout << endl; } }; int main() { ElevatorControl ec(5,8); ec.addRequest(4,1); ec.printStatus(); ec.step(); ec.step(); ec.step(); ec.step(); ec.step(); std::cout << "Hello, world!\n"; }
run
|
edit
|
history
|
help
0
DailyExchRate2
Binary Search
Binary search on sorted array
RCP 27
Matrix rotation
Vowel_check
Set of intervals.
mytemp
Anagrams
cotton farm 0.0.1.0