2

如果我必须通过执行递归深度优先搜索来遍历六边形单元格表来检查其中的文本,如图所示:[在 StackOverflow 上键入显然不会保留格式。]

示例 1:

在此处输入图像描述

示例 2:

在此处输入图像描述

将它们识别为“细胞”的最佳方法是什么?换句话说,除了删除文本对角线并将它们转换为仅包含数字的二维数组之外,在代码中告诉计算机识别 x 特定数量的 y 字符类似于“单元格”的最佳方法是什么?

提前致谢。

4

1 回答 1

1

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
于 2015-04-23T19:18:18.323 回答