[CareerCup] 8.4 Parking Lot 停车场问题
8.4 Design a parking lot using object-oriented principles.
LintCode上的原题,请参见我的另一篇博客Parking Lot 停车场问题。
这道题让我们实现一个停车位的数据结构,由于题目没给任何多余的信息,所以自由度很大,比如能停放什么种类的车,或是否是多层的等等。根据书中描述,这里我们做如下假设:
1. 停车场有多层,每层有多行停车位
2. 停车场可以停摩托车,小轿车和公交车
3. 停车场有摩托车位,紧凑型车位,和大型车位
4. 摩托车可以停在任何位置
5. 小轿车可以停在紧凑型车位和大型车位
6. 公交车只能停在同一行中连续的五个大型车位上,不能停在小位置上
有了这么多条件,我们就可以开始写各种类了。首先可定要有个基本类Vehicle,然后摩托车,小轿车和公交车都可以派生出来,每个派生类和基类不同的是需要的空位数不同,还有就是车型不同,那么就要把基类的判断是否能停在当前位置设为虚函数,在派生类中分别实现出来。我们还要用枚举类VehicleSize来标识车型。然后就是要有停车场类ParkingLot,层类Level,停车位类ParkingSpot,它们之间错综复杂的关系请参见下列代码:
enum class VehicleSize { Motorcycle, Compact, Large }; class Vehicle;
class Level; class ParkingSpot {
public:
ParkingSpot(Level *lvl, int r, int n, VehicleSize s): _level(lvl), _row(r), _spotNumber(n), _spotSize(s) {} // ...
bool isAvailable() { return _vehicle == nullptr; };
bool canFitVehicle(Vehicle *vehicle) {} // ...
bool park(Vehicle *v) {} // ...
int getRow() { return _row; }
int getSpotNumber() { return _spotNumber; }
void removeVehicle() {} // ... private:
Vehicle *_vehicle = nullptr;
VehicleSize _spotSize;
int _row;
int _spotNumber;
Level *_level = nullptr;
}; class Vehicle {
public:
Vehicle() {}
int getSpotsNeeded() { return _spotsNeeded; }
VehicleSize getSize() { return _size; }
void parkInSpot(ParkingSpot s) { _parkingSpots.push_back(s); }
void clearSpots() {} // ...
virtual bool canFitInSpot(ParkingSpot spot) {} protected:
vector<ParkingSpot> _parkingSpots;
string _licensePlate;
int _spotsNeeded;
VehicleSize _size;
}; class Bus: public Vehicle {
public:
Bus() {
_spotsNeeded = ;
_size = VehicleSize::Large;
}
bool canFitInSpot(ParkingSpot spot) { }
}; class Car: public Vehicle {
public:
Car() {
_spotsNeeded = ;
_size = VehicleSize::Compact;
}
bool canFitInSpot(ParkingSpot spot) { }
}; class Motorcycle: public Vehicle {
public:
Motorcycle() {
_spotsNeeded = ;
_size = VehicleSize::Motorcycle;
}
bool canFitInSpot(ParkingSpot spot) { }
}; class Level {
public:
Level() {}
Level(int flr, int numberSpots): _floor(flr), _availableSpots(numberSpots) {}
Level(const Level* lvl) {
*this = *lvl;
}
int availableSpots() { return _availableSpots; }
bool parkVehicle(Vehicle vehicle) {} // ...
void spotFreed() { ++_availableSpots; } private:
int _floor;
vector<ParkingSpot> _spots;
int _availableSpots = ;
static const int _SPOTS_PER_ROW = ;
bool parkStartingAtSpot(int num, Vehicle v) {} // ...
int findAvailableSpots(Vehicle vehicle) {} // ...
}; class ParkingLot {
public:
ParkingLot() {} // ...
bool parkVehicle(Vehicle vehicle) {} // ... private:
vector<Level> _levels;
const int _NUM_LEVELS = ;
};
[CareerCup] 8.4 Parking Lot 停车场问题的更多相关文章
- [LintCode] Parking Lot 停车场问题
Design a parking lot. see CC150 OO Design for details.1) n levels, each level has m rows of spots an ...
- English trip V1 - 2.Don't Do That Teacher:Patrick Key: 祈使句(imperatives)
什么是祈使句? What's imperatives? 求或者希望别人做什么事或者不做什么事时用的句子:带有命令的语气 In this lesson you will learn how to ...
- 新概念英语三 新东方主讲Lesson1
新概念二 Lesson95 词汇 ①get a shock 吓了一跳,得到一个惊喜 例:his wife got a shock get into a such mess 这么不幸搞得一片狼籍弄得这样 ...
- [PA2014]Parking
[PA2014]Parking 题目大意: 停车场是一个宽度为\(w(w\le10^9)\)的矩形.我们以其左下角顶点为原点,坐标轴平行于矩形的边,建立直角坐标系.停车场很长,我们可以认为它一直向右边 ...
- bzoj3718 [PA2014]Parking
Description 你的老板命令你将停车场里的车移动成他想要的样子.停车场是一个长条矩形,宽度为w.我们以其左下角顶点为原点,坐标轴平行于矩形的边,建立直角坐标系.停车场很长,我们可以认为它一直向 ...
- C语言实现简单的停车场管理系统
问题描述:停车场是一个能放n辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放.若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入.当停车场中的车离开时,由于通道窄,在它后面呢 ...
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- CareerCup All in One 题目汇总 (未完待续...)
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
随机推荐
- (第五章)java面向对象之this的作用总结
this关键字总是指向调用该方法的对象. this可以代表任何对象,当this出现在某个方法体中时,它所代表的对象是不确定的,但它的类型是确定的,它所代表的对象只能是当前类的(在那个类中就是那个类), ...
- linq扩展之动态排序
前两天看QQ群里面,一位朋友问的问题,说在linq中怎么实现动态排序呢,自己想了半天,没有头绪,网上找了下相关的资料,看了下,收益挺多,记录下来. 之前我们没有如果不知道动态排序的方法的话,我们可能会 ...
- Stack与Heap的区别
申明:这里所说的栈和堆是程序内存管理中的栈和堆,而不是数据结构里的栈和堆. (1)保存的内容不同:栈里保存的是局部变量,而堆里保存的是动态申请的变量. (2)栈里的内存系统自动申请和释放,程序执行出申 ...
- 【转】Python开发指南:最佳实践精选
总体原则 价值 “为别人开发你也想要使用的工具.” ——Kenneth Reitz "简洁总是胜过可用." ——Pieter Hintjens "满足90%的使用场景.忽 ...
- SpringMVC 返回 html 视图页面,SpringMVC与Servlet,Servlet重定向与转发
1. SpringMVC与Servlet的关系 SpringMVC框架是建立在Servlet之上的,提供各种功能,各种封装,各种方便的同时,它一点儿也没有限制Servlet,我们完全可以在Spring ...
- 深入掌握Java中的enum
对于要在程序中要表示有限种类的某事物,一般我们可以采用两种方式,一是使用:public static final String 常量:二是使用enum来表示.一般而言前者简单,但是不能够很好的提供更多 ...
- 在linux下修改mysql的root密码
第一种方法: root用户登录系统 /usr/local/mysql/bin/mysqladmin -u root -p password 新密码 enter password 旧密码 第二种方法: ...
- MPP 架构数据库
Greenplum是一种基于postgresql的分布式数据库.其采用shared nothing架构(MPP),主机,操作系统,内存,存储都是自我控制的,不存在共享.也就是每个节点都是一个单独的数据 ...
- js制作倒计时效果
该程序可以计算任意指定的两个日期中间的差值. 本例子的指定日期是2017年1月1日,当前时间是2016年10月21日:计算它们之间的差值,在网页上显示如图所示: 关键部分代码: var nowtime ...
- TestNG之Factory
如果我们的测试方法中,同一个变量需要很多个不同的测试数据,那么这些测试数据由谁提供呢,testng提供了factory的注解,下面我们来一探究竟. 一.单独使用Factory 1.新建一个含有@Fac ...