펜토미노 검사기


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. 펜토미노 반사가 원본과 같은 모양입니까? 2. 입력이 인쇄 가능한 ASCII 문자와 줄 바꿈으로 구성되어 있다고 가정 할 수 있습니까?
Dennis

@Dennis 예 및 예
Blue

@DigitalTrauma 그것은 원격으로 복제되지 않습니다. BTW는 굉장한 질문이었습니다. 새로 요청했을 때 대답 할 시간이 없었습니다.
Level River St

@steveverill 당신 말이 맞아요-이 질문을 제대로 읽지 못했습니다
Digital Trauma

답변:


3

자바 스크립트 (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)

@DankMemes 덕분에 2 바이트가 절약 되었습니다 !

용법

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

설명

이 솔루션에 대한 몇 가지 참고 사항 :

  • 이 답변이 유효하지 않을 수 있습니다. 회전 된 pentominoes가 같은 모양인지 실제로 확인하지는 않지만 시도했지만 규칙의 요구 사항을 충족하고 동일한 모양이 회전 된 둘 이상의 포함 된 유효한 pentomino 사각형을 찾을 수 없습니다. 그러나 나는 pentomino 전문가가 아니므로 이것이 실패한 유효한 조합을 찾으면 알려주십시오.
  • 규칙은 또한 사용 STDINSTDOUT입력 및 출력에 대한 답변이 필요 하지만 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.