例如,让我们看看以下ASCII艺术:
/\ - encloses a total of 2 cells - half a cell per slash
\/
/\ - encloses 4 cells fully and half of 8 cells, for a total of 8
/ \
\ /
\/您的挑战是编写一个程序来确定(并输出) ASCII艺术所包含的全部区域,它仅由空格、斜线和换行符组成。斜线不一定是非零区域形状的一部分。一个点被定义为封闭的当且仅当它不能从艺术的包围框之外的任何一点到达,如果斜线是无法通过的。
斜线的面积为零,单元格被假定为1\times1平方。/s代表连接左下角和右上角的线,而\s代表连接左上角和相应单元格右下角的线。空格表示空空间。
/\/\
\ \
\/\/完全包围3个细胞,部分封闭10个细胞,共8个。
///\\\
// \\
/ /\ \
\ \/ /
\\ //
\\\///完全包围12个细胞(四个最里面的斜线有两个对应的半细胞封闭)和一半的12个细胞,总共18个细胞。
/\/\
/\/\
/\/\包围0个细胞。
/\
/ \
\/\/
/\/\
\ /
\/完全包围8个细胞,部分封闭12个细胞,共14个。
/\
/ \
/ \ \
/ / \
\ \/ \/
\ \ /
\ \
\/\/覆盖25个细胞,18个细胞的一半,面积为34个。
这是标记密码-高尔夫,所以最短的答案获胜。
发布于 2020-06-15 02:32:07
12÷⍨≢⍸1=(⍉∘⌽2(⌈∧⊢)/2,⊢)⍣4⍣≡⊃⍪/,/({(∘.∨⍨1=3|⍳4)(⌽⍵)⍵}∘.≠⍨⍳4)[' /'⍳⎕]12÷⍨≢⍸1=(⍉∘⌽2(⌈∧⊢)/2,⊢)⍣4⍣≡⊃⍪/,/({(∘.∨⍨1=3|⍳4)(⌽⍵)⍵}∘.≠⍨⍳4)[' /'⍳⎕]
⊃⍪/,/({(∘.∨⍨1=3|⍳4)(⌽⍵)⍵}∘.≠⍨⍳4)[' /'⍳⎕] ⍝ Preprocessing
( ) ⍝ Create 3 bitmasks
∘.≠⍨⍳4 ⍝ Negated identity matrix of size 4
{ (⌽⍵)⍵} ⍝ Strand with its reflection, and
(∘.∨⍨1=3|⍳4) ⍝ Self OR outer product of 1 0 0 1
[' /'⍳⎕] ⍝ Convert three chars ' /\' to respective bitmasks
,/ ⍝ Join horizontally adjacent arrays horizontally
⍪/ ⍝ and vertically adjacent ones vertically
⊃ ⍝ Remove nesting
12÷⍨≢⍸1=(⍉∘⌽2(⌈∧⊢)/2,⊢)⍣4⍣≡ ⍝ Flood fill from the outside, and find the answer
( 2,⊢) ⍝ Prepend 2 on each row
2(⌈∧⊢)/ ⍝ Pairwise reduce: (x,y)→lcm(max(x,y),y)
⍝ Effectively, if left is 2 and right is nonzero, make it 2;
⍝ keep the right one otherwise
⍉∘⌽ ⍝ Rotate the matrix 90 degrees
⍣4⍣≡ ⍝ Repeat on the four sides, until the flood fill is complete
12÷⍨≢⍸1= ⍝ Count ones, and divide by 12https://codegolf.stackexchange.com/questions/205988
复制相似问题