:"ct32>*TTYatFTEqYSy~*+c
输入格式为
2
{'X X DD', ' X D D', 'X X DD'}
所以另一个例子是
1
{'###', '###'}
输出包含额外的空格,这是质询允许的。
在线尝试!
如果可接受2D字符数组作为输入(我问过OP两次...),则c
可以删除第一个字符数组,因此23个字节:
:"t32>*TTYatFTEqYSy~*+c
在这种情况下,输入格式为(所有字符串的长度均相等,这可能需要右填充空格):
2
['X X DD '; ' X D D'; 'X X DD ']
在线尝试!
说明
: % Input number n implicitly. Generate [1 2 ... n]
" % For loop: repeat n times
c % Convert to char array. In the first iteration it inputs a cell array of
% strings implicitly and converts to a 2D char array, right-padding with
% spaces. In the next iterations it does nothing, as the top of the stack
% is already a 2D char array
t32>* % Convert characters below 32 into 0
TT % Push array [1 1]
Ya % Pad the 2D char array with one zero in the two directions (up/down,
% left/right), on both sides
t % Duplicate
FTEq % Push array [-1 1]
YS % Circularly shift the 2D char array one unit up and one unit right
y % Push a copy of the non-shifted 2D array
~ % Logical negate: nonzero entries become 0, zero entries become 1. This
% will be used as a mask for entries that need to be changed. Since the
% values at those entries are zero, we can simply add the new values. We do
% that by multiplying the mask by the shifted array and adding to the
% non-shifted array
* % Multiply element-wise
+ % Add element-wise
c % Convert the 2D array back to char
% End for
% Implicitly display