สร้างตัวต่อ Nonogram


24

nonogramเป็นสองมิติปริศนาตรรกะที่มีลักษณะบางอย่างเช่นนี้ (ภาพหน้าจอจากเกมPixeloเกม nonogram ที่ชื่นชอบ):

บอร์ด nonogram เปล่า

เป้าหมายของเกมคือการค้นหาว่าภาพเหล่านั้นเข้ารหัสอะไร กฎนั้นง่าย: ตัวเลขในคอลัมน์หรือแถวหมายความว่าอยู่ที่ไหนสักแห่งในคอลัมน์หรือแถวนั้นซึ่งมีหลายช่องในกล่อง ตัวอย่างเช่นแถวด้านล่างในรูปภาพด้านบนจะต้องไม่มีกล่องที่กรอกข้อมูลในขณะที่แถวด้านบนนั้นจะต้องมีกล่องทั้งหมดอยู่ในนั้น แถวที่สามจากด้านล่างมีช่องที่เต็มไป 8 ช่องและแถวทั้งหมดจะอยู่ในแถว

ตัวเลขสองตัวหรือมากกว่าสำหรับคอลัมน์หรือแถวเดียวกันหมายความว่ามีหลายช่อง "วิ่ง" ของกล่องที่เติมเต็มโดยมีช่องว่างอย่างน้อยหนึ่งช่องว่างด้วยความยาวเหล่านั้น คำสั่งซื้อจะถูกเก็บไว้ ตัวอย่างเช่นมีสามช่องที่กรอกไว้ในคอลัมน์ด้านขวาของภาพด้านบนอย่างน้อยหนึ่งช่องว่างด้านล่างแล้วมีอีกหนึ่งช่อง

นี่คือปริศนาเดียวกันที่เกือบจะเสร็จสมบูรณ์:

บอร์ด nonogram เกือบเสร็จแล้ว

(Xs ไม่สำคัญพวกเขาเป็นเพียงคำใบ้ที่ผู้เล่นออกเพื่อให้ตัวเองพูดว่า "สี่เหลี่ยมจัตุรัสนี้ไม่ได้กรอกอย่างแน่นอน" ให้คิดธงใน Minesweeper พวกเขาไม่มีความหมายของกฎ)

หวังว่าคุณจะเห็นได้ว่าตัวอย่างเช่นคอลัมน์กลางพร้อมคำใบ้ที่บอกว่า "2 2" มีการเติมเต็มความยาว 2 กล่องสองกล่อง

ภารกิจของคุณคุณควรเลือกที่จะยอมรับมันคือการเขียนโปรแกรมหรือฟังก์ชั่นที่จะสร้างตัวต่อแบบนี้ คุณจะได้รับขนาดของกระดานเป็นจำนวนเต็มเดียว (5 <= n <= 50) บน stdin หรือเป็นอาร์กิวเมนต์ (ไม่มีเหตุผลว่าทำไมปริศนา nonogram จะต้องเป็นรูปสี่เหลี่ยม แต่สำหรับความท้าทายนี้มันจะเป็น) หลังจากนั้นคุณจะได้รับชุดของ 1 และ 0 เป็นตัวแทนของสี่เหลี่ยมที่เติมและไม่ได้บรรจุในรูปภาพตามลำดับ n แรกของพวกเขาคือแถวบนสุดจากนั้นแถวถัดไป ฯลฯ คุณจะกลับมาหรือพิมพ์เพื่อ stdout คณะกรรมการของเซลล์ 2 * 1 (เพราะพวกเขาดูดีขึ้นและมันจะทำให้คุณมีที่ว่างสำหรับคำใบ้ 2 หลักสำหรับคอลัมน์ ) ทั้งหมดนั้นว่างเปล่าพร้อมด้วยคำแนะนำที่สอดคล้องกับข้อมูลอินพุต

รูปแบบผลลัพธ์

รูปแบบผลลัพธ์

ตัวอย่าง

การป้อนข้อมูล:

./nonogram <<< '5 0 1 1 1 0 1 1 0 1 1 1 0 1 0 1 1 1 0 1 1 0 1 1 1 0'
                                 OR
      n(5,[0,1,1,1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1,1,1,0])

ภาพ:

ภาพตัวอย่างแรก

เอาท์พุท:

           1
         2 1 2
       3 2 1 2 3
     +----------
    3|
  2 2|
1 1 1|
  2 2|
    3|

การป้อนข้อมูล:

./nonogram <<< '15 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 1 0 1 1 1 1 0 0 0 1 1 1 1 1 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1'

ภาพ:

ภาพตัวอย่างที่สอง

เอาท์พุท:

                   1                       1
                 1 1 3       3 5   5 3   3 1
                 7 2 3 2 4 2 3 210 2 3 0 4 215
               +------------------------------
              2|
              1|
              1|
              1|
              1|
            1 1|
        3 3 1 1|
        1 5 1 1|
          3 5 3|
          1 5 1|
          1 3 1|
      1 1 1 1 1|
1 1 1 1 1 1 1 1|
           11 3|
           11 3|

ชี้แจง

  • ผลลัพธ์ของคุณไม่จำเป็นต้องเป็นปริศนาที่แก้ไขได้ ไม่ใช่ nonograms ทั้งหมดที่สามารถแก้ไขได้ แต่นั่นไม่ใช่ความกังวลของคุณ เพียงแค่ส่งคำแนะนำที่สอดคล้องกับอินพุตไม่ว่าจะเป็นตัวต่อที่ดีหรือไม่ก็ตาม
  • อนุญาตให้ใช้โปรแกรมที่รับอาร์กิวเมนต์บนบรรทัดคำสั่ง นี่เป็นประเภทที่กล่าวข้างต้น แต่เป็นไปได้ที่จะเข้าใจผิด นั่นคือสิ่งที่ชี้แจงสำหรับ
  • การพิมพ์0แถวหรือคอลัมน์ที่ไม่ได้กรอกข้อมูลในกล่องเป็นสิ่งจำเป็น ฉันไม่ได้พูดสิ่งนี้ด้วยคำใด ๆ แต่มันอยู่ในข้อมูลตัวอย่าง

ฉันเพิ่งจะแก้ปัญหาด้วยวิธีของฉัน เราสามารถสมมติได้ว่าจะไม่มีตัวเลข 3 หลักในหนึ่งแถวหรือหนึ่งคอลัมน์?
ชัดเจน

2
@voidpigeon: 5<=n<=50เป็นข้อมูลจำเพาะดังนั้นจึงไม่สามารถมีตัวเลข 3 หลักได้
Kyle Kanos

หลังจากโพสต์คำถามนี้ฉันเริ่มทำงานแก้ปัญหาด้วยตัวเอง ฉันจะยังไม่โพสต์ (ตามคำตอบ meta นี้ ) แต่ฉันจะโพสต์จำนวนไบต์ของคุณเพื่อให้คุณมีสิ่งที่จะต้องพยายาม: 404 ไบต์ใน Python 2.7
undergroundmonorail

ผลลัพธ์ตัวอย่างแรกของคุณไม่มีมากกว่าหนึ่ง-ควร?
Ventero

@Ventro คุณถูกต้อง! ฉันรู้ว่าฉันจะเขียนโปรแกรมเพื่อทำสิ่งนี้ได้อย่างไร แต่จริง ๆ แล้วฉันไม่ได้ทำจนถึงตอนนี้ อ๊ะ! (ผมยังเมาขึ้นผลลัพธ์ตัวอย่างที่สอง แต่ฉันคงมันก่อนที่จะมีคำตอบใด ๆ .)
undergroundmonorail

คำตอบ:


9

GolfScript, 128 อักขระ

~](:k/.{{1,%{,}%.!,+}%}:^~{' ':s*}%.{,}%$-1=:9{s*\+9~)>'|'n}+%\zip^.{,~}%$0=){.~[s]*@+>{s\+-2>}%}+%zip{9)s*\n}%\[9s*'+''--'k*n]\

ต้องป้อนข้อมูลใน STDIN เป็นตัวเลขคั่นด้วยช่องว่าง

คุณสามารถทดสอบตัวอย่างที่นี่

รหัสความคิดเห็น:

# Parse the input into an 2D array of digits. The width is saved to variable k
~](:k/

# Apply the code block ^ to a copy of this array
.
{                # begin on code block
  {              # for each line
   1,%           #   split at 0s (1, => [0]) (leading, trailing, multiple 0s are 
                 #   removed because of operator % instead of /)
   {,}%          #   calculate the length of each run of 1s                 
   .!,+          #   special case: only zeros, i.e. []
                 #   in this case the ! operator yiels 1, thus [0], else []
  }%             # end for
}:^              # end of code block
~                # apply

# Format row headers
{' ':s*}%        # join numbers with spaces
.{,}%$-1=:9      # calulate the length of the longest row header
                 # and save it to variable <9>
{                # for each row
  s*\+           #   prepend padding spaces
  9~)>           #   and cut at length <9> from the right
  '|'n           #   append '|' and newline
}+%              # end for

# Format column headers
\zip^            # transpose input array and apply the code block ^
                 # i.e. calculate length of runs
.{,~}%$0=)       # determine (negative) length of the longest column header
{                # for each column
  .~[s]*@+       #   prepend enough spaces
  >              #   and cut at common length (from right)
  {s\+-2>}%      #   format each number/empty to 2 chars width
}+%              # end for
zip              # transpose column header into output lines
{9)s*\n}%        # prepend spaces to each line and append newline

# Inject separator line
\[
9s*              # spaces
'+'              # a plus sign
'--'k*           # k (width) number of '--'
n                # newline
]\

1
+1 ดีฉันได้เรียนรู้เคล็ดลับดีๆจากโพสต์นี้
Cristian Lupascu

ฉันจัดการเพื่อกอล์ฟถึง 123 ตัวอักษร: ~](:k/.zip\]{{1,%{,}%.!,+}%}/{' ':^*}%{.{,}%$-1=}:f~:r{^*\+r~)>'|'n}+%\f{.~)\[^]*@+>{^\+-2>}%}+%zip{r)^*\n}%r^*'+''--'k*n]((ด้วยเหตุผลบางอย่าง lettercount.com พูดว่า 125 ตัวอักษรถ้าคุณคัดลอก แต่ฉันรับรองคุณมันเป็น 123 ตัวอักษร) อัลกอริทึมบางส่วนมีการเปลี่ยนแปลง แต่ส่วนใหญ่ยังคงเหมือนเดิม ฉันยังเปลี่ยนชื่อตัวแปรบางตัว (การมี 9 เป็นตัวแปรเป็นสมาร์ท แต่สร้างความสับสนเช่นกัน) แต่คุณสามารถเปลี่ยนกลับได้หากต้องการ
ความผันผวน

7

ทับทิม, 216 255

n=$*.shift.to_i;k=*$*.each_slice(n)
u=->k{k.map{|i|r=i.join.scan(/1+/).map{|i|"%2d"%i.size}
[*["  "]*n,*r[0]?r:" 0"][-n,n]}}
puts u[k.transpose].transpose.map{|i|" "*(n-~n)+i*""},"  "*n+?++"--"*n,u[k].map{|i|i*""+?|}

แม้ว่าสิ่งนี้จะไม่สร้างผลลัพธ์ตัวอย่างที่แน่นอนที่ให้ไว้ในคำถาม แต่ก็เป็นไปตามข้อกำหนด ความแตกต่างเพียงอย่างเดียวกับตัวอย่างคือฉันพิมพ์เว้นวรรคนำหน้า / ขึ้นบรรทัดใหม่ไม่กี่รายการ

ตัวอย่าง:

$ ruby nonogram.rb 15 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 1 0 1 1 1 1 0 0 0 1 1 1 1 1 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1
# empty lines removed for brevity
                                  1                       1  
                                1 1 3       3 5   5 3   3 1  
                                7 2 3 2 4 2 3 210 2 3 0 4 215
                              +------------------------------
                             2|
                             1|
                             1|
                             1|
                             1|
                           1 1|
                       3 3 1 1|
                       1 5 1 1|
                         3 5 3|
                         1 5 1|
                         1 3 1|
                     1 1 1 1 1|
               1 1 1 1 1 1 1 1|
                          11 3|
                          11 3|

การเปลี่ยนแปลง:

  • 240 -> 231: เปลี่ยนรูปแบบอินพุตเพื่อใช้อาร์กิวเมนต์บรรทัดคำสั่งแทน stdin
  • 231 -> 230: กำจัดพื้นที่โดยการย้ายการตรวจสอบค่าจากไปchunkmap
  • 230 -> 226: ลบแทนการโทร[nil]Array#compact
  • 226 -> 216: ทำให้การสร้างคำใบ้ง่ายขึ้น

คุณพิมพ์บรรทัดใหม่และช่องว่างเพิ่มเติมบางส่วน แต่จนถึงตอนนี้ในการทดสอบทั้งหมดของฉันพวกเขามีคุณสมบัติตรงตามข้อกำหนด "0 หรือมากกว่า" ดังนั้นคุณก็ทำได้ดี ผมเตือนคุณ แต่ในเวลานี้ถ้าผมเริ่มเห็นตัวเลขที่ลอยอยู่ในอากาศไปทางซ้ายของจอภาพของฉันฉันจะมีการตัดสิทธิ์คำตอบนี้ :)
undergroundmonorail

1
@undergroundmonorail: เอาต์พุตถูกพิมพ์ในลักษณะที่length(leading spaces + numbers to the left) == 2*nและheight(leading newlines + numbers at the top) == n... ตราบใดที่จอภาพของคุณมีขนาดใหญ่พอสำหรับ3*n+1 × 2*n+2ตัวละครคุณไม่ควรตัดสิทธิ์ฉัน :)
Ventero

4

ทับทิม, 434

n=$*[i=0].to_i
a,b=[],[]
a.push $*.slice!(1..n)*""while $*.size>1
(b.push a.map{|c|c[i]}*"";i+=1)while i<n
a,b=*[a,b].map{|c|c.map{|d|e=d.split(/[0]+/).map(&:size).select{|f|f>i=0}.map &:to_s;(e.size>0)?e:[?0]}}
m,k=*[a,b].map{|c|c.map(&:size).max}
s="  "*m
k.times{$><<s+"  "+b.map{|c|(" "+((c.size==k-i)?(c.shift):(" ")))[-2..-1]}*"";i+=1;puts}
puts s+" "+?++?-*n*2
a.each{|c|puts"  "*(m-c.size)+" "+c.map{|d|(" "+d)[-2..-1]}*""+?|}

คุณทำงานนี้ได้อย่างไร ฉันพยายามแต่ได้ruby $yourprogram <<< $input ruby_nanograms:7:in '<main>': undefined method '+' for nil:NilClass (NoMethodError)
undergroundmonorail

@undergroundmonorail ruby nonogram.rb 2 1 0 0 1สำหรับ 2x2
ชัดเจน

นี่เป็นคำตอบที่ดี แต่คุณไม่ได้พิมพ์0คอลัมน์ที่สี่สุดท้ายในตัวอย่างที่สอง
undergroundmonorail

ฉันเพิ่งสังเกตเห็นว่า+------... บรรทัดถูกเยื้องโดยหนึ่งช่องว่างมากเกินไป
undergroundmonorail

1
@undergroundmonorail แก้ไขทั้งสองอย่าง
ชัดเจน

4

GolfScript 149 147

รหัส

~](:s/.zip{{[0]%{,`}%['0']or}%.{,}%$)\;:¶;{.,¶\-[' ']*\+}%}:f~¶:v;\[f~]\zip{{{.,2\-' '*\+}%''*}:d2*)' '*:z\+{puts}:o~}%z(;'+'s'-'2**++o~{d'|'+o}/

การแก้ไข:

  • ลบพื้นที่ที่ไร้ประโยชน์
  • กำหนดฟังก์ชั่น one-char ที่ใช้ซ้ำได้putsเพื่อบันทึก char อีกหนึ่งตัว

การสาธิตออนไลน์

รหัสรุ่นที่มีคำอธิบายประกอบค่อนข้าง

# split lines
~](:s/

# make transposed copy
.zip

#prepare numbers to show in the header
{{[0]%{,`}%['0']or}%.{,}%$)\;:¶;{.,¶\-[' ']*\+}%}:f~¶:v;

# prepare numbers to show in the left column
\[f~]\zip

#print header (vertical hints)
{  {{.,2\-' '*\+}%''*}:d~  2*)' '*:z\+puts}%

#print first line
z(;'+'s'-'2**++puts

#print horizontal hints
~{d'|'+ puts}/

4

Javascript (E6) 314 334 357 410

N=(d,l)=>{J=a=>a.join(''),M=s=>(s.match(/1+/g)||['']).map(x=>x.length),f=' '.repeat(d+1),c=[n='\n'],o=n+f+'+'+'--'.repeat(d);for(i=-1;++i<d;)c[i]=M(J(l.map((e,p)=>p%d-i?'':e))),o+=n+(f+J(M(J(l).substr(i*d,d)).map(P=n=>n>9?n:n<10?' '+n:'  '))+'|').slice(-d-2);for(;--i;)o=n+f+' '+J(c.map(e=>P(e.pop())))+o;return o}

Ungolfed

N=(d,l)=> {
  J = a => a.join(''),
  M = s => (s.match(/1+/g)||['']).map(x=>x.length),
  f=' '.repeat(d+1), c=[n='\n'], o=n+f+'+'+'--'.repeat(d);
  for(i = -1; ++i < d;)
    c[i] = M(J(l.map((e,p)=>p%d-i?'':e))),
    o += n+(f+J(M(J(l).substr(i*d,d)).map(P=n=>n>9?n:n<10?' '+n:'  '))+'|').slice(-d-2);
  for(;--i;)
    o=n+f+' '+J(c.map(e=>P(e.pop())))+o;
  return o
}

การใช้

N(5,[0,1,1,1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1,1,1,0])

N(15,[0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,0,0,1,0,1,0,0,1,0,0,0,1,1,1,1,1,0,1,0,1,1,1,1,0,0,0,1,1,1,1,1,0,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,0,0,0,1,0,0,0,1,0,0,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1])

แก้ไขประวัติ

1 ลบ regexp ที่ใช้ในการค้นหาคอลัมน์ Overkill
2 ง่ายกว่าดีกว่า ส่งออกไปยังสตริงไม่ใช่อาร์เรย์ ฟังก์ชั่นผู้ช่วยที่ถูกลบออก FILL (F)
3 ง่ายกว่ามาก ฉันไม่สามารถทำได้ดีกว่านี้ ยังไม่สามารถเปรียบเทียบกับ Golfscript :(


ดี ฉันลองรุ่น Javascript ด้วย แต่จบลงด้วยบางสิ่งบางอย่างประมาณ 500 ไบต์และตัดสินใจว่าฉันใหญ่เกินไปที่จะใส่ที่นี่ จะเป็นการดีถ้าคุณโพสต์เวอร์ชั่นที่ไม่มีชื่อด้วยชื่อตัวแปรดั้งเดิม (ถ้าคุณยังมีอยู่) นอกจากนี้คุณจะเรียกใช้สิ่งนี้ได้อย่างไร หากฉันคัดลอกวางลงในหน้าต่างคอนโซลของ Chrome ฉันจะได้รับ "ReferenceError: ด้านซ้ายไม่ถูกต้องในการกำหนด" มีบางอย่างที่จะเปลี่ยนแปลงหรือเพิ่มก่อนที่จะทำงาน?
tigrou

@tigrou ขออภัย "=>" sintax ใช้งานได้ใน firefox เท่านั้น ตัวแปร: คำแนะนำ c colunns, มิติข้อมูล, l รายการอินพุต, เอาต์พุต o, ตัวแปรลูป i, q และ z temp
edc65


@nderscore เล่นซอรอบกับรหัสผมได้ 326 ในรหัสของคุณ R ไม่ได้เริ่มต้น (ความผิดพลาดง่ายเมื่อคุณลองอีกครั้งและอีกครั้ง ... )
edc65

1

R, 384 ตัวอักษร

a=scan();p=function(x)paste(x,collapse="");P=paste0;s=sapply;l=length;f=function(i)lapply(apply(matrix(a[-1],nr=a,b=T),i,rle),function(x)if(any(x$v)){x$l[!!x$v]}else{0});g=function(j,i)apply(s(j,function(x)sprintf("%2s",c(rep("",max(s(j,l))-l(x)),x))),i,p);c=P(g(f(1),2),"|");d=g(f(2),1);h=p(rep(" ",nchar(c[1])-1));e=P(h,"+",p(rep("-",nchar(d[1]))));d=P(h," ",d);cat(d,e,c,sep="\n")

ด้วยการเยื้องและคำอธิบายบางอย่าง:

a=scan() #Takes input

p=function(x)paste(x,collapse="") #Creates shortcuts
P=paste0
s=sapply
l=length

#This function finds the number of subsequent ones in a line (using rle = run length encoding).
#It takes 1 or 2 as argument (1 being row-wise, 2 column-wise
f=function(i)lapply(apply(matrix(a[-1],nr=a,b=T),i,rle),function(x)if(any(x$v)){x$l[!!x$v]}else{0})

#This function takes the result of the previous and format the strings correctly (depending if they are rows or columns)
g=function(j,i)apply(s(j,function(x)sprintf("%2s",c(rep("",max(s(j,l))-l(x)),x))),i,p)

c=paste0(g(f(1),2),"|") #Computes the rows
d=g(f(2),1) #Computes the columns
h=p(rep(" ",nchar(c[1])-1)) 
e=paste0(h,"+",p(rep("-",nchar(d[1])))) #Prepare vertical border
d=paste0(h," ",d) #Pad column indices with spaces
cat(d,e,c,sep="\n") #Prints

การใช้งาน:

> a=scan();p=function(x)paste(x,collapse="");P=paste0;s=sapply;l=length;f=function(i)lapply(apply(matrix(a[-1],nr=a,b=T),i,rle),function(x)if(any(x$v)){x$l[!!x$v]}else{0});g=function(j,i)apply(s(j,function(x)sprintf("%2s",c(rep("",max(s(j,l))-l(x)),x))),i,p);c=P(g(f(1),2),"|");d=g(f(2),1);h=p(rep(" ",nchar(c[1])-1));e=P(h,"+",p(rep("-",nchar(d[1]))));d=P(h," ",d);cat(d,e,c,sep="\n")
1: 15 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 1 0 1 1 1 1 0 0 0 1 1 1 1 1 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1
227: 
Read 226 items
                    1                       1  
                  1 1 3       3 5   5 3   3 1  
                  7 2 3 2 4 2 3 210 2 3 0 4 215
                +------------------------------
               2|
               1|
               1|
               1|
               1|
             1 1|
         3 3 1 1|
         1 5 1 1|
           3 5 3|
           1 5 1|
           1 3 1|
       1 1 1 1 1|
 1 1 1 1 1 1 1 1|
            11 3|
            11 3|

1

C - 511

C ไม่ได้ถูกสร้างขึ้นมาเพื่อการจัดรูปแบบผลลัพธ์อย่างแน่นอน จำนวนตัวอักษรมีเพียงช่องว่างที่จำเป็น / บรรทัดใหม่

อินพุตมาจาก STDIN ตัวเลขคั่นด้วยช่องว่าง

#define P printf
#define L(x) for(x=0;x<s/2+1;x++)
#define l(x) for(x=0;x<s;x++)
#define B(x,y) x[i][j]||y==s/2?P("%2d",x[i][j]):P("  ");
a[50][50],i,j,s,h[25][50],v[50][25],H[50],V[50],x[25],y[25];
main(){
    scanf("%d",&s);
    L(j)x[j]=y[j]=s/2+1;
    l(i)l(j)scanf("%d",&a[i][j]);
    for(i=s-1;i>=0;i--)
        for(j=s-1;j>=0;j--)
            a[i][j]?
                !H[j]&&(x[j]--,H[j]=1),
                h[x[j]][j]++,
                !V[i]&&(y[i]--,V[i]=1),
                v[i][y[i]]++:
            (H[j]=V[i]=0);
    L(i){
        L(j)P("  ");
        P(" ");
        l(j)B(h,i);
        P("\n");
    }
    L(i)P("  ");
    P("+");
    l(i)P("--");
    P("\n");
    l(i){
        L(j)B(v,j);
        P("|\n");
    }
}

1

มันไม่กี่วันและไม่มีใครตอบในงูหลามดังนั้นนี่เป็นความพยายามของฉัน (อาจจะค่อนข้างแย่):

Python 2.7 - 404 397 380 ไบต์

def p(n,m):
 k=str.join;l=[];g=lambda y:[['  ']*(max(map(len,y))-len(t))+t for t in[[' '*(a<10)+`a`for a in map(len,k("",c).split('0'))if a]or[' 0']for c in y]]
 while m:l+=[map(str,m[:n])];m=m[n:]
 x=g(l);j=k('\n',['  '*max(map(len,x))+'+'+k("",a)for a in zip(*[list(a)+['--']for a in g(zip(*l))])]);return j.replace('+',' ',j.count('+')-1)+'\n'+k('\n',[k("",a+['|'])for a in x])

ฉันจะโพสต์เวอร์ชันที่ไม่อัปโหลดในไม่ช้า แต่ในขณะนี้ฉันคิดว่ามันอ่านง่าย :)

แก้ไข:ในขณะที่เขียนรุ่นที่ไม่ได้แต่งขึ้นฉันสังเกตเห็นการปรับปรุงบางอย่างที่ฉันสามารถทำได้ซึ่งเพิ่มเข้ามาให้มีความสำคัญพอสมควร! ด้วยเหตุผลบางอย่างที่ฉันไม่สามารถอธิบายได้ตอนนี้มีการขึ้นบรรทัดใหม่ที่ด้านบนและช่องว่างทางด้านซ้าย (แม้ว่าฉันไม่คิดว่าฉันจะเปลี่ยนแปลงการทำงานใด ๆ ) แต่ก็ยังมีคุณสมบัติตรงตามเวอร์ชั่น Ungolfed กำลังจะมา!

Ungolfed:

def nonogram(board_size, pixels):
    def hints(board):
        output = []
        for row in board:
            # Convert the row to a string of 1s and 0s, then get a list of strings
            # that came between two 0s.
            s = "".join(row).split('0')

            # A list of the length of each string in that list.
            l = map(len, s)

            # We now have our horizontal hints for the board, except that anywhere
            # there were two 0s consecutively we have a useless 0.
            # We can get rid of the 0s easily, but if there were no 1s in the row at
            # all we want exactly one 0.
            # Solution:
            output.append([h for h in l if h != 0] or [0])
            # In this context, `foo or bar` means `foo if foo is a truthy value, bar
            # otherwise`.
            # An empty list is falsey, so if we strip out all the strings we hardcode
            # the 0.
        return output

    def num_format(hints):
        # For both horizontal and vertical hints, we want a space before single-
        # digit numbers and no space otherwise. Convert hints to strings and add
        # spaces as necessary.
        output = []

        for row in hints:
            output.append([' '*(a < 10) + str(a) for a in row])
            # Multiplying a string by n repeats it n times, e.g. 'abc'*3=='abcabcabc'
            # The only numbers that need a space are the ones less than 10.
            # ' '*(a < 10) first evaluates a < 10 to get a True or False value.
            # Python automatically converts True to 1 and False to 0.
            # So, if a is a one digit number, we do `' '*(1) + str(a)`.
            # If it's a two digit number, we do `' '*(0) + str(a)`.
        return output

    def padding(hints):
        output = []
        longest = max(map(len, hints)) # how long is the longest row?
        for row in hints:
            output.append(['  ']*(longest - len(row)) + row)
            # Add '  ' to the beginning of every row until it's the same length
            # as the longest one. Because all hints are two characters wide, this
            # ensures all rows of hints are the same length.
        return output

    board = []

    while pixels: # non-empty list == True
        # Make a list of the first (board_size) pixels converted to strings, then
        # add that list to board. Remove those pixels from the list of pixels.
        # When pixels is empty, board has a seperate list for each row.
        board.append([str(n) for n in pixels[:board_size]])
        pixels = pixels[board_size:]

    horizontal_hints = padding(num_format(hints(board)))

    vertical_hints = padding(num_format(hints(zip(*board))))
    # zip(*l) is a common way to transpose l.
    # zip([1,2,3], [4,5,6], [7,8,9]) == [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
    # the star operator unpacks an iterable so the contents can be used as
    # multiple arguments, so
    # zip(*[[1,2,3],[4,5,6],[7,8,9]]) is the same as what we just did.
    # Transposing the board and getting the horizontal hints gives us the
    # vertical hints of the original, but transposed. We'll transpose it back,
    # but we'll also add '--' to the end of all of them to make up the line
    vertical_hints = zip(*[a + ['--'] for a in vertical_hints])

    # add n spaces, where n is the length of the longest horizontal hint, plus
    # one space to the beginning of each line in the vertical hints, then join
    # with newlines to make it all one string.
    vertical_hints = '\n'.join(['  '*max(map(len, horizontal_hints)) + '+' +
                               ''.join(a) for a in vertical_hints])

    # find the number of plus signs in the string
    # replace the first (that many - 1) plus signs with spaces
    vertical_hints = vertical_hints.replace('+', ' ', vertical_hints.count('+')-1)

    # add a pipe to each row of horizontal hints, then join it with newlines
    horizontal_hints = '\n'.join([''.join(a + ['|']) for a in horizontal_hints])

    # add and return
    return vertical_hints + '\n' + horizontal_hints

มีการเปลี่ยนแปลงเล็กน้อยเพื่อประโยชน์ในการอ่าน ( gถูกแบ่งออกเป็นสามฟังก์ชั่นที่มีชื่อความเข้าใจในรายการที่ซับซ้อนทำให้เป็นforลูป) แต่มีเหตุผลว่ามันทำงานในลักษณะเดียวกัน

นี่คือสาเหตุที่ทำให้เกิดความสับสนว่าอันนี้ไม่ได้พิมพ์ช่องว่างและบรรทัดใหม่ในขณะที่คนตีกอล์ฟทำ ¯ \ _ (ツ) _ / ¯


1
อืมฉันหาทางออกของคุณไม่เจอ (ขออภัยเป็นเพียงเรื่องตลกที่น่ากลัวเกี่ยวกับการนับตัวละครไม่ต้องสนใจฉัน :))
Doorknob

@dor Aha! ลองสร้างรหัสข้อผิดพลาด HTTP ของคุณตอนนี้! : P
undergroundmonorail
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.