Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

The matrix has U

Given a matrix of size at least ×ばつ3 formed by positive integers, determine if it contains at least one "U" pattern, defined as

+ + + - - - + +
+ + - N - N - +
+ + - N - N - +
+ + - N N N - +
+ + + - - - + +

where

  • N is the same number, repeated in those seven positions
  • - (optional) represents any number different than N. Each - can be a different number
  • + (optional) represents any number. Each + can be a different number.

The amount of + and - entries obviously depends on the size of the matrix. In particular, some - may not exist because the pattern is adjacent to a matrix border. The above representation corresponds to a ×ばつ8 matrix.

The pattern must have the specified orientation. Reflections or rotations are not valid.

Test cases

Truthy

  • Pattern with N=8:

     3 4 7 5 6 5 4 8
     8 7 3 8 5 8 2 4
     9 9 9 8 7 8 1 3
     4 5 3 8 8 8 3 6
     6 8 9 2 3 2 1 4
    
  • Same pattern with some N values nearby:

     3 4 7 5 6 5 8 8
     8 7 3 8 5 8 2 4
     9 9 9 8 7 8 1 3
     4 5 3 8 8 8 3 6
     6 8 8 2 3 2 1 4
    
  • Pattern with N=3, touching a matrix border:

     7 5 4 7 5 4 5 6
     7 1 5 3 5 3 6 3
     3 5 3 3 9 3 2 3
     3 1 2 6 7 3 3 3
     4 5 2 8 9 6 8 4 
    
  • Pattern with N=4, touching a matrix corner:

     4 1 4 6
     4 3 4 3 
     4 4 4 5
     7 5 3 5
    
  • Two patterns, with N=5 and N=9:

     6 7 9 4 5 6 7
     5 2 5 9 8 9 8
     5 1 5 9 6 9 3
     5 5 5 9 9 9 4
     8 7 6 1 3 2 5
    
  • Pattern with N=3, and broken pattern with 1:

     1 2 1 2 3 2 3
     1 2 1 2 3 2 3
     1 1 1 1 3 3 3
    
  • Numbers can be greater than 9; here N=25:

     23 56 34 67 34 3
     34 25 4 25 48 49
     24 25 97 25 56 56
     12 25 25 25 32 88
    
  • Minimalistic case, N=2:

     2 1 2
     2 5 2
     2 2 2
    

Falsy

  • Nothing special here:

     7 8 6 5 4
     3 4 5 6 3
     3 5 6 4 4
     7 8 9 3 2
    
  • Rotated or reflected patterns are not valid:

     9 9 9 3 7 7 7 5
     4 4 9 2 7 8 7 6
     9 9 9 8 7 9 7 4
    
  • Some - entry spoils the pattern

     9 5 5 6 5
     3 8 5 9 5
     2 9 5 5 5
    
  • Some - entry spoils the pattern, even if the result would be a "U" with longer horns

     7 8 5 2 5
     9 2 5 6 5
     3 8 5 9 5
     2 9 5 5 5
    
  • Minimalistic case, no pattern

     9 9 9
     9 8 9
     9 9 9 
    

Additional rules

  • You can choose to output:
    • Any two distinct values/arrays/strings... depending on whether the matrix contains the specified pattern or not; or
    • Anything truthy if the matrix contains the specified pattern, and anything falsy otherwise. The specific truthy and falsy values/arrays/strings... can be different for different inputs.
  • The code should work in theory for matrices of arbitrarily large size, containing arbitrarily large numbers. In practice, it is acceptable if the program is limited by time, memory or data-type restrictions.
  • Input and output are flexible as usual. Programs or functions are allowed, in any programming language. Standard loopholes are forbidden.
  • Shortest code in bytes wins.

Answer*

Draft saved
Draft discarded
Cancel
0

AltStyle によって変換されたページ (->オリジナル) /