谁的邻居是敌对的?


10

介绍

出于挑战的目的,我们将在正方形矩阵定义元素的邻居(使得)为 与对角线,水平或垂直相邻的所有项。(即,它们“环绕”而不环绕)。Ë一个Ë=一个一世Ĵ一个EË Ë

对于悬挂物,的邻居的正式定义用于 MATIX为(0索引): 中 一个一世Ĵñ×ñ一个

ñ一世Ĵ={一个一个b一个bË一世Ĵ[0ñž2}
Ë一世Ĵ={一世-1个一世一世+1个}×{Ĵ-1个ĴĴ+1个} \ {一世Ĵ}

假设索引i,\:j的元素一世Ĵ所有邻居都互斥(即光盘一个一世Ĵñ=1个ññ一世Ĵ)。可悲的是,这个穷人无法从附近粗鲁的居民那里借来一杯糖。

任务

足够的故事:给定一个正整数的方阵中号,输出以下之一:

  • 元件的指示占用一些索引中的所有条目的平面列表(重复数据删除或不)一世Ĵ中号,使得邻居ñ一世Ĵ是敌对。
  • 一个布尔值矩阵,在邻居处于敌对的位置时为1个 s,否则为0(您可以选择任何其他一致的值来代替01个)。
  • 代表敌对邻居的索引对i,\:j的列表一世Ĵ

Physica中的参考实现 –还支持I / O的Python语法。您可以通过任何标准方法并以任何合理的格式接受输入并提供输出,同时请注意,默认情况下禁止这些漏洞。这是代码高尔夫球,因此以字节为单位(每种语言)的最短代码胜出!

此外,您也可以将矩阵大小作为输入,并且也可以将矩阵作为平面列表,因为它始终是正方形。

考虑以下矩阵:

641014272232535836

每个元素的对应邻居是:

i j – E  -> Neighbours                          | All coprime to E?
                                                |
0 0 – 64 -> {10; 27; 22}                        | False
0 1 – 10 -> {64; 14; 27; 22; 32}                | False
0 2 – 14 -> {10; 22; 32}                        | False
1 0 – 27 -> {64; 10; 22; 53; 58}                | True
1 1 – 22 -> {64; 10; 14; 27; 32; 53; 58; 36}    | False
1 2 – 32 -> {10; 14; 22; 58; 36}                | False
2 0 – 53 -> {27; 22; 58}                        | True
2 1 – 58 -> {27; 22; 32; 53; 36}                | False
2 2 – 36 -> {22; 32; 58}                        | False

因此输出必须是以下之一:

  • {27; 53}
  • {{0; 0; 0}; {1; 0; 0}; {1; 0; 0}}
  • {(1; 0); (2; 0)}

测试用例

Input –> Version 1 | Version 2 | Version 3

[[36, 94], [24, 69]] ->
    []
    [[0, 0], [0, 0]]
    []
[[38, 77, 11], [17, 51, 32], [66, 78, 19]] –>
    [38, 19]
    [[1, 0, 0], [0, 0, 0], [0, 0, 1]]
    [(0, 0), (2, 2)]
[[64, 10, 14], [27, 22, 32], [53, 58, 36]] ->
    [27, 53]
    [[0, 0, 0], [1, 0, 0], [1, 0, 0]]
    [(1, 0), (2, 0)]
[[9, 9, 9], [9, 3, 9], [9, 9, 9]] ->
    []
    [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    []
[[1, 1, 1], [1, 1, 1], [1, 1, 1]] ->
    [1, 1, 1, 1, 1, 1, 1, 1, 1] or [1]
    [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
    [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
[[35, 85, 30, 71], [10, 54, 55, 73], [80, 78, 47, 2], [33, 68, 62, 29]] ->
    [71, 73, 47, 29]
    [[0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]]
    [(0, 3), (1, 3), (2, 2), (3, 3)]

向敌对邻居借东西?出于某种原因,这让我想起了杰夫·明特(Jeff Minter)的游戏“ 悬停鲍弗(Hover Bovver) ...
Arnauld

我们可以将矩阵大小作为输入吗?
Delfad0r

@ Delfad0r我总是忘记提及这一点。是的,您可以将矩阵大小作为输入。
Xcoder先生18年

Answers:


3

APL(Dyalog),17个字节

1=⊢∨(×/∘,↓)⌺3 3÷⊢

在线尝试!(将测试用例翻译为APL的信用证为ngn)

简要说明

(×/∘,↓)⌺3 3 获取每个元素与其邻居的乘积。

然后我将其除以论据÷⊢,以便矩阵中的每个条目都已映射到其邻居的乘积。

最后,我使用此矩阵获取参数的gcd ⊢∨,并检查是否等于1,1=

请注意,与ngn的答案一样,由于解释器中的错误,对于某些输入,此操作将失败。


2

JavaScript(ES6),121个字节

返回布尔值矩阵,其中false表示敌对。

m=>m.map((r,y)=>r.map((v,x)=>[...'12221000'].some((k,j,a)=>(g=(a,b)=>b?g(b,a%b):a>1)(v,(m[y+~-k]||0)[x+~-a[j+2&7]]||1))))

在线尝试!

怎么样?

隔离每个单元格的8个邻居的方法与我在此处描述的方法类似。

已评论

m =>                            // m[] = input matrix
  m.map((r, y) =>               // for each row r[] at position y in m[]:
    r.map((v, x) =>             //   for each value v at position x in r[]:
      [...'12221000']           //     we consider all 8 neighbors
      .some((k, j, a) =>        //     for each k at position j in this array a[]:
        ( g = (a, b) =>         //       g is a function which takes 2 integers a and b
            b ?                 //       and recursively determines whether they are
              g(b, a % b)       //       coprime to each other
            :                   //       (returns false if they are, true if they're not)
              a > 1             //
        )(                      //       initial call to g() with:
          v,                    //         the value of the current cell
          (m[y + ~-k] || 0)     //         and the value of the current neighbor
          [x + ~-a[j + 2 & 7]]  //
          || 1                  //         or 1 if this neighbor is undefined
  ))))                          //         (to make sure it's coprime with v)

2

MATL,22字节

tTT1&Ya3thYC5&Y)Zd1=A)

输入是一个矩阵。输出是带有敌对邻居的所有数字。

在线尝试!验证所有测试用例

工作实例说明

以输入[38, 77, 11; 17, 51, 32; 66, 78, 19]为例。堆栈内容从下至上显示。

t         % Implicit input. Duplicate
          % STACK: [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
                   [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
TT1&Ya    % Pad in the two dimensions with value 1 and width 1
          % STACK: [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
                   [1,  1,  1,  1,  1;
                    1,  38, 77, 11, 1;
                    1,  17, 51, 32, 1;
                    1,  66, 78, 19, 1
                    1,  1,  1,  1,  1]
3thYC     % Convert each sliding 3×3 block into a column (in column-major order)
          % STACK: [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
                   [ 1,  1,  1,  1, 38, 17,  1, 77, 51;
                     1,  1,  1, 38, 17, 66, 77, 51, 78;
                     1,  1,  1, 17, 66,  1, 51, 78,  1;
                     1, 38, 17,  1, 77, 51,  1, 11, 32;
                    38, 17, 66, 77, 51, 78, 11, 32, 19;
                    17, 66,  1, 51, 78,  1, 32, 19,  1;
                     1, 77, 51,  1, 11, 32,  1,  1,  1;
                    77, 51, 78, 11, 32, 19,  1,  1,  1;
                    51, 78,  1, 32, 19,  1,  1,  1,  1]
5&Y)      % Push 5th row (centers of the 3×3 blocks) and then the rest of the matrix
          % STACK: [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
                   [38, 17, 66, 77, 51, 78, 11, 32, 19]
                   [ 1,  1,  1,  1, 38, 17,  1, 77, 51;
                     1,  1,  1, 38, 17, 66, 77, 51, 78;
                     1,  1,  1, 17, 66,  1, 51, 78,  1;
                     1, 38, 17,  1, 77, 51,  1, 11, 32;
                    17, 66,  1, 51, 78,  1, 32, 19,  1;
                     1, 77, 51,  1, 11, 32,  1,  1,  1;
                    77, 51, 78, 11, 32, 19,  1,  1,  1;
                    51, 78,  1, 32, 19,  1,  1,  1,  1]
Zd        % Greatest common divisor, element-wise with broadcast
          % STACK: [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
                   [1,  1,  1,  1,  1,  1,  1,  1,  1;
                    1,  1,  1,  1, 17,  6, 11,  1,  1;
                    1,  1,  1,  1,  3,  1,  1,  2,  1;
                    1,  1,  1,  1,  1,  3,  1,  1,  1;
                    1,  1,  1,  1,  3,  1,  1,  1,  1;
                    1,  1,  3,  1,  1,  2,  1,  1,  1;
                    1, 17,  6, 11,  1,  1,  1,  1,  1;
                    1,  1,  1,  1,  1,  1,  1,  1,  1]
1=        % Compare with 1, element-wise. Gives true (1) or false (0)
          % STACK: [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
                   [1, 1, 1, 1, 1, 1, 1, 1, 1;
                    1, 1, 1, 1, 0, 0, 0, 1, 1;
                    1, 1, 1, 1, 0, 1, 1, 0, 1;
                    1, 1, 1, 1, 1, 0, 1, 1, 1;
                    1, 1, 1, 1, 0, 1, 1, 1, 1;
                    1, 1, 0, 1, 1, 0, 1, 1, 1;
                    1, 0, 0, 0, 1, 1, 1, 1, 1;
                    1, 1, 1, 1, 1, 1, 1, 1, 1]
A         % All: true (1) for columns that do not contain 0
          % STACK: [38, 77, 11;
                    17, 51, 32;
                    66, 78, 19]
                   [1, 0, 0, 0, 0, 0, 0, 0, 1]
)         % Index (the matrix is read in column-major order). Implicit display
          % [38, 19]

如果矩阵大于3x3,这可以工作吗?
罗伯特·弗雷泽

@RobertFraser是的,该过程不取决于矩阵大小。例如,请参见最后一个测试用例
Luis Mendo


1

果冻,24 字节

嗯,似乎很长。

ỊẠ€T
ŒJ_€`Ç€ḟ"J$ịFg"FÇịF

一个接受正整数列表的单子链接,该列表返回在敌对邻域中的每个值的列表(版本1,无重复数据删除)。

在线尝试!或见一个测试套件

怎么样?

ỊẠ€T - Link 1: indices of items which only contain "insignificant" values: list of lists
Ị    - insignificant (vectorises) -- 1 if (-1<=value<=1) else 0 
  €  - for €ach:
 Ạ   -   all?
   T - truthy indices

ŒJ_€`Ç€ḟ"J$ịFg"FÇịF - Main Link: list of lists of positive integers, M
ŒJ                  - multi-dimensional indices
    `               - use as right argument as well as left...
   €                -   for €ach:
  _                 -     subtract (vectorises)
      €             - for €ach:
     Ç              -   call last Link (1) as a monad
          $         - last two links as a monad:
         J          -   range of length -> [1,2,3,...,n(elements)]
        "           -   zip with:
       ḟ            -     filter discard (remove the index of the item itself)
            F       - flatten M
           ị        - index into (vectorises) -- getting a list of lists of neighbours
               F    - flatten M
              "     - zip with:
             g      -   greatest common divisor
                Ç   - call last Link (1) as a monad
                  F - flatten M
                 ị  - index into

1

Python 2中182个 177 166字节

lambda a:[[all(gcd(t,a[i+v][j+h])<2for h in[-1,0,1]for v in[-1,0,1]if(h|v)*(i+v>-1<j+h<len(a)>i+v))for j,t in E(s)]for i,s in E(a)]
from fractions import*
E=enumerate

在线尝试!

输出带有True / False条目的列表列表。


1

Haskell,95个字节

m?n|l<-[0..n-1]=[a|i<-l,j<-l,a<-[m!!i!!j],2>sum[1|u<-l,v<-l,(i-u)^2+(j-v)^2<4,gcd(m!!u!!v)a>1]]

在线尝试!

该函数?将矩阵m作为列表列表和矩阵大小n; 它返回敌对状态的条目列表。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.