Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Elevator
//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 return 0; } bool isDirectionGood(Request& request) { // depends on requester level and requested level 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 && ++lit != levelsToReach.end()) { // 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, 0)); // 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(); } void addRequest(int floor, int destination) { requests.push_back(Request(floor, destination)); } void printStatus() { 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,4); ec.addRequest(2,3); ec.printStatus(); ec.step(); ec.printStatus(); ec.step(); ec.printStatus(); ec.step(); ec.printStatus(); std::cout << "Hello, world!\n"; }
run
|
edit
|
history
|
help
0
Hello world!
maximize the difference
VirtualRestoPoint
hilbert
template
cvcvcvcvv
offsetof
Using copy_n
SEJM BLURWA
Graph Theory