user3835720
user3835720

Reputation: 81

Depth First Search in C

If I have to iterate through a table of hexagonal cells checking for text inside them by conducting a recursive depth first search, arranged as shown: [Typing it out on StackOverflow apparently doesn't keep the formatting.]

Example 1:

enter image description here

Example 2:

enter image description here

What would be the best way to identifying them as "cells?" In other words, besides removing the textual diagonal lines and converting them into a 2D array with just numbers in it, what would be the best way to tell the computer in code to recognize x certain number of y characters resembles a "cell?"

Thanks in advance.

Upvotes: 2

Views: 151

Answers (1)

plesiv
plesiv

Reputation: 7028

Easiest way to represent a hexagonal grid would be plain 2-d array with special rule about neighborhood of the cells. Take your second case for example, in matrix form it would be:

char M[][] = 

{
 { 'b', 'g', 'g', 'b', ' ' },
 { 'g', ' ', 'B', 'B', 'B' },
 { 'g', 'B', ' ', 'b', 'g' },
 { 'B', ' ', 'g', 'g', 'g' }
}

Element in column m in row n is neighbor with:

  • elements in columns m and m + 1 in row n - 1
  • elements in columns m - 1 and m + 1 in row n
  • elements in columns m - 1 and m in row n + 1

Upvotes: 1

Related Questions