描述:

设计一个停车场
1. 一共有n层,每层m列,每列k个位置
2.停车场可以停摩托车,公交车,汽车
3.停车位分别有摩托车位,汽车位,大型停车位
4.每一列,摩托车位编号范围为[0,k/4),汽车停车位编号范围为[k/4,k/4*3),大型停车位编号范围为[k/4*3,k)
5.一辆摩托车可以停在任何停车位
6.一辆汽车可以停在一个汽车位或者大型停车位
7.一辆公交车可以停在一列里的连续5个大型停车位

样例:

level=1, num_rows=1, spots_per_row=11
parkVehicle("Motorcycle_1") // return true
parkVehicle("Car_1") // return true
parkVehicle("Car_2") // return true
parkVehicle("Car_3") // return true
parkVehicle("Car_4") // return true
parkVehicle("Car_5") // return true
parkVehicle("Bus_1") // return false
unParkVehicle("Car_5")
parkVehicle("Bus_1") // return true

enum VehicleSize {Motorcycle,Compact,Large,
}//abstract Vehicle class
abstract class Vehicle {// Write your code hereprotected int spotsNeeded;protected VehicleSize size;protected String licensePlate;  // id for a vehicleprotected ArrayList<ParkingSpot> parkingSpots = new ArrayList<ParkingSpot>(); // id for parking where may occupy multipublic int getSpotsNeeded() {return spotsNeeded;}public VehicleSize getSize() {return size;}/* Park vehicle in this spot (among others, potentially) */public void parkInSpot(ParkingSpot spot) {parkingSpots.add(spot);}/* Remove car from spot, and notify spot that it's gone */public void clearSpots() {for (int i = 0; i < parkingSpots.size(); i++) {parkingSpots.get(i).removeVehicle();}parkingSpots.clear();}//this need to be implement in subclasspublic abstract boolean canFitInSpot(ParkingSpot spot);public abstract void print();
}class Motorcycle extends Vehicle {// Write your code herepublic Motorcycle() {spotsNeeded = 1;size = VehicleSize.Motorcycle;}public boolean canFitInSpot(ParkingSpot spot) {return true;}public void print() {  System.out.print("M");  }
}class Car extends Vehicle {// Write your code herepublic Car() {spotsNeeded = 1;size = VehicleSize.Compact;}public boolean canFitInSpot(ParkingSpot spot) {return spot.getSize() == VehicleSize.Large || spot.getSize() == VehicleSize.Compact;}public void print() {  System.out.print("C");  }
}class Bus extends Vehicle {// Write your code herepublic Bus() {spotsNeeded = 5;size = VehicleSize.Large;}public boolean canFitInSpot(ParkingSpot spot) {return spot.getSize() == VehicleSize.Large;}public void print() {  System.out.print("B");  }
}class ParkingSpot {// Write your code hereprivate Vehicle vehicle;private VehicleSize spotSize;private int row;private int spotNumber;private Level level;public ParkingSpot(Level lvl, int r, int n, VehicleSize sz) {level = lvl;row = r;spotNumber = n;spotSize = sz;}public boolean isAvailable() {return vehicle == null;}/* Checks if the spot is big enough for the vehicle (and is available). This compares* the SIZE only. It does not check if it has enough spots. */public boolean canFitVehicle(Vehicle vehicle) {return isAvailable() && vehicle.canFitInSpot(this);}/* Park vehicle in this spot. */public boolean park(Vehicle v) {if (!canFitVehicle(v)) {return false;}vehicle = v;vehicle.parkInSpot(this);return true;}/* Remove vehicle from spot, and notify level that a new spot is available */public void removeVehicle() {level.spotFreed();vehicle = null;}public int getRow() {return row;}public int getSpotNumber() {return spotNumber;}public VehicleSize getSize() {return spotSize;}public void print() {  if (vehicle == null) {  if (spotSize == VehicleSize.Compact) {  System.out.print("c");  } else if (spotSize == VehicleSize.Large) {  System.out.print("l");  } else if (spotSize == VehicleSize.Motorcycle) {  System.out.print("m");  }  } else {  vehicle.print();  }  }
}/* Represents a level in a parking garage */
class Level {// Write your code hereprivate int floor;private ParkingSpot[] spots;private int availableSpots = 0; // number of free spotsprivate int SPOTS_PER_ROW;public Level(int flr, int num_rows, int spots_per_row) {floor = flr;int SPOTS_PER_ROW = spots_per_row;int numberSpots  = 0;spots = new ParkingSpot[num_rows * spots_per_row];//init size for each spot in array spotsfor (int row = 0; row < num_rows; ++row) {for (int spot = 0; spot < spots_per_row / 4; ++spot) {VehicleSize sz = VehicleSize.Motorcycle;spots[numberSpots] = new ParkingSpot(this, row, numberSpots, sz);numberSpots ++;}for (int spot = spots_per_row / 4; spot < spots_per_row / 4 * 3; ++spot) {VehicleSize sz = VehicleSize.Compact;spots[numberSpots] = new ParkingSpot(this, row, numberSpots, sz);numberSpots ++;}for (int spot = spots_per_row / 4 * 3; spot < spots_per_row; ++spot) {VehicleSize sz = VehicleSize.Large;spots[numberSpots] = new ParkingSpot(this, row, numberSpots, sz);numberSpots ++;}}availableSpots = numberSpots;}/* Try to find a place to park this vehicle. Return false if failed. */public boolean parkVehicle(Vehicle vehicle) {if (availableSpots() < vehicle.getSpotsNeeded()) {return false; // no enough spots}int spotNumber = findAvailableSpots(vehicle);if(spotNumber < 0) {return false;}return parkStartingAtSpot(spotNumber, vehicle);}/* find a spot to park this vehicle. Return index of spot, or -1 on failure. */private int findAvailableSpots(Vehicle vehicle) {int spotsNeeded = vehicle.getSpotsNeeded();int lastRow = -1;int spotsFound = 0;for(int i = 0; i < spots.length; i++){ParkingSpot spot = spots[i];if(lastRow != spot.getRow()){spotsFound = 0;lastRow = spot.getRow();}if(spot.canFitVehicle(vehicle)){spotsFound++;}else{spotsFound = 0;}if(spotsFound == spotsNeeded){return i - (spotsNeeded - 1); // index of spot}}return -1;}/* Park a vehicle starting at the spot spotNumber, and continuing until vehicle.spotsNeeded. */private boolean parkStartingAtSpot(int spotNumber, Vehicle vehicle) {vehicle.clearSpots();boolean success = true;for (int i = spotNumber; i < spotNumber + vehicle.spotsNeeded; i++) {success &= spots[i].park(vehicle);}availableSpots -= vehicle.spotsNeeded;return success;}/* When a car was removed from the spot, increment availableSpots */public void spotFreed() {availableSpots++;}public int availableSpots() {return availableSpots;}public void print() {  int lastRow = -1;  for (int i = 0; i < spots.length; i++) {  ParkingSpot spot = spots[i];  if (spot.getRow() != lastRow) {  System.out.print("  ");  lastRow = spot.getRow();  }  spot.print();  }  }
}public class ParkingLot {private Level[] levels;private int NUM_LEVELS;// @param n number of leves// @param num_rows  each level has num_rows rows of spots// @param spots_per_row each row has spots_per_row spotspublic ParkingLot(int n, int num_rows, int spots_per_row) {// Write your code hereNUM_LEVELS = n;levels = new Level[NUM_LEVELS];for (int i = 0; i < NUM_LEVELS; i++) {levels[i] = new Level(i, num_rows, spots_per_row);}}// Park the vehicle in a spot (or multiple spots)// Return false if failedpublic boolean parkVehicle(Vehicle vehicle) {// Write your code herefor (int i = 0; i < levels.length; i++) {if (levels[i].parkVehicle(vehicle)) {return true;}}return false;}// unPark the vehiclepublic void unParkVehicle(Vehicle vehicle) {// Write your code herevehicle.clearSpots();}public void print() {  for (int i = 0; i < levels.length; i++) {  System.out.print("Level" + i + ": ");  levels[i].print();System.out.println("");  }  System.out.println("");  }
}

  

转载于:https://www.cnblogs.com/wangnanabuaa/p/6198118.html

Parking-lot相关推荐

  1. 【新概念二】 28-No parking

    WX公众号:电子小彭友  28-No parking Jasper White is one of those rare people who believes in ancient myths. 句 ...

  2. Intelligent Parking Building

    Intelligent Parking Building 题目描述 There is a new revolution in the parking lot business: the parking ...

  3. 【Hard to Park】Estimating Parking Difficulty at Scale

    First Pass Title Hard to Park? Estimating Parking Difficulty at Scale Author Neha Arora James Cook R ...

  4. 游戏car android,Car Parking Multiplayer游戏

    Car Parking Multiplayer游戏是一款最近比较火的手机汽车驾驶类赛车停车场多人游戏,该游戏中玩家可以选择自己喜欢的车辆,支持自定义车型和汽车颜色,支持第一视角和第三视角,多人在线玩, ...

  5. VH-HFCN based Parking Slot and Lane Markings Segmentation on Panoramic Surround View

    VH-HFCN based Parking Slot and Lane Markings Segmentation on Panoramic Surround View Abstract Method ...

  6. 自动驾驶论文: VH-HFCN based Parking Slot and Lane Markings Segmentation on Panoramic Surround View

    VH-HFCN based Parking Slot and Lane Markings Segmentation on Panoramic Surround View PDF: https://ar ...

  7. 新概念二册 Lesson 28 No parking禁止停车 ( 定语从句)

    文章目录 1 课文 2 单词 2.1 rare [reə] adj. 罕见的,稀少的 2.2 ancient [ˈeɪnʃənt] adj. 古代的,古老的 2.3 myth [mɪθ] n.神话故事 ...

  8. 【论文精读】Hybrid Bird‘s-Eye Edge Based Semantic Visual SLAM for Automated Valet Parking

    [论文精读]Hybrid Bird's-Eye Edge Based Semantic Visual SLAM for Automated Valet Parking 论文出处 ICRA2021 摘要 ...

  9. AVP-SLAM 小结: Semantic Visual Mapping and Localization for Autonomous Vehicles in the Parking Lot

    AVP-SLAM 小结: Semantic Visual Mapping and Localization for Autonomous Vehicles in the Parking Lot 1. ...

  10. 洛谷刷题C语言:Physics Problem、PARKING、Trol、信息学竞赛、POT

    记录洛谷刷题C语言 「dWoi R1」Physics Problem 题目背景 面对白板上的物理题,王马陷入了沉思 -- 题目描述 有 nnn 个状态,编号为 111 到 nnn.这 nnn 个状态之 ...

最新文章

  1. 结构体DIR和dirent
  2. eclipse 环境下整合 struts2+spring+hibernate 开发web应用常见问题及解答
  3. 碎片化趋势下手机浏览器或成赢家
  4. 无状态mysql_既然HTTP是无状态协议,mysql_close还有必要么?
  5. opencv配置_Opencv在vs2012下的配置
  6. Win11系统如何解除网络限制
  7. 为什么重写equals时必须重写hashCode方法?
  8. C语言和设计模式(策略模式)
  9. 零基础学python电子书-资料│最适合大学生零基础学的Python视频+电子书
  10. python卸载_技术 | Python 包安装和卸载的几种方式
  11. Millumin for Mac视频实时编辑软件
  12. xxl-job架构源码解析
  13. 在校大学生如何规划自己的程序员之路
  14. AV1:比HEVC/H.265更有效率的视频编码格式?
  15. 菜鸟Java远程连接腾讯云服务器上面的数据库
  16. 机器学习实战之信用卡诈骗(三)
  17. BIO、NIO、AIO网络编程
  18. SIMetrix教程-008.死区时间;Dead time
  19. 怎样调整计算机视角,电脑调节不了CAD极轴角度怎样解决|电脑中调节CAD极轴角度的方法...
  20. python求素数对(质数对)个数

热门文章

  1. 企业微信朋友圈这样定时发送!太省事了!
  2. 数字城市三维可视化技术路线
  3. 【ChatGPT恋爱攻略】情话篇篇,真诚又用心
  4. 利用GISTIC2.0整合队列CNV拷贝数变异分析结果
  5. 蒲公英应用内测、托管的平台
  6. redash源码部署
  7. 【Charles】小米手机安装证书问题
  8. 数云原力大会暨2023TECH第五届数字中国技术年会开幕
  9. postgres日志配置方式
  10. 陶哲轩实分析-第2章-从头开始:自然数