Challenge
Given a matrix of positive integers, determine if there are any "rings" of mountains. The formal definition for this challenge is: given a matrix of positive integers, is there any positive integer n
for which there is a closed ring of cells in the matrix that are strictly greater than n
such that all cells enclosed in the ring are less than or equal to n
.
Let's take a truthy example:
3 4 5 3
3 1 2 3
4 2 1 3
4 3 6 5
If we set n
to 2
:
1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1
As we can clearly see, the 1
s along the edge form a ring.
We define a ring as an ordered collection of cells where adjacent cells in the collection are also adjacent (edge or corner) on the grid. Additionally, the ring must contain at least 1 cell inside of it; that is, attempting to edge-only BFS-floodfill the entire matrix excluding the cells in the collection and never traversing a cell in the collection must miss at least one cell.
Truthy Test Cases
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
Falsy Test Cases
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
Rules
- Standard Loopholes Apply
- This is code-golf, so the shortest answer in bytes in each language is declared the winner of its language. No answers will be accepted.
- The input may be taken as any reasonable form for a matrix of positive integers
- The output may be given as any two reasonable, consistent, distinct values indicating [true] or [false].
n
is the third "truthy" test case actually truthy? [1,2]? \$\endgroup\$