We propose a novel algorithm to solve the Routing and Spectrum Allocation (RSA) problem in dynamic flexible grid optical networks. Unlike most previous proposals, the algorithm solves the R and SA problems jointly by exhaustively searching the solution space and taking the network state into account. As a result, the shortest possible path with enough spectrum availability is allocated to establish the connections. Simulation results show that, in terms of blocking ratio, our proposal significantly outperforms previously proposed algorithms. In some cases, the performance is better by more than one order of magnitude.