By inspection, your implementation is O(n^3), and this is assuming that nothing is pushed back onto @queue inside of the most inner for-loop.
You say it is the same algorithm as the one implemented in Pascal, but is it the same IMPLEMENTATION of this algorthm, and is there anyway to reduce the complexity? Perhaps you can post some psuedo code of the algorithm or the pascal code.