It is a very difficult problem to solve see http://tex.stackexchange.com/questions/4507/avoiding-rivers-.... With TeX most of the time you will not see any rivers.
If you're trying to avoid rivers, then based on information after point X you might find that your sequence of breaks B wasn't actually the best after all, because it ended up interacting badly with later spaces and producing rivers. So you can no longer prove that you've achieved a global optimum of the penalty function.
Of course you may be willing to take this hit and settle for non-optimum line-breaking (and you still might do better than an algorithm that doesn't take rivers into account at all).