Okay, there is the brute force approach with pruning. This takes O(N^3)
For ease of demonstration, I will go through an N-by-N square that has the sum of a and b
S:
+ | 4 5 6
--|-------
1 | 5 6 7
2 | 6 7 8
3 | 7 8 9
And I am looking to build c={6,7,8}
I find a '6' in S. I remove it, and mark its row and column as unavailable
S:
+ | 4 5 6
--|-------
1 | / X /
2 | 6 / 8
3 | 7 / 9
Solution = { (1,5) }
Then I try to find a '7'
S:
+ | 4 5 6
--|-------
1 | / X /
2 | / / 8
3 | X / /
Solution = { (1,5) (3,4) }
And finally the '6'
S:
+ | 4 5 6
--|-------
1 | / X /
2 | / / X
3 | X / /
Solution = { (1,5) (3,4) (2,6) }
The 1st loop ( the one for '6' ) will continue and find another match : (2,4). This will then form the second solution { (2,4) (1,6) (3,5) }
Now, One way to improve this is, use some dynamic-programming: find out all possible combinations that give the result beforehand.
Given c={ 6 7 8}, create sets S_x where x is {6,7,8} and
S_x = { (i,j) } such that S[i][j]=x
So:
S_6 = { (1,2) (2,1) }
S_7 = { (1,3) (2,2) (3,1) }
S_8 = { (2,3) (3,2) }
And now, the same algorithm with given heuristics will run in O(S_l1 * S_l2 * ... S_lN), where S_li denotes the length of S_i.
This may run a factor faster in the average case.
It will also handle the c={7,7,7} case properly.
That's pretty much all I got.
a = [5]*100
,b = [10]*100
andc = [15]*100
, is there one solution or ~100! solutions? – Laius