也许你可以试试这个:
1. Assign integer values to each position in the matrix, like this, [1,2,3,4
5,6,7,8
9,10,11,12
13,14,15,16].
2.Read the position of zeroes. (I guess, shapes correpond to '0' s located either horizontally or vertically aligned). Store them in an array. So, for first case your array will read [3,4,7,11,10]
2. Then Start 'drawing' the shape.
1. First value 3. so shape= 0.
2. Next value 4. Check if it is consecutive to any other value in the array. that value is 3. so the shape = 00
3. Next val= 7. Is it consecutive ? no. Is it 4 more than any other value? yes, 3. So it goes below 3. shape= 00
0
4. Next 11, similar to step3, it goes below 7. shape= 00
0
0
5. Next 10, it is one less than 11, so it is placed before 11. shape= 00
0
00.
你可以做一些优化,比如连续,只检查上一个。数组中的 val。对于垂直,仅检查四个 prev 值。
另外,不要忘记边界值的特殊条件,例如 4 和 5。我不知道它们会形成形状。