สิ่งที่ถูกล้อมรอบ


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 คือสิ่งที่ "ล้อมรอบ" บล็อกข้อความ
  • # จะอยู่ติดกันเสมอ (รวมทั้งแนวทแยงมุม)
  • # จะสร้างรูปร่างแบบปิดเสมอ
  • จะมี#รูปร่างเดียวเท่านั้น
  • ในกรณีที่มีรูปร่างเว้าควรเติมช่องว่าง
  • ช่องว่างจะต้องเก็บรักษาไว้ในผลลัพธ์

ตอนแรกฉันเป็นเหมือน .. แค่เอาของออก#ไปแล้วคุณไป ... แล้วมันก็ยาก
Bald Bantha

ฉันมีปัญหาในการป้อนข้อมูลใน javascript และแยกตามบรรทัดใหม่ ... ฉันจะต้องป้อนข้อมูลได้อย่างไร มันสามารถจัดรูปแบบด้วย\nหลังจากแต่ละบรรทัดของการป้อนข้อมูลและส่งผ่านเป็นฟังก์ชั่นพารามิเตอร์เพื่อโปรแกรมของฉันหรืออะไร
Bald Bantha

1
ชุดอักขระอินพุตที่ถูกต้องคืออะไร
Ton Hospel

มีข้อผิดพลาดในผลลัพธ์ของตัวอย่างMNหรือไม่ เอาต์พุตประกอบด้วยข้อความล้อมรอบเท่านั้น_M_\n___N(ใช้เครื่องหมายขีดเส้นใต้แทนช่องว่างเนื่องจากปัญหาการจัดรูปแบบ) ในขณะที่ในตัวอย่างabcและCodegoเอาต์พุตยังมีช่องว่างที่ #s อยู่ในอินพุต หากข้อความที่ล้อมรอบด้วย #s เท่านั้นที่จะพิมพ์ดังนั้นเอาต์พุตของตัวอย่างabcควรเป็น_a_\n_b_c_(แทนที่จะเป็น__a_\n_b_c) และผลลัพธ์ของตัวอย่างCodegoควรเป็นCo\nde\n_go(แทนที่จะเป็นC___o\nd___e\n__go)
14904 epidemic

@epidemian อาจับได้ดี ฉันได้แก้ไขMNตัวอย่างแล้ว เนื่องจากไม่ควรเว้นวรรคหลัง M.
Downgoat

คำตอบ:


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

@ mbomb007: ใช่เนื่องจากพื้นที่มีการหมุนซ้ำ ๆ ดังนั้นจึงสามารถเดินตามทางเดินที่คดเคี้ยวได้ การวนลูปหยุดเร็วเกินไปก่อนที่จะลดผนังหนามากเป็นข้อบกพร่องเท่าที่ฉันรู้
Ton Hospel

@ mbomb007: Aaaaand ข้อบกพร่องของกำแพงหนาได้รับการแก้ไขแล้ว
Ton Hospel

การคัดลอกวางโซลูชันของคุณตามที่เป็น (ไม่แทนที่ตัวอักษรที่ใช้ Escape) เอาต์พุตเป็นเพียงอินพุตที่มีการ#ปล้นทั้งหมด โปรดตรวจสอบเซสชั่นทุบตีฉัน: codepad.org/YbCzB4O4
ardnew

@ardnew: โอ๊ะโอขอโทษ สำหรับการอัปเดตครั้งล่าสุดไม่ได้ลองชิมวิธีแก้ปัญหาเต็มรูปแบบและฉันควรจะแทนที่ในขณะที่จนกว่า แก้ไขแล้วโปรดลองอีกครั้ง
Ton 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
ประกาศตัวแปรภายในสำหรับลูปภายนอกด้วยy=z=0
Bálint
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.