# How to match a 2d pattern in a 2d array?

This topic is 1900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to figure out how it's possible to match a 2D pattern inside a 2D array as shown below;

2D array
1, 0, 0, 1, 0, 1
1, 0, 1, 1, 1, 1
0, 1, 0, 0, 1, 0
1, 0, 1, 1, 0, 1
0, 0, 0, 0, 1, 0
1, 1, 1, 1, 0, 1

2D pattern to be matched
1, 0, 1
0, 1, 0
1, 0, 1

Let's mention that the 2D array can be larger in other contexts. I'm also using 0 and 1 as values, but it could be any other IDs. I just tried to keep my example simple. Anyone know what would be the most efficient and fastest way to match all patterns in the 2d array in a C++ or C# program (or whatever)?

It's for a game I'm trying to make. When clicking with the mouse the player set tiles on the grid, so in real-time I need to check if the pattern condition is met each time the player click to change a tile.

URLs or tutorials with similar topics would be greatly appreciated. Thank you for your insights! Edited by thescholar

##### Share on other sites
You need to iterate through your pattern and test it ID by ID at position (0,0), then move to (1,0) do the same, etc, for all positions.
As soon as you find a mismatch, you can move on, so maybe not as slow as you think.

Though!
If a pattern appeared, it must contain the newly placed tile right?
So you don't have to check through the whole array, only in a box roughly 2x the size of your pattern, around the position where the user clicks. Edited by Olof Hedman

##### Share on other sites
The brute force method above by Olof is the fastest you can achieve for matching any random pattern to any random 2d grid during run-time.

However, assuming you have a finite set of defined dimensions for your pattern (3x3, 2x2, 4x4 for example), then you could create separate data structures for the large 2d grid at the beginning of your program. So, for example, you create a 4x4 grid of unique IDs from your 6x6 2d array for 3x3 pattern matching. The IDs calculated can be created from a hashmap function that you define to return a unique ID for a given 3x3 input, and then at runtime you simply pass in your 3x3 pattern to the function and then look to see if the ID returned is in your 4x4 grid. You would have to create separate data structures in the beginning of your program for the different dimensions of patterns you planned to support.

##### Share on other sites
i think brute force is not the fastest method possible, but it's the easiest to implement.
how are you storing your data?
an adaption of a text search algorithm like boyer moore could be faster, also clustering the 0,1 into integers, and compare at integer level would be alot faster

if you need all matches of the pattern i'd use 2d convolution on the input data, with the pattern as filter kernel.
if your data is really really huge you can also speed up the convolution by fourier transform...

##### Share on other sites

This topic is 1900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.