I have saw a matching algorithm they based on distance metric. Euklid in n-dimensional room. You are only 2 dimensional, so You can calculate simply vectors enter link description here
the principle behind is to have a pool, calculate every distances and then go thru and remove every find pair from the pool. So in this example you cannot match B1 again, because they is out.
The algorithm calculate a vector from the Pool array I found this, only fragments, I think this is Python :
diffVec[count] = np.power(np.sum(np.power(currentDiff,c)/numBins),1/c)
Module numpy