[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 ...
随机推荐
- Jmeter代理录制脚本
录制的原理: 1.LR/Jmeter录制是针对网络通讯协议层面的,它只关心客户端与服务器端的通讯包2.LR/Jmeter的并发测试实际上就是并发客户端与服务器端的通讯过程3.压力是通过多进程/多线程方 ...
- python基本数据结构-字典-方法
- 读书笔记——Windows核心编程(2)禁止C运行时触发的所有Debug Assertion Failed对话框
1 定义一个函数 void _invalid_parameter( const wchar_t * expression, const wchar_t * function, const wchar_ ...
- ajaxFileUpload文件上传
一.简介 ajaxFileUpload是一种异步的文件上传控件,通过ajax进行文件上传,并获取上传处理结果.在很多时候我们需要使用到文件上传的功能,但是不需要使用那些强大的上传插件.此时就可以使用a ...
- SQL SERVER 2012 使用订阅发布同步数据库
软件做大了,客户就多了,一个数据库服务器是远远不够的,当有一台数据服务器卦掉,那整个系统就会崩溃,所以必须考虑到数据库的自动同步与备份,当一台数据库服务 器宕机,自然就有用一台数据服务器启动起来保证整 ...
- lvs realserver 配置VIP
# $# 表示提供到shell脚本或者函数的参数总数: # 1表示只有一个参数. #/bin/bash #file: tun_RS.sh if [ $# -ne 1 ]; then echo “usa ...
- 迅为4412开发板支持AVIN视频输入/AV监控摄像头输入模块
AVIN模块(iTOP-4412开发板专用) 产品介绍:视频输入/AV监控摄像头输入模块: 该模块及配套的软件为开发视频采集.监控.车载后视等产品提供了很好的参考. iTOP-4412 开发平台 开发 ...
- selenium获取html的表格单元格数据
获取网页的表格的某个单元格的值,思路: 1.获取表格 2.获取表格的所有行 3.根据某一行获取该行的所有列 4.根据某一列获得该行该列的单元格值 根据以上思路,可以知道,只需要行.列就可以得到单元格的 ...
- Mysql如何清空数据库的所有表数据
1.先查询出库中的所有表,“db”是数据库名称 SELECT CONCAT('truncate table ',TABLE_NAME,';') AS a FROM INFORMATION_SCHEMA ...
- 【温故而知新-CSS】使用CSS设计网站导航栏
body #nav li a { width: auto; } #nav li a:hover { background-color: #ffcc00; color: #fff; border-rig ...