We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
'''1. Swaps that happen between any 2 buckets are always of equal value (you can exchange 1 for 1 but not 2 for 1, ratio should always be 1:1), therefore the number of balls in each bucket will always remain the same. 2. We need to make sure that the total number of balls in each container should equal the number of balls of any type 't' for a fair trade(1:1 ratio). This is done to check whether there are any type 't' balls that satisfy the requirements of the solution (i.e. we want all balls of a single type 't' in only one container).3. Calculate the number of balls in each container(sum of rows) and store it in a list.4. Calculate the number of balls of each type(sum of columns) and store it in a list.5. Sort the two lists and compare them. If they are equal then a solution exists otherwise, no solution exists.Try theses cases to see and understand the pattern:Possible:21 00 121 22 130 4 02 0 11 0 241 2 3 42 1 4 33 4 2 14 3 1 240 0 5 04 0 0 00 2 0 10 1 0 2Impossible:22 10 022 10 131 2 33 2 12 3 131 0 00 2 00 2 041 2 1 32 1 3 11 3 2 13 2 1 1Note: For more test cases try Solved Sudoku puzzles.Here is Python2 Solution:'''q=int(raw_input().strip())forxinxrange(q):n=int(raw_input().strip())m=[]rowsum=[0]*ncolsum=[0]*nforiinxrange(n):m.append(map(int,raw_input().strip().split(' ')))rowsum[i]=sum(m[-1])colsum=map(lambdaa,b:a+b,colsum,m[-1])rowsum.sort()colsum.sort()ifrowsum==colsum:print'Possible'else:print'Impossible'
Organizing Containers of Balls
You are viewing a single comment's thread. Return to all comments →