# Thread: Searching through a grid of numbers

1. ## Searching through a grid of numbers

Hello:
Right now say; I have a 12x6 grid full of numbers from 1-999.
If I want to find a sequence of numbers in this grid that exists in a straight line in the following 8 directions: North, North East, East, South East, South, South West, West, and North West... how would I do this?
Whats the best way to loop through the inputted sequence and search for it in the grid?
I am completely lost here so any help would be greatly appreciated!

2. I would have a one-dimensional array to hold your sequence.

int sequence[SEQ_SIZE];

I would have a two-dimensional array to hold your grid of numbers.

int grid[12][6];

Work through the two-dimensional array from left to right and top to bottom. Check each number in the grid to see if it matches the first number in the sequence. If it does, search around that grid location to see if you can find the second number in the sequence. If you find the second number, search around that grid location to find the third number. If you don't find the next number in the sequence, keep moving down the grid until you come to the next instance of the first number of the sequence you're searching for. Rinse and repeat until all numbers in the sequence have been found.

Since you know the current location you're searching, simple addition or subtraction to the row and col numbers (i.e. grid[row-1][col+1] to search north east) will get you the other locations you need to search. I would be sure to check to make sure that the row and col numbers are in bounds even after you add or subtract from them, however.