被包围的东西


18

我一直想用#s 包围一些文本,但是我很难弄清楚我所包围的内容,因此在此挑战中,您将编写一个程序来做到这一点

例子

输入/输出用换行符分隔。

###
#a#
###

a
 #
#a#
 #

a
  ###  
 # a #
# b c #
#######

  a 
 b c 
ABCDHIJ
E####GK
F# M #L
#   N#O
P####

  M 
   N
###A###
#C#B#o#
#d###e#
 # go#
  ###

C   o
d   e
  go

规格

  • #s是什么“包围”文本块
  • # 将始终彼此相邻(包括对角线)
  • # 总是会形成一个封闭的形状
  • 只会有一种#形状
  • 如果是凹形,应在孔中填充空格。
  • 必须在输出中保留空格

起初我就像..只是拿出#s然后就走了...然后变得很难了。
Bald Bantha

我在javascript中获取输入并按换行符分割时遇到问题...我该如何获取输入?可以\n在每行输入后用a格式化并作为函数参数传递给我的程序吗?
Bald Bantha

1
什么是有效输入字符集?
Ton Hospel

MN示例的输出是否有错误?它的输出仅包含被包围的文本_M_\n___N(由于格式问题,使用下划线而不是空格),而在abcCodego示例中,输出还包括空格,其中在输入中包含#。如果仅打印用#括起来的文本,则abc示例的输出应为_a_\n_b_c_(而不是__a_\n_b_c),而Codego示例的输出应为Co\nde\n_go(而不是C___o\nd___e\n__go)。
流行病2016年

@epidemian啊,不错。我已经解决了这个MN例子。因为在M之后不应该有多余的空间
。– Downgoat

Answers:


6

Perl中,144 138 132 129 128 127 126 124字节

包括+2 -p0

该代码假定\0不是有效的输入字符(至少在内#)。

使用STDIN上的输入运行:

surround.pl < surround.txt

surround.pl

#!/usr/bin/perl -p0
/^#[^#\0]/m&&s/^|[^#\n\0]\0/\0\0/mg,s%.%s/.(.*)/$+\0/g;/#/&&reverse"\n",/^./mg%seg until$?++<$$_++;y/\0/#/;s/^#*\n|#+$|^#//mg;y;#; 

该代码按原样运行,但是用\0和替换\n为要求分数的文字版本。请注意,该行的末尾有一个空格。该代码循环太多次,因此您可能需要等待30秒左右才能输出。

说明

我要进行洪水填充,\0#从外面沿正交方向停下来。之后,我将切掉#边并替换所有剩余的空格。为避免必须处理洪水填充中的所有方向,我将反复旋转目标区域,仅从右向左进行洪水填充

/^#[^#\0]/m                   The rotation is written such that it slices
                              off the first column. That is ok unless the
                              first column contains a # that is followed by
                              something that could be the inside. There is
                              no newline inside the [] because short lines
                              will get extended during the rotation and 
                              the character following the # will end
                              up as a \0 and match in a later round
    &&s/^|[^#\n\0]\0/\0\0/mg  In case the # could be an interior border I
                              will add two columns of \0's in front. One 
                              will be a sacrifice for the rotation, the
                              other column will end up at the end of the area
                              after two rotations and function as seed for the
                              floodfill. This regex also does one step of
                              the floodfill from the back to the front.
                              After a certain number of loops we are certain
                              to get to a first column that must not be 
                              dropped so at some point the last column is 
                              guaranteed to consist of only \0. And we only need
                              to fill backward since the rotations will make
                              any direction backward at some point

s%.%  process column  %seg    I will replace each character (including \n)
                              in the string by the next column in reversed
                              order or an empty string if there are no more
                              interesting columns. This is therefore a right
                              rotation. There are less columns than
                              characters so this loop is long enough

    s%.%s/.(.*)/$+\0/g        Remove the next (now first) character from each
                              row (so remove the column). Because the
                              original area is not necessarily a rectangle
                              add a \0 at the end of the row so we won't run
                              out out of columns (this would cause shorter
                              rows to have no entry in the new rotated row)
                              This will not do anything for empty lines so
                              they DO get squeezed out. But that is not a 
                              problem since the problem statement says there
                              will be only one # shape so any empty lines
                              are can be safely dropped (this would not be
                              so if there could be multiple # shapes because
                              that could create a new surrounded area

    /#/                       Check if any of the remaining columns still 
                              has a #. If not all remaining columns are on 
                              the outside and can be dropped
       &&reverse"\n",/^./mg   Collect the column and add a \n to its reverse

 until$?++<$$_++              Keep doing this until we get to a multiple of
                              65536 rotations when $? waraps back around to 0
                              (this is a multiple of 4 so the area is left
                              unrotated) and an area we have seen before
                              ($$_ >= 1)
                              (so all slicing and flood filling is finished)
                              $_ having been seen in a previous rotations is
                              not a problem (though rather tricky to prove)

在这一点上,例如

AB##J
E####GK
F# M #L
#   N#O
P####

将被替换为:

0000000
0####00
0# M #0
#   N#0
0####00

基本上,所有不直接与内部接壤的列和行都被切掉了。剩下的所有外部字符都已替换为\ 0。在顶部和右侧有一个额外的\ 0层。所以剩下的就是清理:

y/\0/#/                       Replace any outside that is left by #
s/^#*\n|#+$|^#//mg            Removes the first two and last line (the only 
                              lines that can consist of purely #)
                              Removes any trailing #
                              Removes the first column of #
y;#; \n;                      Replace any remaining # by space since they 
                              are needed to fill the concave parts
                              The final \n; is not written since it is implicit
                              in the -p loop

如果有的话,您的垃圾填埋场是否在内部角落工作?
mbomb007 '16

@ mbomb007:是的,因为该区域反复旋转,所以它可以跟随任何曲折的走廊。据我所知,唯一的缺陷是在减少厚壁之前就停止循环太早
Ton Hospel

@ mbomb007:Aaaaand现在解决了厚壁缺陷
Ton Hospel '16

按原样复制粘贴解决方案(而不替换转义的字符),输出只是去除了所有内容的输入#。请验证我的bash会话:codepad.org/YbCzB4O4
ardnew

@ardnew:糟糕,抱歉。对于最后一次更新并没有放弃完整的解决方案,因此我应该将其替换为直到。现在固定的,请重试
吨Hospel

4

JavaScript中,485个464 427 417 396 390字节

s='indexOf';k='lastIndexOf';h="#";t=b=>b[0].map((x,i)=>b.map(x=>x[i]));i=>{m=i.split`
`;for(h of m){m[m[s](h)]=h.split``;}for(y=0;y<m.length;y++){for(z=x=0;x<m[y].length;x++){if(m[y][x]==h)break;if(m[y][s](h)<x&&m[y][k](h)>x)z++;q=t(m);if(q[y][s]h)<x&&m[y][k](h)>x)z++;if(z>2)m[y][x]=h}}for(p of m){v=p.join``.match(/\S/);e=v?p.join``:'';m[m[s](p)]=e;}m=m.join`
`;return m.replace(#/g," ")}

是。我试过了。而且,尽管我有485个字节,但我还是赢了,因为没有人愿意回答这个问题。所以,哈!
而且,我很清楚自己可以打高尔夫球,但现在我很累... 好了,现在我是396岁,感谢Conor的大部分打高尔夫球...:D


1
声明变量里面的for循环与外y=z=0
巴林特
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.