Calculate the shortest distance based on the C++ maze problem