マトリックスジグソーパズル


24

入力:

  • 整数 n
  • 2つの等しいサイズの正方行列(幅/高さがの倍数であるn

出力:

二つの異なるご自身の選択の値、1はfalsey結果のtruthy結果と1のためのもの(そうはい、のいずれか1/0の代わりtrue/falseにもかかわらず、Javaのような言語のための有効な出力である。それらは、公式truthy / falsey値と考えられていません)。

真実/偽の出力はn by n、ある行列のサイズのブロックを他の行列と等しくなるように再配置できるかどうかを示します。

例:

入力:

Matrix 1:
1 2 3 4 5 6
7 8 9 0 1 2
3 4 5 6 7 8
9 8 7 6 5 4
3 2 1 0 9 8
1 1 1 1 1 1

Matrix 2:
3 2 9 8 7 8
1 1 1 1 5 4
3 4 5 6 1 0
9 0 7 6 1 1
5 6 1 2 3 4
1 2 7 8 9 8

Integer n:
2

出力: truthy

どうして?

行列をのブロックに分割すると2 by 2、1つの行列のすべてのブロックが他の行列でも見つかることがわかります。

Matrix 1:
1 2 | 3 4 | 5 6
7 8 | 9 0 | 1 2
---------------
3 4 | 5 6 | 7 8
9 8 | 7 6 | 5 4
---------------
3 2 | 1 0 | 9 8
1 1 | 1 1 | 1 1

Matrix 2:
3 2 | 9 8 | 7 8
1 1 | 1 1 | 5 4
---------------
3 4 | 5 6 | 1 0
9 0 | 7 6 | 1 1
---------------
5 6 | 1 2 | 3 4
1 2 | 7 8 | 9 8

チャレンジルール:

  • マトリックスには負でない数字(範囲[0,9])のみが含まれると想定できます。
  • 行列の幅/高さが等しく、次の倍数であると仮定できます。 n
  • n範囲内[1, 50]にあり、行列の幅/高さが範囲内にあると仮定できます[1,100]
  • の個々のブロックは、n by nがのブロックに分割されたときに行列が互いの順列であるかどうかを判断するために一度だけ使用できn by nます。
  • n by n同じ複数のブロックが存在する場合があります。
  • n by nチェックするときに二つの行列がのブロックに分割したときに、互いの順列である場合にブロックが同じ向きのままになりますn by n

一般的なルール:

  • これはであるため、バイト単位の最短回答が優先されます。
    コードゴルフ言語では、非コードゴルフ言語で回答を投稿することを妨げないでください。「任意の」プログラミング言語の可能な限り短い答えを考えてみてください。
  • デフォルトのI / Oルールを使用した回答には標準ルールが適用されるため、STDIN / STDOUT、関数/メソッド、適切なパラメーターおよび戻り値型、完全なプログラムを使用できます。あなたの電話。
  • デフォルトの抜け穴は禁止されています。
  • 可能であれば、コードのテストへのリンク(TIOなど)を追加してください。
  • また、回答の説明を追加することを強くお勧めします。

テストケース:

Input:
Matrix 1:       Matrix 2:       Integer:
1 2 3 4 5 6     3 2 9 8 7 8     2
7 8 9 0 1 2     1 1 1 1 5 4
3 4 5 6 7 8     3 4 5 6 1 0
9 8 7 6 5 4     9 0 7 6 1 1
3 2 1 0 9 8     5 6 1 2 3 4
1 1 1 1 1 1     1 2 7 8 9 8
Output:
truthy

Input:
Matrix 1:       Matrix 2:       Integer:
1 2 3 4 5 6     3 2 9 8 7 8     1
7 8 9 0 1 2     1 1 1 1 5 4
3 4 5 6 7 8     3 4 5 6 1 0
9 8 7 6 5 4     9 0 7 6 1 1
3 2 1 0 9 8     5 6 1 2 3 4
1 1 1 1 1 1     1 2 7 8 9 8
Output:
truthy

Input:
Matrix 1:       Matrix 2:       Integer:
1 2 3 4 5 6     3 2 9 8 7 8     3
7 8 9 0 1 2     1 1 1 1 5 4
3 4 5 6 7 8     3 4 5 6 1 0
9 8 7 6 5 4     9 0 7 6 1 1
3 2 1 0 9 8     5 6 1 2 3 4
1 1 1 1 1 1     1 2 7 8 9 8
Output:
falsey

Input:
Matrix 1:    Matrix 2:    Integer:
1 2 3 4      1 2 3 4      4
2 3 4 5      2 3 4 5
3 4 5 6      3 4 5 6
4 5 6 7      4 5 6 7
Output:
truthy

Input:
Matrix 1:    Matrix 2:    Integer:
1 2 3 4      3 4 3 4      2
2 3 4 5      4 5 4 5
3 4 5 6      1 2 5 6
4 5 6 7      2 3 6 6
Output:
falsey

Input:
Matrix 1:    Matrix 2:    Integer:
1 2          2 3          1
3 4          1 1
Output:
falsey

Input:
Matrix 1:    Matrix 2:    Integer:
0            8            1
Output:
falsey

Input:
Matrix 1:    Matrix 2:    Integer:
1 2 3 4      1 2 1 2      2
5 6 7 8      5 6 5 6
9 0 0 9      0 9 9 0
4 3 2 1      2 1 4 3
Output:
falsey

Input:
Matrix 1:    Matrix 2:    Integer:
1 2 1 2      9 5 1 2      2
3 4 3 4      7 7 3 4
8 3 9 5      1 2 8 3
6 1 7 7      3 4 6 1
Output:
truthy

Input:
Matrix 1:      Matrix 2:      Integer:
1 0 2 0 0 3    1 1 1 0 0 3    2
1 1 1 1 1 1    2 0 1 1 1 1
2 2 2 2 2 2    2 2 2 2 2 2
3 3 3 3 3 3    3 3 3 3 3 3
4 4 4 4 4 4    4 4 4 4 4 4
5 5 5 5 5 5    5 5 5 5 5 5
Output:
falsey

[[,]]形式の行列を含むペーストビン。


マトリックスをマトリックスのリストとして使用できますか?
ジョーキング

@JoKing 2つの分離されたマトリックス入力ではなく、両方のマトリックスのリストを意味しますか?それがあなたが求めているものであるなら、確かに、なぜそうではありません。
ケビンクルーイッセン


例が[ [ 0 ] ], [ [ 25 ] ], 1存在するのはなぜですか?You can assume the matrices will only contain non-negative digits (range [0,9])マトリックスの値は0から9の間だけであることを理解しましたか?
オリビエグレゴワール

2
@OlivierGrégoire申し訳ありませんが、[0,9]後でサンドボックスに範囲に関するルールを追加しました。テストケースをに変更しました[[0]],[[8]]
ケビンクルーイッセン

回答:


4

ゼリー 10  9 バイト

ż⁹/ẎsṢʋ€E

オンラインでお試しください!(または、テストケースから簡単にコピーアンドペーストするための前処理を使用)

その収率右に左に(リストのリストなど)は、2つの行列のリストを受け入れるダイアディックリンクおよび整数1または0truthyまたはfalseyそれぞれ。

どうやって?

ż⁹/ẎsṢʋ€E - Link: [M1, M2]; N
       €  - for each of [M1, M2]:
      ʋ   -   last four links as a dyad (i.e. f(M, N)):
 ⁹        -     (chain's right argument, N)
 ⁹/       -     N-wise-reduce with:
ż         -       zip together
   Ẏ      -     tighten
    s     -     split into chunks of length N
     Ṣ    -     sort
        E - equal?

8

APL(Dyalog Extended)19 18 17バイト

-2 ngnに感謝します。

匿名の暗黙の挿入機能。n左引数として取り、2つの行列のリストを右引数として取ります。ゼロインデックスが必要です(⎕IO←0)。ちなみに、この関数は任意の数の次元の配列で機能します。

≡.{∧,⍵⊂⍨⊂0=⍺|⍳≢⍵}

オンラインでお試しください!

≡.{... } 次の関数と同一の結果は、各マトリックスに適用されるnのように

≢⍵ 行列のサイズ

 インデックス0…サイズ–1

⍺| 除算時の除算余り n

 すべての次元に沿って使用するために囲む

⍵⊂⍨ これを使用して、マトリックスをサブマトリックスのマトリックスにパーティション化*し、
  対応する要素が前の要素よりも小さい場合に新しいパーティションを開始します。ゼロでマークされた要素を削除します

, 行列を解いて部分行列のリストにする

 ソート昇順


(≢⍵)⍴⍺↑1-> 0=⍺|⍳≢⍵(with ⎕io←0
ngn

@ngnありがとう。できた
アダム

≡/{}¨->≡.{}
ngn

@ngn完了。ありがとう。
アダム


6

Perl 6の94の68 63バイト

{[eqv] map *.rotor($^a).map({[Z] $_}).flat.rotor($a²).sort,@_}

オンラインでお試しください!

入力をsize, [matrix1, matrix2]boolean として返す匿名コードブロックTrue/False。行列をチャンクに分割するより効率的な方法があるかもしれませんrotor

説明:

{                                                            }  # Anonymous code block
       map                                                ,@_   # For both matrices 
           *.rotor($^a)   # Split the matrix into N sized chunks
                       .map({[Z] $_})  # Then zip each of those chunks together
                                     .flat  # Flatten the resulting list
                                          .rotor($a²)  # Then split into the NxN lists
                                                     .sort   # And sort them
 [eqv]    # And then check if the lists are equivalent 


4

Java(JDK)、221バイト

(n,a,b)->{java.util.Arrays A=null;int l=a.length,x=l/n,i=0,j,z;var c=new String[x*x];A.fill(c,"");var d=c.clone();for(;i<l;i++)for(j=0;j<l;d[z]+=b[i][j++])c[z=i/n+j/n*x]+=a[i][j];A.sort(c);A.sort(d);return A.equals(c,d);}

オンラインでお試しください!

説明

考えは、各小さなセルを比較可能な文字列として選択し、それらの文字列をソートして順番に比較することです。

(n,a,b)->{
 java.util.Arrays A=null;             // Shortcut A for the several java.util.Arrays that'll come
 int l=a.length,x=l/n,i=0,j,z;        // Variable declarations
 var c=new String[x*x];               // Declare the small squares list
 A.fill(c,"");                        // Fill the lists of small squares with the empty string.
 var d=c.clone();                     // Make a copy of the list, for the second matrix
 for(;i<l;i++)
  for(j=0;j<l;d[z]+=b[i][j++])        // For each matrix cell
   c[z=i/n+j/n*x]+=a[i][j];           // Fill the small square with the value, string-wise
 A.sort(c);A.sort(d);                 // Sort both small squares list
 return A.equals(c,d);                // Return true if they're equal, false otherwise.
}

クレジット

  • Kevin Cruijssenのおかげで-12バイト!

ゴルフを忘れましたfor(j=0;j<l;){c[z=i/n+j/n*x]+=a[i][j];d[z]+=b[i][j++];}か?..ループ内にすべてを入れることでブラケットを取り外すことができます。また、i=0ループ内のi宣言はすでに0であるため、削除できます。
ケビンクルーイッセン

そして、実際にゴルフをするための1つのこと:代わりにvar d=new String[x*x];できますvar d=c.clone();234バイト
ケビンクルーッセン

PS:TIOに2D整数配列に変換する文字列が含まれているのはなぜですか?テストケースの下部にペーストビンを追加しました。このため、[and ]{andに置き換えて}、先頭new int[][]にを追加すれば十分です。;)
ケビン・クルーッセン

ダンミット、私はペーストビンを見ていなかった:(そして、残りは、私はまだゴルフをしている。ラフなパスをしたが、ありがとう:-)
オリビエ・グレゴワール

i=0私は自分自身ではなく、使用して配列を埋め残骸でしたArrays.fill。ありがとう:-)そして、clone私はそれを使うことを考えましたが、それでもObject実際の型ではなくを返すだろうと思っていました。私はその時点でいくつかのバージョンに遅れている必要があります;)
オリビエグレゴワール

4

Japt、18バイト

®mòV yòV rc n qÃr¥

オンラインでお試しください!

説明:

®              Ã      #Apply this to each of the input matrices:
 mòV                  # Split each row into groups of n
     yòV              # Split each column into groups of n
         rc           # Flatten into a list of nxn submatrices
            n         # Sort that list
              q       # Turn it into a string
                r¥    #Return true if both matrices had identical results

Japt は配列を値で比較しないため、「文字列に変換する」ステップが必要です。これ、多次元配列は機能しない組み込みの組み込み関数です。


2
明日の会議の間に、A.e()多次元配列の作業を試みるために時間を作ることができるかどうかを確認します。常にそれに戻ることを意味しました。それまでの間ÕmòV-> yòVは1バイト節約します。
シャギー

ところで、配列の等価性を比較する際の制限は、Japtに固有のものではなくJavaScriptのものです;)
Shaggy

4

TSQL、164バイト

入力を行うためにテーブル変数を設定します。入力の作成とデータの挿入は、バイトカウントには含まれていません。データを抽出するための実際のクエリのみ。

Golfed(テストテーブルは含まれません-ungolfedバージョンにあります):

SELECT iif(exists(SELECT*FROM(SELECT string_agg(v,'')within
group(order by x,y)s,m FROM @t GROUP BY x/@,y/@,m)x
GROUP BY s HAVING max(m)=min(m)or sum(m-.5)<>0),0,1)

ゴルフをしていない:

-- test data
DECLARE @ INT = 2
-- x = x-position of the input
-- y = y-position of the input
-- v = value
-- m = matrix(0 or 1)
DECLARE @t table(x int, y int, v int, m int)
--insert first matrix values
INSERT @t values
(0,0,1,0),(0,1,2,0),(0,2,1,0),(0,3,2,0),
(1,0,3,0),(1,1,4,0),(1,2,3,0),(1,3,4,0),
(2,0,8,0),(2,1,3,0),(2,2,9,0),(2,3,5,0),
(3,0,6,0),(3,1,1,0),(3,2,7,0),(3,3,7,0)
INSERT @t values
(0,0,9,1),(0,1,5,1),(0,2,1,1),(0,3,2,1),
(1,0,7,1),(1,1,7,1),(1,2,3,1),(1,3,4,1),
(2,0,1,1),(2,1,2,1),(2,2,8,1),(2,3,3,1),
(3,0,3,1),(3,1,4,1),(3,2,6,1),(3,3,1,1)

-- query
SELECT iif(exists
  (
    SELECT *
    FROM
    (
      SELECT string_agg(v,'')within group(order by x,y)s,m
      FROM @t
      GROUP BY x/@,y/@,m
    ) x
    GROUP BY s
    HAVING max(m)=min(m)or sum(m-.5)<>0
  ),0,1)

やってみて


4

JavaScript(ES6)、88バイト

(n,a,b)=>(g=a=>a.map((r,y)=>r.map((v,x)=>o[y/n<<7|x/n]+=[v]),o=[])&&o.sort()+o)(a)==g(b)

オンラインでお試しください!

どうやって?

このコードは次のとおりです。

  • 各入力行列のすべての部分行列をセルの連結として抽出する
  • サブマトリックスを辞書式順序でソートする
  • 結果が両方の入力行列で同じかどうかをテストする

課題で説明されている制限を活用しています。

  • マトリックスは1桁の数字で構成されているため、サブマトリックスのすべてのセルをセパレータなしで連結しても、一意の表現を取得[[1,2],[3,4]]できます(たとえば、として保存できます"1234")。

  • 100(x,y)I

    I=yn×128+xn

    またはJSコードとして: y / n << 7 | x << n

コメント済み

(n, a, b) =>           // n, a, b = input variables (integer, matrix 1, matrix 2)
  (g = a =>            // g = helper function taking one of the two matrices
    a.map((r, y) =>    // for each row r[] at position y in a[]:
      r.map((v, x) =>  //   for each value v at position x in r[]:
        o[             //     update o[]:
          y / n << 7 | //       the position of the slot is computed by taking advantage
          x / n        //       of the limit on the matrix width (see above)
        ] += [v]       //     coerce v to a string and append it to o[slot]
                       //     all slots are initially undefined, so all resulting strings
                       //     are going to start with "undefined", which is harmless
      ),               //   end of inner map()
      o = []           //   start with o = empty array
    ) &&               // end of outer map()
    o.sort() + o       // sort o[] and coerce it to a string by concatenating it with itself
  )(a) == g(b)         // test whether g(a) is equal to g(b)

3

54 49バイト

1FθF⪪ιηF÷L§κ⁰η⊞υEκ§⪪μηλW∧υ⊟υ¿№✂υ⁰⊘⊕Lυ¹ι≔Φυ⁻⌕υιλυ⎚

オンラインでお試しください!リンクは、コードの詳細バージョンです。入力を同じサイズの2次元配列の配列として受け取ります。成功した場合は1、失敗した場合は何も出力しません。説明:

1

成功を想定してください。

Fθ

配列をループします。

F⪪ιη

配列をn-size行のチャンクに分割します。

F÷L§κ⁰η

各列チャンクをループします。

⊞υEκ§⪪μηλ

行チャンクの各行の列チャンクを抽出し、結果のサブマトリックスをリストに保存します。

W∧υ⊟υ

リストが空ではない間に、リストの最後のチャンクを削除します。これは、通常の状況では2番目の配列に由来します。

¿№✂υ⁰⊘⊕Lυ¹ι

リストの前半にあるそのチャンクの出現回数をカウントします。通常の状況では、このチャンクには最初の配列の残りのチャンクが含まれます。

≔Φυ⁻⌕υιλυ

ゼロ以外の場合、そのチャンクの最初の出現をリストから削除します。

ゼロの場合、出力をクリアし、偽造します。



2

Haskell、74 73バイト

import Data.Lists
i#m|c<-chunksOf i=c.transpose=<<c m
(m!n)i=i#m\\i#n==[]

注:TIOがインストールされていないData.Lists私が使用しているので、Data.List不足している機能を追加する代わりにchunksOfオンラインそれをお試しください!

i#m=           -- function '#' makes a list of all transposed jigsaw blocks of matrix 'm'
               -- of size 'i'
 c<-chunksOf i -- define helper function 'c' that splits it's argument into
               -- chunks of site 'i'
         c m   -- split the matrix into chunks of size 'i'
      =<<      -- for each chunk
   transpose   --   transpose
 c.            --   and split into chunks of size 'i', again
               -- flatten one level of nesting ('=<<' is concatMap)

(m!n)i=        -- main function
    i#m\\i#n   -- remove every element of i#n from i#m
      ==[]     -- and check if it results in an empty list  

2

C#(Visual C#Interactive Compiler)、186バイト

(a,b,n)=>{string[]s(int[][]c){int i=0,j,l=a.Length,m=l/n;var r=new string[m*m];for(;i<l;i++)for(j=0;j<l;)r[i/n*m+j/n]+=c[i][j++];Array.Sort(r);return r;}return s(a).SequenceEqual(s(b));}

オンラインでお試しください!

-1 @KevinCruijssenに感謝します!

より少ないゴルフコード:

// anonymous function
// a and b are 2d jagged arrays
// n is the size of the sub matrix
(a,b,n)=>{
  // helper function that translates
  // the sub matrices into strings
  // of digits.
  string[]s(int[][]c){
    // i and j are loop counters
    int i=0,j,
      // l is the size of a side of a matrix
      l=a.Length,
      // m is the number of sub matrices
      // per side of a matrix
      m=l/n;
    // the concatenated digits are
    // stored in a single dimension
    // array
    var r=new string[m*m];
    // nested loops build up
    // the digit strings
    for(;i<l;i++)
      for(j=0;j<l;)
        r[i/n*m+j/n]+=c[i][j++];
    // The resulting array is
    // sorted before it is returned for
    // ease of comparison.
    Array.Sort(r);
    return r;
  }
  return s(a).SequenceEqual(s(b));
}

ゴルフのちょっとしたことj++は、取り外し+=c[i][j++]+" ";てバイトを節約するために置くことができます。
ケビンクルーイッセン

ヒントをありがとう:)興味深いことに、私はJavaのソリューションとまったく同じソリューションを思いつきました。
ダナ

1

PHP186 163 162バイト

function($a,$b,$n){$f=function($j,$n){foreach($j as$x=>$r)foreach($r as$y=>$v)$o[count($j)*($x/$n|0)+$y/$n|0].=$v;sort($o);return$o;};return$f($a,$n)==$f($b,$n);}

オンラインでお試しください!

すべての良い挑戦のように、私はこれがかなり簡単だと思い始めて、それは私にいくつかのカーブを投げました。@Kevin Cruijssenちゃんとできました!

マトリックスを各ブロックの値を含む文字列にチャンクします。次に、配列がソートされ、等しいかどうか比較されます。

ゴルフをしていない:

function jigsaw_chunk( $j, $n ) {
    foreach( $j as $x => $r ) {
        foreach( $r as $y => $v ) {
            $o[ count( $j ) * floor( $x/$n ) + floor( $y/$n )] .= $v;
        }
    }
    sort( $o );
    return $o;
}

function jigsaw_test( $a, $b, $n ) {
    return jigsaw_chunk( $a, $n ) == jigsaw_chunk( $b, $n );
}

// Test 6
var_dump( jigsaw_test( [[1,2],[3,4]], [[2,3],[1,1]], 1 ) );

出力

bool(false)

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.