Uma matriz de sinal alternada é uma matriz n
by que n
consiste nos números -1, 0, 1, de modo que:
- A soma de cada linha e coluna é 1
- As entradas diferentes de zero em cada linha e coluna se alternam no sinal
Essas matrizes generalizam matrizes de permutação e o número de matrizes para um dado n
era de interesse por algum tempo. Eles ocorrem naturalmente durante o método de condensação de Dodgson para calcular determinantes da matriz (nomeado em homenagem a Charles Dodgson, mais conhecido como Lewis Carroll).
Aqui estão alguns exemplos de matrizes de sinais 4 por 4 alternadas:
0 1 0 0 1 0 0 0 0 0 1 0 0 0 1 0
0 0 1 0 0 0 1 0 0 1 -1 1 1 0 -1 1
1 0 0 0 0 1 -1 1 1 -1 1 0 0 1 0 0
0 0 0 1 0 0 1 0 0 1 0 0 0 0 1 0
E aqui estão alguns exemplos de matrizes 4 por 4 que não são matrizes de sinais alternadas:
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 -1 (last row and last column don't add to 1)
0 0 0 1
1 0 0 0
-1 1 1 0
1 0 0 0 (third row does not alternate correctly)
O seu programa ou função será dada uma n
por n
matriz ( n >= 1
) de -1S, 0s e 1s - saída de um valor truthy se a matriz dada é uma matriz sinal alternado, caso contrário, a saída de um valor Falsas.
Isso é código-golfe , então o objetivo é minimizar o número de bytes usados.
Casos de teste
Os seguintes casos de teste são fornecidos em um formato de lista 2D semelhante ao Python.
Verdade:
[[1]]
[[1,0],[0,1]]
[[0,1],[1,0]]
[[0,1,0],[0,0,1],[1,0,0]]
[[0,1,0],[1,-1,1],[0,1,0]]
[[0,1,0,0],[0,0,1,0],[1,0,0,0],[0,0,0,1]]
[[1,0,0,0],[0,0,1,0],[0,1,-1,1],[0,0,1,0]]
[[0,0,1,0],[0,1,-1,1],[1,-1,1,0],[0,1,0,0]]
[[0,0,1,0],[1,0,-1,1],[0,1,0,0],[0,0,1,0]]
[[0,0,1,0,0],[0,1,-1,1,0],[1,-1,1,0,0],[0,1,0,-1,1],[0,0,0,1,0]]
[[0,0,1,0,0,0,0,0],[1,0,-1,0,1,0,0,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