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 停车场问题的更多相关文章

  1. [LintCode] Parking Lot 停车场问题

    Design a parking lot. see CC150 OO Design for details.1) n levels, each level has m rows of spots an ...

  2. English trip V1 - 2.Don't Do That Teacher:Patrick Key: 祈使句(imperatives)

    什么是祈使句?    What's imperatives? 求或者希望别人做什么事或者不做什么事时用的句子:带有命令的语气 In this lesson you will learn how to ...

  3. 新概念英语三 新东方主讲Lesson1

    新概念二 Lesson95 词汇 ①get a shock 吓了一跳,得到一个惊喜 例:his wife got a shock get into a such mess 这么不幸搞得一片狼籍弄得这样 ...

  4. [PA2014]Parking

    [PA2014]Parking 题目大意: 停车场是一个宽度为\(w(w\le10^9)\)的矩形.我们以其左下角顶点为原点,坐标轴平行于矩形的边,建立直角坐标系.停车场很长,我们可以认为它一直向右边 ...

  5. bzoj3718 [PA2014]Parking

    Description 你的老板命令你将停车场里的车移动成他想要的样子.停车场是一个长条矩形,宽度为w.我们以其左下角顶点为原点,坐标轴平行于矩形的边,建立直角坐标系.停车场很长,我们可以认为它一直向 ...

  6. C语言实现简单的停车场管理系统

    问题描述:停车场是一个能放n辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放.若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入.当停车场中的车离开时,由于通道窄,在它后面呢 ...

  7. [CareerCup] 18.1 Add Two Numbers 两数相加

    18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...

  8. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  9. CareerCup All in One 题目汇总 (未完待续...)

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

随机推荐

  1. DP大作战——多重背包

    题目描述 在之前的上机中,零崎已经出过了01背包和完全背包,也介绍了使用-1初始化容量限定背包必须装满这种小技巧,接下来的背包问题相对有些难度,可以说是01背包和完全背包的进阶问题. 多重背包:物品可 ...

  2. Python UnicodeDecodeError

    出于对goagent的兴趣,看了python,后来又想了解一下gae,于是就按照gae python创建hello world应用程序,可是一开始就遇到这样一个问题: UnicodeDecodeErr ...

  3. 常用的JSTL函数

    下面是JSTL中自带的方法列表以及其描述 函数名 函数说明 使用举例 fn:contains 判断字符串是否包含另外一个字符串 <c:if test="${fn:contains(na ...

  4. android媒体文件扫描

    项目中可能有这样的需求:下载或导入.导出的图片.音乐等媒体文件,需要马上能在图库或本地视屏播放器中显示出来,或者要能在媒体数据库中查询到媒体文件的相关信息,这时我们就得主动通知系统扫描新的媒体文件了. ...

  5. Nodejs断言测试

    var assert = require('assert');/*node中,我们可以使用assert模块来测试代码.equal()和notEqual()分别作相等性和不等性的判断,第一个参数是期望值 ...

  6. 标准IO的缓冲问题

    在看APU时,第8章进程时, #include <stdio.h> #include <unistd.h> ; char buf[] = "a write to st ...

  7. iTOP-4412开发板---Linux系统学习下载步骤

    本文转自迅为论坛:http://www.topeetboard.com 1.cd /home/topeet/Linux-simple/console  下建立.c文件 2. 编译命令,就在此目录下 # ...

  8. Web安全--XSS模版

    [XSS基本探测pyload]   <script>alert("xss")</script> <script>alert(/xss/)< ...

  9. WEB安全--Google Hacking

    通常我们用Google查询一些我们测试站点的一些信息,Google提供了一系列的搜索语句,下面我为大家详细的介绍一下! 常用语法: site:指定域名 intext:正文中存在关键字的网页 intit ...

  10. 怎样用ZBrush快速雕刻皮肤纹理

    今天的ZBrush教程我们将对利用基础笔刷制作出的“亡灵僵尸”头部模型进行皮肤纹理的处理,主要用到了Layers 3D图层和Alpha笔触类型添加皮肤纹理. 详细的视频教程地址可前往:http://w ...