If the only crossings permitted are bridges, then this is easily solved. A bridge needs:
- an ascending section of track;
- a raised but level section of track;
- a descending section of track
Presumably you have a set number of support pieces for these, so if you allow any piece of track to alter the z co-ordinate if necessary, you just need to keep track of how many times you've changed z (ie how many ascents and descents you have) and how many times z has remained constant but non-zero (ie how many raised level sections you have). Detecting when you need a bridge to cross over another piece of track is easy.
If you can have flat cross-overs, then you need to model the crossover as a special case.