I make a pretty large
assumption about the size of the cars for this algorithm. They
can not be long enough to cover more than one cell at a time.
If they were, this would be a much greater problem.
My idea on this one is simply to check
the count on the cells. I start with the photocell the train
just hit and move backward following the track layout data
structures. Essentially I use the track data structure as a
linked list to traverse the track. I check if the cell is
allocated to the train. If it is, I check what it's count
is. If I get three cells in a row with the same count, I have
a decoupled car. Another condition that tells if a car is
uncoupled is having 2 cells with the same count and the one preceding
those two has a count one or more greater.
Having two cells in a row with the same
count does not indicate an uncoupled car. I do not de-allocate
a cell until the cell after it has been passed over by the train as
well. This means that for a small time (however long it takes
for the last car to pass over the cell) two cells have the same
count.
|