[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 ...
随机推荐
- Stronger (What Doesn't Kill You)
今天听一个歌曲,挺不错的.以前一直不知道意思.这次把歌词摘抄下来. 试听音乐: 原版MV: You know the bed feels warmer 你知道被窝里的温暖 Sleeping here ...
- 强制IE使用最高版本引擎渲染页面,避免默认使用IE7引擎导致的页面布局混乱及其它问题
背景 基于Asp.net MVC的一个Intranet web application, 现象 Application发布到服务器端后,在客户端IE访问页面布局混乱,并有javascript报错 原因 ...
- (ios实战)单个ViewControl适配不同ios版本xib文件实现
xcode5 中的界面布局 根据sdk 分成ios7.0 and Later 和 ios6.1 and Earlier 两种,那如何xib同时支持 ios6 和ios7 的界面呢 方法如下: 在xco ...
- jQuery Validate 表单验证插件----Validate简介,官方文档,官方下载地址
一. jQuery Validate 插件的介绍 jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆 ...
- 如何通过js实现图片预览功能
一.效果预览 效果图: 二.实现代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...
- Altium Designer PCB制作入门实例
概要:本章旨在说明如何生成电路原理图.把设计信息更新到PCB文件中以及在PCB中布线和生成器件输出文件.并且介绍了工程和集成库的概念以及提供了3D PCB开发环境的简要说明.欢迎使用Altium De ...
- linux alarm函数解除read write等函数的阻塞
看到apue的第十章,说到alarm,pause可以实现sleep,可以让某些一直阻塞的函数超时,例如read,write.代码如下: static void sig_alrm(int signo) ...
- [嵌入式开发板]iTOP-4412开发板linux 系统存储空间的修改
平台:iTOP-4412开发板 这里我们以修改成 1G 存储空间为例来讲解修改方法, 如果需要改 成其他大小的存储空间,参照此方法修改即可. 首先连接好 iTOP-4412 开发板的调试串口到 pc ...
- c++字符串互相转换
1.string vs char* //string to char* string str; const char* cch = str.c_str(); ]; strcpy(ch,cch); // ...
- html5 实现video标签的自定义播放进度条
现在随着html5的渐热,越来越多的web开发者都开始选择使用html5写出一些比较好的web应用. html代码: <!DOCTYPE html> <html lang=" ...