Cody

Problem 42639. Find the Final State of an Abelian Sandpile

Let us define an Abelian sand pile as a matrix that is only in a stable and final state when all of its elements are less than 4. When any of its elements is greater than 4, distribute four sand grains, one each to the elements above, below, to the left, and to the right of the offending element. Continue doing this until the matrix is stable.

So

 0 0 0 
 0 4 1
 1 0 0

would become

 0 1 0
 1 0 2
 1 1 0

because the 4 in the middle is unstable and gets distributed.

What makes this process Abelian is that the order in which you distribute the "sand grains" doesn't matter. This gives you considerable algorithmic freedom.

So, given an input matrix A, return the resulting stable Abelian sand pile matrix B.

Note: you won't have to worry about sand grains "falling off the edge of the table." The given matrix A will always be big enough to handle the resulting stable matrix B.

Examples

 A = [ 0 0 0 0
       0 4 4 0
       0 0 0 0 ]
 B = [ 0 1 1 0
       1 1 1 1
       0 1 1 0 ]

and

 A = [ 0 0 0
       0 9 0
       0 0 0 ] 
 B = [ 0 2 0
       2 1 2
       0 2 0 ]

Solution Stats

66.04% Correct | 33.96% Incorrect
Last Solution submitted on Sep 22, 2019


Problem Recent Solvers28