当前位置:网站首页>LeetCode-1279. 红绿灯路口
LeetCode-1279. 红绿灯路口
2022-07-06 11:17:00 【边界流浪者】
这是两条路的交叉路口。第一条路是 A 路,车辆可沿 1 号方向由北向南行驶,也可沿 2 号方向由南向北行驶。第二条路是 B 路,车辆可沿 3 号方向由西向东行驶,也可沿 4 号方向由东向西行驶。
每条路在路口前都有一个红绿灯。红绿灯可以亮起红灯或绿灯。
绿灯表示两个方向的车辆都可通过路口。
红灯表示两个方向的车辆都不可以通过路口,必须等待绿灯亮起。
两条路上的红绿灯不可以同时为绿灯。这意味着,当 A 路上的绿灯亮起时,B 路上的红灯会亮起;当 B 路上的绿灯亮起时,A 路上的红灯会亮起.
开始时,A 路上的绿灯亮起,B 路上的红灯亮起。当一条路上的绿灯亮起时,所有车辆都可以从任意两个方向通过路口,直到另一条路上的绿灯亮起。不同路上的车辆不可以同时通过路口。
给这个路口设计一个没有死锁的红绿灯控制系统。
实现函数 void carArrived(carId, roadId, direction, turnGreen, crossCar) :
carId 为到达车辆的编号。
roadId 为车辆所在道路的编号。
direction 为车辆的行进方向。
turnGreen 是一个函数,调用此函数会使当前道路上的绿灯亮起。
crossCar 是一个函数,调用此函数会允许车辆通过路口。
当你的答案避免了车辆在路口出现死锁,此答案会被认定为正确的。当路口已经亮起绿灯时仍打开绿灯,此答案会被认定为错误的。
示例 1:
输入: cars = [1,3,5,2,4], directions = [2,1,2,4,3], arrivalTimes = [10,20,30,40,50]
输出: [
"Car 1 Has Passed Road A In Direction 2", // A 路上的红绿灯为绿色,1 号车可通过路口。
"Car 3 Has Passed Road A In Direction 1", // 红绿灯仍为绿色,3 号车通过路口。
"Car 5 Has Passed Road A In Direction 2", // 红绿灯仍为绿色,5 号车通过路口。
"Traffic Light On Road B Is Green", // 2 号车在 B 路请求绿灯。
"Car 2 Has Passed Road B In Direction 4", // B 路上的绿灯现已亮起,2 号车通过路口。
"Car 4 Has Passed Road B In Direction 3" // 红绿灯仍为绿色,4 号车通过路口。
]
示例 2:
输入: cars = [1,2,3,4,5], directions = [2,4,3,3,1], arrivalTimes = [10,20,30,40,40]
输出: [
"Car 1 Has Passed Road A In Direction 2", // A 路上的红绿灯为绿色,1 号车可通过路口。
"Traffic Light On Road B Is Green", // 2 号车在 B 路请求绿灯。
"Car 2 Has Passed Road B In Direction 4", // B 路上的绿灯现已亮起,2 号车通过路口。
"Car 3 Has Passed Road B In Direction 3", // B 路上的绿灯现已亮起,3 号车通过路口。
"Traffic Light On Road A Is Green", // 5 号车在 A 路请求绿灯。
"Car 5 Has Passed Road A In Direction 1", // A 路上的绿灯现已亮起,5 号车通过路口。
"Traffic Light On Road B Is Green", // 4 号车在 B 路请求绿灯。4 号车在路口等灯,直到 5 号车通过路口,B 路的绿灯亮起。
"Car 4 Has Passed Road B In Direction 3" // B 路上的绿灯现已亮起,4 号车通过路口。
]
解释: 这是一个无死锁的方案。注意,在 A 路上的绿灯亮起、5 号车通过前让 4 号车通过,也是一个正确且可被接受的方案。
提示:
1 <= cars.length <= 20
cars.length = directions.length
cars.length = arrivalTimes.length
cars 中的所有值都是唯一的。
1 <= directions[i] <= 4
arrivalTimes 是非递减的。
#include<iostream>
#include<functional>
#include<mutex>
using namespace std;
class TrafficLight {
public:
TrafficLight() {
road = 'A';
}
void carArrived(
int carId, // ID of the car
int roadId, // ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
int direction, // Direction of the car
function<void()> turnGreen, // Use turnGreen() to turn light to green on current road
function<void()> crossCar // Use crossCar() to make car cross the intersection
) {
std::lock_guard<mutex> lck(mtx);
if (direction <= 2 && road != 'A') {
turnGreen();
road = 'A';
}
else if(direction > 2 && road!='B') {
turnGreen();
road = 'B';
}
crossCar();
}
private:
char road = 'A';
std::mutex mtx;
};
边栏推荐
- Simple understanding of MySQL database
- Multithreading Basics: basic concepts of threads and creation of threads
- Human bone point detection: top-down (part of the theory)
- AutoCAD - what is the default lineweight for centerline drawing and CAD? Can I modify it?
- [depth first search] Ji suanke: Square
- 业务与应用同步发展:应用现代化的策略建议
- When visual studio code starts, it prompts "the code installation seems to be corrupt. Please reinstall." Solution to displaying "unsupported" information in the title bar
- Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services
- First day of rhcsa study
- 助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!
猜你喜欢
About static type, dynamic type, ID, instancetype
The second day of rhcsa study
视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
线代笔记....
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
Simple understanding of MySQL database
多线程基础:线程基本概念与线程的创建
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
随机推荐
五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
Airiot IOT platform enables the container industry to build [welding station information monitoring system]
The list of people who passed the fifth phase of personal ability certification assessment was published
About static type, dynamic type, ID, instancetype
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
Crawling data encounters single point login problem
第五期个人能力认证考核通过名单公布
Summary of performance knowledge points
When visual studio code starts, it prompts "the code installation seems to be corrupt. Please reinstall." Solution to displaying "unsupported" information in the title bar
2022.2.12
pytorch常见损失函数
How does crmeb mall system help marketing?
Nuc11 cheetah Canyon setting U disk startup
Mathematics in machine learning -- common probability distribution (XIII): Logistic Distribution
爬虫玩得好,牢饭吃到饱?这3条底线千万不能碰!
The role of applet in industrial Internet
AcWing 3537.树查找 完全二叉树
R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
Unlock 2 live broadcast themes in advance! Today, I will teach you how to complete software package integration Issues 29-30
关于npm install 报错问题 error 1