有山环吗?


14

挑战

给定一个正整数矩阵,请确定山是否存在“环”。对此挑战的正式定义是:给定一个正整数矩阵,是否存在任何正整数,矩阵中n存在一个闭环,该闭环严格大于n以使环中包含的所有单元均小于或等于到n

让我们举一个真实的例子:

3 4 5 3
3 1 2 3
4 2 1 3
4 3 6 5

如果我们设置n2

1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1

我们可以清楚地看到,1沿边缘的s形成一个环。

我们将环定义为单元的有序集合,其中集合中的相邻单元在网格上也相邻(边或角)。此外,该环内部必须至少包含1个单元;也就是说,尝试仅边沿BFS填充整个矩阵(不包括集合中的单元),并且从不遍历集合中的一个单元,则必须至少丢失一个单元。

真实的测试案例

4 7 6 5 8 -> 1 1 1 1 1
6 2 3 1 5 -> 1 0 0 0 1 (n = 3)
6 3 2 1 5 -> 1 0 0 0 1
7 5 7 8 6 -> 1 1 1 1 1

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

1 3 1
3 1 3
1 3 1

7 5 8 7 5 7 8 -> if you have n = 4, you get an interesting ridge shape around the top and right of the grid
8 4 4 2 4 2 7
6 1 8 8 7 2 7
5 4 7 2 5 3 5
5 6 5 1 6 4 5
3 2 3 2 7 4 8
4 4 6 7 7 2 5
3 2 8 2 2 2 8
2 4 8 8 6 8 8

5 7 6 8 6 8 7 -> there is an island in the outer ring (n = 4), but the island is a ring
5 3 2 4 2 4 7
6 3 7 8 5 1 5
8 2 5 2 8 2 7
8 3 8 8 8 4 7
6 1 4 1 1 2 8
5 5 5 5 7 8 7

150 170 150
170 160 170
150 170 150

虚假测试用例

1 2 3 2 1 -> this is just a single mountain if you picture it graphcially
2 3 4 3 2
3 4 5 4 3
2 3 4 3 2
1 2 3 2 1

4 5 4 3 2 -> this is an off-centered mountain
5 6 5 4 3
4 5 4 3 2
3 4 3 2 1

1 1 1 1 1 -> this is four mountains, but they don't join together to form a ring
1 2 1 2 1
1 1 1 1 1
1 2 1 2 1
1 1 1 1 1

3 3 3 3 3 -> there is a ring formed by the `3`s, but the `4` in the middle is taller so it doesn't qualify as a mountain ring
3 1 1 1 3
3 1 4 1 3
3 1 1 1 3
3 3 3 3 3

3 4 4 4 3
4 4 3 4 4
3 3 3 3 4
4 4 3 4 4
3 4 4 4 3

1  2  3  4  5
6  7  8  9  10
11 12 13 14 15
16 17 18 19 20
22 23 24 25 26

规则

  • 适用标准漏洞
  • 这是,因此每种语言中以字节为单位的最短答案被宣布为其语言的赢家。不接受任何答案。
  • 对于正整数矩阵,输入可以采取任何合理形式
  • 可以将输出指定为表示[true]或[false]的两个合理,一致,不同的值。

对于n第三个“真实的”测试案例而言,真实情况是什么?[1,2]
暴民埃里克(Erik the Outgolfer)'18年

@EriktheOutgolfer 3s的环由角相邻。所以,是的。
user202729

Answers:


3

果冻,38 个字节

Ẇ€Z$⁺Ẏµ,ZẈ>2ẠµƇµḊṖZƊ⁺FṀ<,Z.ịḊṖ$€Ɗ€ƊȦ)Ṁ

在线尝试!

如果矩阵包含山脉,则输出1,否则输出0

工作方式(略过时)

我也许可以缩短一些代码,所以本节可能要进行大量编辑。

助手链接

,Z.ịḊṖ$€Ɗ€ – Helper link. Let S be the input matrix.
,Z         – Pair S with its transpose.
        Ɗ€ – For each matrix (S and Sᵀ), Apply the previous 3 links as a monad.
  .ị       – Element at index 0.5; In Jelly, the ị atom returns the elements at
             indices floor(x) and ceil(x) for non-integer x, and therefore this
             returns the 0th and 1st elements. As Jelly is 1-indexed, this is the
             same as retrieving the first and last elements in a list.
    ḊṖ$€   – And for each list, remove the first and last elements.

例如,给出以下形式的矩阵:

A(1,1) A(1,2) A(1,3) ... A(1,n)
A(2,1) A(2,2) A(2,3) ... A(2,n)
A(3,1) A(3,2) A(3,3) ... A(3,n)
...
A(m,1) A(m,2) A(m,3) ... A(m,n)

这将返回数组(顺序无关紧要):

A(1,2), A(1,3), ..., A(1,n-1)
A(m,2), A(m,3), ..., A(m,n-1)
A(2,1), A(3,1), ..., A(m-1,1)
A(2,n), A(3,n), ..., A(m-1,n)

长话短说,这会生成最外层的行和列,并且去除了边角。

主要链接

Ẇ€Z$⁺Ẏµ,ZẈ>2ẠµƇµḊṖZƊ⁺FṀ<ÇȦ)Ṁ – Main link. Let M be the input matrix.
Ẇ€                           – For each row of M, get all its sublists.
  Z$                         – Transpose and group into a single link with the above.
    ⁺                        – Do twice. So far, we have all contiguous sub-matrices.
     Ẏ                       – Flatten by 1 level.
      µ      µƇ              – Filter-keep those that are at least 3 by 3:
       ,Z                      – Pair each sub-matrix S with Sᵀ.
         Ẉ                     – Get the length of each (no. rows, no. columns).
          >2                   – Element-wise, check if it's greater than 2.
            Ạ                  – All.
               µ          )  – Map over each sub-matrix S that's at least 3 by 3
                ḊṖ           – Remove the first and last elements.
                  ZƊ         – Zip and group the last 3 atoms as a single monad.
                    ⁺        – Do twice (generates the inner cells).
                     FṀ      – Flatten, and get the maximum.
                       <Ç    – Element-wise, check if the results of the helper
                               link are greater than those in this list.
                         Ȧ   – Any and all. 0 if it is empty, or contains a falsey
                               value when flattened, else 1.
                           Ṁ – Maximum.

2

干净224个 ... 161个字节

import StdEnv,StdLib
p=prod
~ =map
^ =reverse o$
@ =transpose o~(^o^)
$l=:[h:t]|h>1=l=[1: $t]
$e=e
?m=p[p(~p(limit(iterate(@o@)(~(~(\a|a>b=2=0))m))))\\n<-m,b<-n]

在线尝试!

定义函数? :: [[Int]] -> Int0如果有铃声则返回,1否则返回。

通过将矩阵变成2s(代表山脉)和0s(代表山谷),然后用1s 淹没,直到结果停止变化为止。如果任何0山高仍然存在,则乘积将为0


1

JavaScript(Node.js),302字节

a=>a.some((b,i)=>b.some((n,j)=>(Q=(W=(i,j,f)=>[a.map((b,I)=>b.map((t,J)=>I==i&J==j)),...a+0].reduce(A=>A.map((b,I)=>b.map((t,J)=>f(I)(J)&&(A[I-1]||b)[J]|(A[I+1]||b)[J]|b[J-1]|b[J+1]|t))))(i,j,I=>J=>a[I][J]<=n)).some((b,i)=>b.some((d,j)=>d&&!i|!j|!Q[i+1]|b[j+1]==b.b))<!/0/.test(W(0,0,I=>J=>!Q[I][J]))))

在线尝试!

检查是否从某个点流出无法到达边界,而边界可以走到每个点

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.