This algorithm is wrong. The premise for B to get the correct color (blue) is that A doesn't get a blue hat, and the probability of A not getting blue is 2/3, so the probability of B getting blue is 2/3× 1/2= 1/3. ...
In fact, imagine this problem as the calculation process of a, b and c taking hats in order should be
(1/3) × (2/3×1/2 )× (2/3×1/2×1) =1/27 ... That is to say, the probability of each person getting the correct color hat.
I don't know if you understand, but you can leave me a message if you have any questions.