您将得到一个宽度为的方阵,其中包含平方数。
您的任务是使所有平方数“爆炸”,直到它们都消失。您必须打印或返回最终矩阵。
进一步来说:
- 在矩阵中寻找最高的平方。
- 寻找其最小的相邻邻居(水平或垂直且不环绕)。
- 将替换为,将替换为。
重复从步骤1开始的过程,直到矩阵中不再有正方形为止。
例
输入矩阵:
最高的正方形爆炸成两个部分,并与最小的邻居合并,变成:
最高的方块爆炸并与最小的方块合并:
The highest square explodes and merges with its smallest neighbor :
The only remaining square explodes and merges with its smallest neighbor :
There's no square anymore, so we're done.
Rules
- The input matrix is guaranteed to have the following properties:
- at each step, the highest square will always be unique
- at each step, the smallest neighbor of the highest square will always be unique
- the sequence will not repeat forever
- The initial matrix may contain 's, but you do not have to worry about making explode, as it will never be the highest or the only remaining square.
- I/O can be processed in any reasonable format
- This is code-golf
Test cases
Input : [[16,9],[4,25]]
Output: [[24,6],[20,5]]
Input : [[9,4],[1,25]]
Output: [[3,12],[5,5]]
Input : [[625,36],[196,324]]
Output: [[750,540],[14,252]]
Input : [[1,9,49],[1,4,1],[36,25,1]]
Output: [[3,6,7],[6,2,7],[6,5,5]]
Input : [[81,4,64],[16,361,64],[169,289,400]]
Output: [[3,5472,8],[624,323,1280],[13,17,20]]
Input : [[36,100,1],[49,144,256],[25,49,81]]
Output: [[6,80,2],[42,120,192],[175,21,189]]
Input : [[256,169,9,225],[36,121,144,81],[9,121,9,36],[400,361,100,9]]
Output: [[384,13,135,15],[24,1573,108,54],[180,11,108,6],[380,209,10,90]]
Input : [[9,361,784,144,484],[121,441,625,49,25],[256,100,36,81,529],[49,4,64,324,16],[25,1,841,196,9]]
Output: [[171,19,700,4032,22],[11,210,525,7,550],[176,60,6,63,23],[140,112,1152,162,368],[5,29,29,14,126]]
You must print or return the final matrix.
Can I modify the input matrix instead?