เครื่องมือตรวจสอบ Pentomino


9

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

งานของคุณ

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

ทางออกที่ถูกต้องจะต้อง

  • มี 5 ตัวอักขระ (ยกเว้นบรรทัดใหม่)
  • อักขระแต่ละชุดต้องเชื่อมต่ออย่างสมบูรณ์
  • อักขระแต่ละชุดต้องมีรูปร่างที่ไม่ซ้ำกัน
  • อยู่ในรูปทรงสี่เหลี่ยมปกติ

ถ้ามันเป็นทางออกที่ถูกต้องให้ส่งออกค่าความจริงมิฉะนั้นส่งออกค่าเท็จ

โปรแกรมของคุณอาจเป็นฟังก์ชั่นหรือโปรแกรมเต็มรูปแบบ แต่ต้องนำอินพุตจาก stdin และเอาต์พุตไปที่ stdout

กรณีทดสอบ

โซลูชั่นที่ถูกต้อง

000111
203331
203431
22 444
2   46
57 666
57769!
58779!
58899!
5889!!

00.@@@ccccF111//=---
0...@@c))FFF1//8===-
00.ttttt)))F1/8888=-

การกำหนดค่าไม่ถูกต้อง

invalid (doesn't contain 12 unique characters)

111112222233333444445555566666
77777888889999900000qqqqqwwwww (Each set has the same shape)

1234567890qw
w1234567890q
qw1234567890
0qw123456789
90qw12345678 (None of the characters are connected)

1234567890qw (Not 5 characters in every set)

1111122222333334444455555666666
77777888889999900000qqqqqwwwwww (More than 5 characters in some sets)

00
0                   
00.@@@ccccF111//=---
 ...@@c))FFF1//8===-
  .ttttt)))F1/8888=- (Doesn't form a rectangular shape)

1. การสะท้อนของ pentomino มีรูปร่างเหมือนต้นฉบับหรือไม่? 2. เราสามารถสมมติว่าการป้อนข้อมูลประกอบด้วยอักขระ ASCII ที่พิมพ์ได้และการขึ้นบรรทัดใหม่ได้หรือไม่
Dennis

@Dennis ใช่และใช่
บลู

@DigitalTrauma ไม่ซ้ำกันจากระยะไกล BTW นั่นเป็นคำถามที่ยอดเยี่ยมมันเป็นความอัปยศที่ฉันไม่มีเวลาตอบเมื่อได้รับคำถามใหม่
เลเวลริเวอร์

@ สตีฟคุณไม่ถูกต้อง - ฉันไม่ได้อ่านคำถามนี้อย่างถูกต้อง
Digital Trauma

คำตอบ:


3

JavaScript (ES6), 237 235 222 ไบต์

f=p=>(m=[],s=[],d=0,l=p.indexOf`
`+1,[...p].map((c,i)=>(i+1)%l&&!m[i]?g=d-2<s.indexOf((t=a=>m[a]|p[a]!=c?r=0:(m[a]=y.push(a),n=a<n?a:n,t(a+1)+t(a-1)+t(a+l)+t(a-l)+1))(n=i,y=[])!=5?g=0:s[d++]=y.map(a=>r+=a-n)|r):0),d==12&g)

บันทึก 2 ไบต์ด้วย@DankMemes !

การใช้

f(`000111
203331
203431
22 444
2   46
57 666
57769!
58779!
58899!
5889!!`);
=> true

คำอธิบาย

หมายเหตุสองสามข้อเกี่ยวกับโซลูชันนี้:

  • เป็นไปได้ว่าคำตอบนี้ไม่ถูกต้อง มันไม่ได้ตรวจสอบว่าเพนโตโนที่ถูกหมุนนั้นมีรูปร่างเหมือนกันหรือไม่ แต่ฉันพยายามแล้ว แต่ไม่สามารถหาเพนโตมิโนสี่เหลี่ยมผืนผ้าที่ถูกต้องที่ตรงกับข้อกำหนดในกฎและรวมรูปร่างที่หมุนสองหรือมากกว่านั้น แต่ฉันไม่ใช่ผู้เชี่ยวชาญ Pentomino ดังนั้นหากคุณพบชุดค่าผสมที่ถูกต้องซึ่งสิ่งนี้ล้มเหลวโปรดแจ้งให้เราทราบ
  • กฎยังต้องการคำตอบในการใช้STDINและSTDOUTสำหรับอินพุตและเอาต์พุต แต่prompt()ถูกออกแบบมาสำหรับอินพุตบรรทัดเดียวเท่านั้นและคอมพิวเตอร์ (Windows) ของฉันจะใส่\r\nอักขระที่บรรทัดใหม่ทุกบรรทัดโดยอัตโนมัติเมื่อวางดังนั้นฉันจึงทำให้มันเป็นฟังก์ชั่นที่รับสตริง
f=p=>(
  m=[],                      // m = map of checked characters
  s=[],                      // s = list of shapes found (stored as integer)
  d=0,                       // d = number shapes found
  l=p.indexOf`
`+1,                         // l = length of each line (including newline character)
  [...p].map((c,i)=>         // iterate through each character of the input
    (i+1)%l&&                // skip newline characters
      !m[i]?                 // skip the character if it has already been mapped
        g=                   // g = pentomino is valid
          d-2<s.indexOf(     // check if shape already existed before just now
            (t=a=>           // t() checks if character is part of the shape then maps it
              m[a]|          // skip if character is already mapped
                p[a]!=c      //    or if the current character is part of the shape
              ?r=0:(
                m[a]=        // mark the character as mapped
                  y.push(a), // y = list of shape character indices
                n=a<n?a:n,   // n = minimum index of all characters in the shape
                t(a+1)+      // check and map adjacent characters
                t(a-1)+
                t(a+l)+
                t(a-l)+
                1
              )
          )(n=i,y=[])
            !=5?g=0:         // make sure there are only 5 characters in the shape
            s[d++]=          // add the shape to the list
              y.map(a=>      // sum of (index of each character in the shape - minimum
                r+=a-n)|r    //     index) = unique integer representing the shape
        ):0
  ),
  d==12&g                    // ensure there is 12 shapes and return the 'is valid' result
)

1
คุณสามารถละเมิดแม่แบบที่ติดแท็กด้วยl=p.indexOf`<newline here>`เพื่อบันทึก 2 ไบต์
DankMemes

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