在游戏中( it is c++ board game like matrix with soldiers which can use formation
)士兵可以以队形部署。(在地图/矩阵上的一个单元上只能部署一个士兵,编队可以在8个方向上,与x轴成0,45,90,135,180,225,270,315度,当士兵在编队时,他们在相邻的单元中,例如,除了x2- x1 =1之外,0度y的编队对所有人都是相同的,依此类推,45度y2-y1=1和x2 -x1 =1 )。队形有line和hollow_square。我需要一个非常有效的方法来检查士兵是否在编队(士兵在课堂上有x和y自己的位置)。对于第i行,按x排序(我有std::list<std::pair<int,int> >
,它代表部队中士兵的位置),并检查相邻元素(in case 0, 180; in case 45, 135 ,225, 315 also check for difference for y is 1 or -1
)之间的差异是否为1。如果士兵在编队,如何检查hollow_square?
(就像这张丑陋的图片,澄清一下)
发布于 2015-08-27 01:28:59
你可以试试这个:
Make a copy of the board
Color the squares where you have soldiers using the first available color
For each square:
if the square is white, then
flood fill the board starting at this square with the next available color
while flood filling, check whether all border squares are the soldier color
if so, you have some closed formation. Otherwise, you just colored the unoccupied parts of the board.
由此产生的彩色棋盘将有一种颜色,其中有部队和独特的颜色,用于未占用和封闭的空间。你可能会考虑的一些事情--允许在队形内形成队形吗?
https://stackoverflow.com/questions/16623586
复制相似问题