Classifique uma região por sua inclinação


16

Definições

O k ésimo anel de uma matriz quadrada de tamanho N , em que 1 ≤ k ≤ teto (N / 2) é a lista formada pelos elementos das k e e (N-k + 1) ésima filas e colunas, mas sem a primeiro e último elementos k-1 .

Exemplo:

Matriz:

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

Delimitado em anéis:

+ ------------------- +
| 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 |
+ ------------------- +

O primeiro anel do anterior é 1,2,3,4,5,1,4,8,3,4,5,6,7,3,8,6, o segundo é 7,8,9,5,9,1,2,7e o terceiro é 6.

Um N por N da matriz de números inteiros positivos é (para os fins desta desafio):

  • côncava se todos os números inteiros sobre o k th anel são estritamente maior do que aqueles sobre o (k + 1) th anel, onde K é qualquer número inteiro entre 1 e N (aqueles no primeiro anel são maiores do que aqueles no segundo, que são por sua vez, maior que os do terceiro, etc.). Exemplo:

    4 5 6 4 7 -> porque 4,5,6,4,7,4,8,5,5,4,6,5,9,5,5,4 são todos maiores que
    4 3 2 2 4 qualquer um dos 3,2,2,3,2,3,3,2, todos superiores a 1
    5 2 1 3 8
    5 3 3 2 5
    9 5 6 4 5
    
  • flat se todos os números inteiros na matriz forem iguais. Outro exemplo (talvez redundante):

    2 2 2 2
    2 2 2 2
    2 2 2 2
    2 2 2 2
    
  • convexo se todos os números inteiros no k- ésimo anel forem estritamente inferiores aos do (k + 1) -ésimo anel, onde k é qualquer número inteiro entre 1 e N (aqueles no primeiro anel são inferiores aos do segundo anel, que são por sua vez, inferiores aos do terceiro, etc.). Exemplo:

    1 2 1 -> porque 1 e 2 são ambos inferiores a 6
    2 6 2
    1 2 1
    
  • misturado se a matriz não atender a nenhum dos critérios acima. Exemplo:

    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
    

Desafio

Dada uma matriz quadrada de números inteiros positivos de tamanho pelo menos 3 , classifique-a de acordo com as definições acima. Ou seja, gera um dos quatro valores consistentes diferentes , com base no fato de a matriz ser côncava, plana, convexa ou mista.

Você pode competir em qualquer linguagem de programação e pode receber e fornecer saída por qualquer método padrão e em qualquer formato razoável, observando que essas brechas são proibidas por padrão. Isso é , então a submissão mais curta (em bytes) para todos os idiomas vence.

Casos de teste

Aqui estão alguns exemplos para você escolher - selecionei 6 de cada categoria.

Côncavo

[[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]]

Plano

[[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]]

Convexo

[[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]]

Misturado

[[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]]

Este desafio foi publicado anteriormente na Sandbox . Obrigado a quem deu um feedback valioso por lá.
Mr. Xcoder

2
Rapaz, não seria bom ter algum matriz-de-array conversão de cadeia de / para a matriz de funções úteis para processar todos esses casos de teste em uma variedade de línguas :)
ngm

@ngm Não se atreva a pensar que ainda não temos um ! : P
Sr. Xcoder

Respostas:


5

Java (JDK 10) , 247 232 220 bytes

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;}

Experimente online!

Saídas:

  • 1 para "côncavo"
  • 2 para "apartamento"
  • 3 para "convexo"
  • 4 para "misto"

Ungolfed:

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

Geléia ,  18 17  16 bytes

Eu acredito que há muito potencial para esse esforço ser jogado fora do golfe

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

Um link monádico que aceita uma lista de listas de números que retorna uma lista de números inteiros:

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

Experimente online! Ou veja a suíte de testes .

L‘Hpoderia ser substituído pelo menos eficiente, mas atomicamente mais curto JÆm.

Quão?

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 bytes

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

Experimente online!

Saídas set([1]), set([0]), set([-1]),ou Falsepara côncavo, plano, convexo ou misto, respectivamente.

Thx for: A 27 bytes impressionantes de algumas otimizações por ovs . E depois outros 13 bytes depois.

A compreensão da lista A(devido aos ovs) cria uma lista dos elementos de cada anel, classificados.

Em seguida, comparamos os valores maxe minentre os anéis adjacentes observando os 0th e -1th th elementos de cada lista classificada em A. Observe que se, por exemplo, Mé côncavo, mincada anel externo deve ser maior que maxo próximo anel mais interno ; e segue-se que maxcada anel externo também será maior quemin o próximo anel mais interno.

Se Mfor côncavo, plano ou convexo, o conjunto dessas min/maxcomparações terá apenas 1 elemento {-1, 0, 1}; se estiver misturado, haverá dois ou mais elementos.


@ovs: Isso é bonito; Salvei outro byte, transformando-o em uma compreensão de lista (e pensando que essa poderia ser uma técnica muito útil para outros desafios semelhantes).
Chas Brown

Talvez exista uma maneira de diminuir a compreensão da lista, mas um loop while ainda parece ser mais curto: 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 bytes)
ovs

@ovs: Você omitiu ,A=()de sua contagem de bytes ...
Chas Brown

Eu recebo 174 bytes comA=()
ovs

Ah! Desculpas, eu não entendi. Isso é diferente do que a versão anterior, que era da forma: while M: A+= (some expression).
Chas Brown


4

JavaScript (ES6), 168 bytes

Devoluções:

  • -1 para apartamento
  • 0 para misturado
  • 1 para convexo
  • 2 côncavo
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)

Experimente online!

Quão?

Mínimo e máximo em cada anel

Calculamos o mínimo de m e o valor máximo M em cada anel.

Testamos se uma célula está localizada em um determinado anel, calculando a distância ao quadrado do centro da matriz em cada eixo. Tomando o valor absoluto funcionaria tão bem, mas a quadratura é mais curta.

Uma célula em (x, y) está localizada no n- ésimo anel (indexado 0, iniciando no mais externo) se a seguinte fórmula for falsa :

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

Onde:

  • X = k² - (x - w) ²
  • Y = k² - (y - w) ²
  • w = (a.length - 1) / 2
  • k = w - n

Exemplo: a célula (1, 2) está no 2º anel de uma matriz 6x6?

  | 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

Bandeiras

No fim de cada iteração, que compara m e M contra o mínimo de p e o máximo P do anel anterior e actualizar a variável bandeira i em conformidade:

  • i |= 1se m> P
  • i |= 2se M <p
  • definimos bits mais altos de i se M! = m

No final do processo, convertemos o valor final de i da seguinte maneira:

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 bytes

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

Experimente online!

retornos 1= côncavo, ::= plano, -1= convexo, 0= misto

( ::é usado como um espaço reservado para valores ausentes em k)


Uma estratégia diferente, usando oK:&/1_`{&/+(y>|/x;y<&/x;,/x=/:y)}':(,/*:'(|+:)\)'-1_(-1_1_+-1_1_)\
zgrep

@zgrep nice! :) Sinta-se livre para postar como uma resposta em separado e ter idéias de meu se você quiser - por exemplo, parece que minha dividindo-se em anéis é mais curto, mas eu não tentei em oK ainda
NGN


Ooh, essa é uma divisão de anel muito legal! Eu gosto disso.
Zgrep 2/06

2

OK , 56 bytes

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

Baseado na resposta da ngn .

Experimente online!

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

não é necessário @ se você transformar tudo em um grande lambda:{&/1_`{&/+(y>|/x;y<&/x;,/x=/:y)}':(,/x)@.=i&|i:&/!2##x}
ngn

1

C ++ 17 (gcc) , 411 bytes

#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;};

Um novo recorde! (no momento da postagem, pelo menos) Oh, bem, é um pouco bacana, mas ainda é C ++.

Experimente online!

O Lambda Cpega ae std::vector<std::vector<int>>retorna 1 para côncavo, 2 para convexo, 3 para plano ou 0 para misto.

Uma versão mais legível do código, com identificadores descritivos, comentários, R-> returne I-> intescritos, etc .:

#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
Eu não acho que significa 'bacana' o que você acha que isso significa
ASCII-only
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.