Gym 100507I Traffic Jam in Flower Town (模拟)

本文介绍了一个交通灯模拟问题,模拟不同方向车辆通过十字路口的过程,考虑左转车辆需等待对向车辆通过的情况,通过直接模拟的方法计算所有车辆通过所需时间。

Traffic Jam in Flower Town

题目链接:

http://acm.hust.edu.cn/vjudge/contest/126546#problem/I

Description


Having returned from Sun City, Dunno told all his friends that every shorty may have a personal
automobile. Immediately after that so many citizens took a fancy of becoming road-users, that Bendum
and Twistum had to make a mass production of cars on soda water with syrup. Now traffic jams from
several cars occasionally appear on the crossing of Bell-flower Street and Daisy Street.
Bell-flower Street goes from the South to the North and has two driving paths. It has the right driving,

i. e. automobiles move from the South to the North on the Eastern path and from the North to the South
on the Western path. Daisy Street is single-pathed, and it is perpendicular to Bell-flower Street. There is
one-way movement on it, but its driving direction is organized in such a way that automobiles drive away
from the crossroad in two opposite directions (see the picture).
Yesterday on his way home Dunno saw cars standing in a traffic jam
on Bell-flower Street from different sides of the crossing with Daisy
Street. Some of the drivers wanted to go forward, some wanted to turn
right or left. An automobile can pass the crossing in one second, but if
the driver is turning left, he first have to let pass all oncoming vehicles,
going forward and to the right. How many seconds did it take all the
cars to pass the crossing, providing that no other cars drove up to the
crossing?

Input


The first line contains the sequence of symbols “F”, “L” and “R”, describing directions in which drivers who
arrived to the crossing from the South wanted to go. “F” stands for those drivers who were going forward,
“L” is for those who were turning left, and “R” is for those who were turning right. Automobiles are listed
in the order from the closest to the crossing to the farthest one. The second line contains the description
of the cars, arrived to the crossing from the North, in the same form. Both sequences have length from 1
to 1 000.

Output


Output time in seconds, which took all the cars to pass the crossing.

Examples


RLF
FF
4
L
L
1

Explanation


In the first example we number the cars from 1 to 5 in the order described in the input data. Then
in the first second the crossing was passed by the first and the fourth cars because they didn’t cause
an obstruction to each other. Then the second car was turning left and had to let the fifth car pass. As
a result, at each of the following three seconds only one car passed the crossing, and their order was as
follows: the fifth one, the second one and the third one.
In the second example the cars didn’t cause any obstruction to each other and turned simultaneously.


题意:


如图所示的道路通行方向.
左转时要先让对面的直行和右转先走.
给出两个方向来的车辆将要走的方向.
给出总共需要的时间. (不冲突的方向可以同时走).


题解:


直接模拟一遍就可以了.
这题读题比做题要难呀.
队友写的代码,我就不重复写了,挂上来做个记录.


代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

char s1[1005], s2[1005];
int main() {
    gets(s1); gets(s2);
    int len1 = strlen(s1);
    int len2 = strlen(s2);
    int pos1 = 0, pos2 = 0;
    int ans = 0;
    while (1) {
      if (pos1 == len1 && pos2 == len2) break;
      if (pos1 == len1) {ans++; pos2++; continue;}
      if (pos2 == len2) {ans++; pos1++; continue;}
      if (s1[pos1] == 'F' && s2[pos2] == 'F') {
        pos1++, pos2++; ans++; continue;
      }
      if (s1[pos1] == 'R' && s2[pos2] == 'R') {
        pos1++, pos2++; ans++; continue;
      }
      if (s1[pos1] == 'R' && s2[pos2] == 'F') {
        pos1++, pos2++; ans++; continue;
      }
      if (s1[pos1] == 'F' && s2[pos2] == 'R') {
        pos1++, pos2++; ans++; continue;
      }
      if (s1[pos1] == 'L' && s2[pos2] == 'L') {
        pos1++, pos2++; ans++; continue;
      }
      if (s1[pos1] == 'L') {
        pos2++; ans++; continue;
      }
      if (s2[pos2] == 'L') {
        pos1++; ans++; continue;
      }
    }
    printf("%d\n", ans);

  return 0;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5746652.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值