按坡度对区域进行分类


16

定义

ķ 的大小的正方形矩阵的环Ñ,其中1≤ķ≤天花板(N / 2)是通过的元件形成的列表ķ (N-K + 1)行和列,但没有第一个和最后一个k-1个元素。

例:

矩阵:

1 2 3 4 5
6 7 8 9 1
8 7 6 5 4
3 2 1 9 8
7 6 5 4 3

以圆环分隔:

+ ------------------- +
| 1 2 3 4 5 |
| + ----------- + |
| 6 | 7 8 9 | 1 |
| | + --- + | |
| 8 | 7 | 6 | 5 | 4 |
| | + --- + | |
| 3 | 2 1 9 | 8 |
| + ----------- + |
| 7 6 5 4 3 |
+ ------------------- +

上面的第一圈是1,2,3,4,5,1,4,8,3,4,5,6,7,3,8,6,第二圈是7,8,9,5,9,1,2,7第三是6

由正整数组成的N × N矩阵是(出于本挑战的目的):

  • 如果第k 环上的所有整数都严格大于第(k + 1)环上的整数,则凹入,其中k1N之间的任何整数 (第一个环上的大于第二个环上的,即反过来比第三等更大)。例:

    4 5 6 4 7->因为4,5,6,4,7,4,8,5,5,4,6,5,9,5,5,4都高于
    4 3 2 2 4 3、2、2、3、2、3、3、2中的任何一个都大于1
    5 2 1 3 8
    5 3 3 2 5
    9 5 6 4 5
    
  • 如果矩阵中的所有整数均相等,则为Flat。另一个例子(也许是多余的):

    2 2 2 2
    2 2 2 2
    2 2 2 2
    2 2 2 2
    
  • 如果第k 环上的所有整数都严格小于第(k + 1)环上的整数,则,其中k1N之间的任何整数 (第一个环上的整数小于第二个环上的整数,即依次低于第三个等)。例:

    1 2 1->因为1和2都小于6
    2 6 2
    1 2 1
    
  • 混合如果矩阵不满足任何的上述标准。例:

    3 3 3 3 3
    3 2 2 2 3
    3 2 3 2 3
    3 2 2 2 3
    3 3 3 3 3
    

挑战

给定大小为至少3的正整数的方阵,请根据上述定义对其进行分类。也就是说,根据矩阵是凹面,平坦,凸面还是混合矩阵,输出四个不同的一致值之一。

您可以使用任何编程语言进行竞争,并且可以通过任何标准方法并以任何合理的格式接受输入并提供输出,同时请注意,默认情况下会禁止这些漏洞。这是,因此每种语言的最短提交(以字节为单位)将获胜。

测试用例

这里有很多示例供您选择-我从每个类别中选择了6个。

[[3, 3, 3], [3, 1, 3], [3, 3, 3]]
[[2, 3, 4], [5, 1, 6], [7, 8, 9]]
[[19, 34, 45], [34, 12, 14], [13, 13, 13]]
[[3, 4, 3, 4], [4, 2, 1, 3], [3, 1, 2, 4], [4, 3, 4, 3]]
[[4, 5, 6, 4, 7], [4, 3, 2, 2, 4], [5, 2, 1, 3, 8], [5, 3, 3, 2, 5], [9, 5, 6, 4, 5]]
[[7, 7, 7, 7, 7], [7, 6, 6, 6, 7], [7, 6, 5, 6, 7], [7, 6, 6, 6, 7], [7, 7, 7, 7, 7]]

平面

[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
[[2, 2, 2], [2, 2, 2], [2, 2, 2]]
[[8, 8, 8], [8, 8, 8], [8, 8, 8]]
[[120, 120, 120], [120, 120, 120], [120, 120, 120]]
[[10, 10, 10, 10], [10, 10, 10, 10], [10, 10, 10, 10], [10, 10, 10, 10]]
[[5, 5, 5, 5, 5, 5], [5, 5, 5, 5, 5, 5], [5, 5, 5, 5, 5, 5], [5, 5, 5, 5, 5, 5], [5, 5, 5, 5, 5, 5], [5, 5, 5, 5, 5, 5]]

凸面

[[1, 2, 1], [2, 6, 2], [1, 2, 1]]
[[1, 1, 1], [1, 2, 1], [1, 1, 1]]
[[19, 34, 45], [34, 76, 14], [13, 6, 13]]
[[3, 3, 3, 3], [3, 4, 4, 3], [3, 4, 4, 3], [3, 3, 3, 3]]
[[192, 19, 8, 6], [48, 324, 434, 29], [56, 292, 334, 8], [3, 4, 23, 23]]
[[291, 48, 7, 5], [47, 324, 454, 30], [58, 292, 374, 4], [9, 2, 53, 291]]

混合的

[[1, 2, 3], [4, 5, 9], [6, 7, 8]]
[[10, 14, 21], [100, 8, 3], [29, 2, 19]]
[[5, 5, 5, 5], [5, 4, 4, 5], [5, 4, 6, 5], [5, 5, 5, 5]]
[[3, 3, 3, 3], [3, 1, 2, 3], [3, 3, 2, 3], [3, 3, 3, 3]]
[[12, 14, 15, 16], [12, 18, 18, 16], [12, 11, 11, 16], [12, 14, 15, 16]]
[[5, 5, 5, 5, 5], [5, 4, 4, 4, 5], [5, 4, 6, 4, 5], [5, 4, 4, 4, 5], [5, 5, 5, 5, 5]]

此挑战先前已发布在“ 沙箱”中。感谢那些在那里提供了宝贵反馈的人。
Xcoder先生18年

2
男孩,方便地将矩阵数组字符串转换为矩阵函数或从矩阵函数转换为矩阵函数,以处理各种语言的所有这些测试用例不是很好:)
ngm,

@ngm你不敢以为我们还没有人!:P
Xcoder先生18年

Answers:


5

的Java(JDK 10) 247个 232 220字节

x->{int i=0,j=x.length,k,m,M,p=0,P=0,r=0;for(;i<j;){for(M=m=x[k=i][--j];k<=j;)for(int q:new int[]{x[i][k],x[j][k],x[k][i],x[k++][j]}){m=m<q?m:q;M=M<q?q:M;}r=i++>0?(k=P<m?3:p>M?1:P==m?2:4)*r!=r*r?4:k:0;p=m;P=M;}return r;}

在线尝试!

输出:

  • 1 为“凹”
  • 2 为“平”
  • 3 对于“凸”
  • 4 用于“混合”

取消高尔夫:

x -> { // lambda that takes in the input int[][]
  int i = 0, // index of right bound of ring
      j = x.length, // index of left bound of ring
      k, // index of row-column-pair in ring
      m, // minimum of ring
      M, // maximum of ring
      p = 0, // minimum of previous ring
      P = 0, // maximum of previous ring
      r = 0; // result
  for (; i < j; ) { // iterate the rings from outside inwards
    // set both min and max to be to top right corner of the ring (and sneakily set some loop variables to save space)
    for (M = m = x[k = i][--j]; k <= j; ) // iterate the row-column pairs of the ring from top-right to bottom-left
      for (int q : new int[] {x[i][k], x[j][k], x[k][i], x[k++][j]}) { // iterate all of the cells at this row-column pair (and sneakily increment the loop variable k)
        // find new minimum and maximum
        m = m < q ? m : q;
        M = M < q ? q : M;
      }
    r = // set the result to be...
      i++ > 0 ? // if this is not the first ring... (and sneakily increment the loop variable i)
              // if the new result does not match the old result...
              (k = P < m ? // recycling k here as a temp variable to store the new result, computing the result by comparing the old and new mins/maxes
                         3
                         : p > M ?
                                 1
                                 : P == m ? 
                                          2
                                          : 4) * r != r * r ? // multiplying by r here when comparing because we want to avoid treating the case where r = 0 (unset) as if r is different from k
                                                            4 // set the result to "mixed"
                                                            : k // otherwise set the result to the new result
              : 0; // if this is the first ring just set the result to 0
    // set the old ring mins/maxes to be the current ones
    p = m; 
    P = M;
  }
  return r; // return the result
}

5

果冻 18 17  16 字节

我相信这项努力有很大的潜力

L‘HạŒỤṀ€IṠQṢ«FE$

接受数字列表的单子链接,该列表返回整数列表:

Concave ->  [0, 0]
Flat    ->  [-1, 0, 1]
Convex  ->  [-1, 0]
Mixed   ->  [-1, 0, 0]

在线尝试!或查看测试套件

L‘H可以用效率较低但原子较短的代替JÆm

怎么样?

L‘HạŒỤṀ€IṠQṢ«FE$ - Link: list of (equal length) lists of numbers
L                - length
 ‘               - increment
  H              - halve
                 -   = middle 1-based index (in both dimensions as the input is square)
    ŒỤ           - sort multi-dimensional indices by their corresponding values
                 -   = a list of pairs of 1-based indexes
   ạ             - absolute difference (vectorises)
                 -   = list of [verticalDistanceToMiddle, horizontalDistanceToMiddle] pairs
      Ṁ€         - maximum of €ach
                 -   each = N/2-k (i.e. 0 as middle ring and N/2 as outermost)
        I        - incremental deltas (e.g. [3,2,2,3,1]->[3-2,2-2,3-2,1-3]=[-1,0,1,-2])
         Ṡ       - sign (mapping -n:-1; 0:0; and +n:1)
          Q      - de-duplicate
           Ṣ     - sort
                 -   = concave:[0, 1]; convex:[-1, 0]; flatOrMixed:[-1, 0, 1]
               $ - last two links as a monad
             F   -   flatten
              E  -   all equal? (1 if flat otherwise 0)
            «    - minimum (vectorises)
                 -   = concave:[0, 0]; convex:[-1, 0]; mixed:[-1, 0, 0]; flat:[-1, 0, 1]

5

Python 2中219个 216 189 176字节

def g(M):A=[sorted((M[1:]and M.pop(0))+M.pop()+[i.pop(j)for j in[0,-1]for i in M])for k in M[::2]];S={cmp(x[j],y[~j])for x,y in zip(A,A[1:])for j in[0,-1]};return len(S)<2and S

在线尝试!

输出set([1]), set([0]), set([-1]),或分别False用于凹,平,凸或混合。

Thx for:ovs的一些优化后高达27个字节。然后再添加 13个字节。

列表理解A(由于ovs)将创建每个环的元素列表,并进行排序。

接下来,我们通过查看A中每个排序列表的th和th元素来比较相邻环之间的maxmin值。请注意,例如,如果是凹形的,则每个外环必须大于下一个最内环; 然后得出,每个外环的环也将大于下一个最内环的环。0-1Mminmaxmaxmin

如果M是凹面,平面或凸面,则这些min/max比较的集合中只有1个元素{-1, 0, 1};如果混合,将有两个或多个元素。


@ovs:真是不错。我通过将另一个字节转换为列表理解来保存了另一个字节(并认为这对于其他类似的挑战可能是非常有用的技术)。
Chas Brown

也许有一种方法可以缩短列表理解,但是while循环似乎仍然更短:while M:k=M[0]+M[-1];M=M[1:-1];A+=sorted(k+[i.pop(j)for j in[0,-1]for i in M]),(174字节)
ovs

@ovs:您,A=()的字节数已省略...
Chas Brown

我得到的174个字节A=()
OVS

啊! 抱歉,我误会了。这与您的早期版本(格式为)不同while M: A+= (some expression)
Chas Brown


4

JavaScript(ES6),168个字节

返回值:

  • -1 用于公寓
  • 0 对于混合
  • 1 对于凸
  • 2 用于凹面
f=(a,k=w=~-a.length/2,p,P,i,m,M,y=w)=>k<0?i%4%3-!i:a.map(r=>r.map(v=>Y|(X=k*k-x*x--)<0&&X|Y<0||(m=v>m?m:v,M=v<M?M:v),x=w,Y=k*k-y*y--))|f(a,k-1,m,M,i|M-m<<2|2*(M<p)|m>P)

在线尝试!

怎么样?

每个环上的最小和最大

我们计算每个环上的最小m和最大M。

我们通过计算距每个轴上矩阵中心的平方距离,来测试一个单元格是否位于给定的环上。取绝对值也可以,但是平方更短。

在小区(X,Y)位于上Ñ个环(0索引,从最外面的一个开始)如果满足下列公式是

((Y != 0) or (X < 0)) and ((X != 0) or (Y < 0))

哪里:

  • X =k²-(x-w)²
  • Y =k²-(y-w)²
  • w =(长度-1)/ 2
  • k = w-n

示例: 6x6矩阵第二环上的单元格(1,2)是吗?

  | 0 1 2 3 4 5   w = (6 - 1) / 2 = 2.5
--+------------   (x, y) --> ( x-w,  y-w) --> ((x-w)²,(y-w)²)
0 | 0 0 0 0 0 0   (1, 2) --> (-1.5, -0.5) --> (  2.25,   0.5)
1 | 0 1 1 1 1 0   
2 | 0[1]0 0 1 0   k = w - 1 = 1.5
3 | 0 1 0 0 1 0   k² = 2.25
4 | 0 1 1 1 1 0   X = 2.25 - 2.25 = 0 / Y = 2.25 - 0.5 = 1.75
5 | 0 0 0 0 0 0   ((X != 0) or (Y < 0)) is false, so (1,2) is on the ring

标志

在每次迭代结束时,我们将mM与上一个环的最小p和最大P进行比较,并相应地更新标志变量i

  • i |= 1如果m> P
  • i |= 2如果M <p
  • 如果M!= m我们设置i的较高位

在过程结束时,我们将i的最终值转换为:

i % 4  // isolate the 2 least significant bits (for convex and concave)
% 3    // convert 3 to 0 (for mixed)
- !i   // subtract 1 if i = 0 (for flat)

4

K(ngn / k)100 71 69字节

{$[1=#?,/a:(,/x)@.=i&|i:&/!2##x;;(&/m>1_M,0)-&/(m:&/'a)>-1_0,M:|/'a]}

在线尝试!

返回1=凹,::=平,-1=凸,0 =混合

::用作k中缺失值的占位符)


使用OK的&/1_`{&/+(y>|/x;y<&/x;,/x=/:y)}':(,/*:'(|+:)\)'-1_(-1_1_+-1_1_)\

@zgrep不错!:)随时将其发布为单独的答案,并根据需要从我的想法中获取想法-例如,看来我分成几圈的时间更短,但我还没有尝试过
ngn


哦,那是一个非常整齐的戒指!我喜欢。
zgrep

2

OK,56字节

&/1_`{&/+(y>|/x;y<&/x;,/x=/:y)}':{(,/x)@.=i&|i:&/!2##x}@

基于ngn的答案

在线尝试!

concave:1 0 0
   flat:0 0 1
 convex:0 1 0
  mixed:0 0 0

如果您将所有内容都转化为一个大lambda,则无需@:{&/1_`{&/+(y>|/x;y<&/x;,/x=/:y)}':(,/x)@.=i&|i:&/!2##x}
ngn

1

C ++ 17(gcc),411字节

#import<map>
#define R return
#define T(f,s)X p,c;for(auto&e:r){c=e.second;if(p.a&&!p.f(c)){s;}p=c;}R
using I=int;struct X{I a=0;I z=0;I f(I n){R!a||n<a?a=n:0,n>z?z=n:0;}I
l(X x){R z<x.a;}I g(X x){R a>x.z;}I e(X x){R a==z&a==x.a&z==x.z;}};I
N(I i,I j,I s){i*=s-i;j*=s-j;R i<j?i:j;}auto C=[](auto&&m){I
s=size(m),i=-1,j;std::map<I,X>r;for(;++i<s;)for(j=-1;++j<s;)r[N(i,j,s-1)].f(m[i][j]);T(g,T(l,T(e,R 0)3)2)1;};

新高分!(至少在发布时)哦,这有点漂亮,但仍然是C ++。

在线尝试!

Lambda C取a std::vector<std::vector<int>>并返回1(表示凹面),2(表示凸面),3(表示平面)或0(表示混合)。

更清晰的代码版本,带有描述性标识符,注释,R-> returnI-> int写出,等等:

#include <map>

// Abbreviation for golfing. Spelled out below.
#define R return

// Macro to test whether all pairs of consecutive Ranges in `rings`
// satisfy a condition.
// func: a member function of Range taking a second Range.
// stmts: a sequence of statements to execute if the condition is
//        not satisfied. The statements should always return.
//        May be missing the final semicolon.
// Expands to a statement, then the return keyword.
// The value after the macro will be returned if all pairs of Ranges
// satisfy the test.
#define TEST(func, stmts)                                     \
    Range prev, curr;                                         \
    for (auto& elem : rings) {                                \
        curr = elem.second;                                   \
        // The first time through, prev.a==0; skip the test.  \
        if (prev.a && !prev.func(curr))                       \
        { stmts; }                                            \
        prev = curr;                                          \
    }                                                         \
    return

// Abbreviation for golfing. Spelled out below.
using I = int;

// A range of positive integers.
// A default-constructed Range is "invalid" and has a==0 && z==0.
struct Range
{
    int a = 0;
    int z = 0;
    // Add a number to the range, initializing or expanding.
    // The return value is meaningless (but I is shorter than void for golfing).
    int add(int n) {
        return !a||n<a ? a=n : 0, n>z ? z=n : 0;
        /* That is:
        // If invalid or n less than previous min, set a.
        if (a==0 || n<a)
            a = n;
        // If invalid (z==0) or n greater than previous max, set z.
        if (n>z)
            z = n;
        return dummy_value;
        */
    }

    // Test if all numbers in this Range are strictly less than
    // all numbers in Range x.
    int less(Range x)
    { return z < x.a; }

    // Test if all numbers in this Range are strictly greater than
    // all numbers in Range x.
    int greater(Range x)
    { return a > x.z; }

    // Test if both this Range and x represent the same single number.
    int equal(Range x)
    { return a==z && a==x.a && z==x.z; }
};

// Given indices into a square matrix, returns a value which is
// constant on each ring and increases from the first ring toward the
// center.
// i, j: matrix indices
// max: maximum matrix index, so that 0<=i && i<=max && 0<=j && j<=max
int RingIndex(int i, int j, int max)
{
    // i*(max-i) is zero at the edges and increases toward max/2.0.
    i *= max - i;
    j *= max - j;
    // The minimum of these values determines the ring.
    return i < j ? i : j;
}

// Takes a container of containers of elements convertible to int.
// Must represent a square matrix with positive integer values.
// Argument-dependent lookup on the outer container must include
// namespace std, and both container types must have operator[] to
// get an element.  (So std::vector or std::array would work.)
// Returns:
//   1 for a concave matrix
//   2 for a convex matrix
//   3 for a flat matrix
//   0 for a mixed matrix
auto C /*Classify*/ = [](auto&& mat)
{
    int mat_size=size(mat), i=-1, j;
    std::map<int, Range> rings;

    // Populate rings with the range of values in each ring.
    for (; ++i<mat_size;)
        for (j=-1; ++j<mat_size;)
            rings[RingIndex(i, j, mat_size-1)].add(mat[i][j]);

    // Nested macros expand to
    // Range prev, curr; for ... if (...) {
    //   Range prev, curr; for ... if (...) {
    //     Range prev, curr; for ... if (...) {
    //       return 0;
    //     } return 3;
    //   } return 2;
    // } return 1
    // Note each scope declares its own prev and curr which hide
    // outer declarations.
    TEST(greater, TEST(less, TEST(equal, return 0) 3) 2) 1;
};

1
我不认为“漂亮”意味着您认为的含义
仅ASCII
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.