これを試すことができるかもしれません:
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。垂直方向の場合、4 つの prev 値のみを確認します。
また、4と5のような境界値の特別な条件を忘れないでください。それらは形を作りません。