检测地图是否全通的算法,摘自视频的MapGenerator

bool MapIsFullyAccessible(bool[,]obstacleMap, int currentObstacleCount){bool[,] mapFlags = new bool[obstacleMap.GetLength(0),obstacleMap.GetLength(1)];//检查过的存个标记Queuequeue = new Queue();

queue.Enqueue (mapCenter);

mapFlags [mapCenter.x, mapCenter.y] = true;

int accessibleTileCount = 1;

while(queue.Count >0){

Coord tile = queue.Dequeue ();

for (int x = -1; x <= 1; x++) {

for (int y = -1; y <= 1; y++) {

int neighbourX = tile.x + x;

int neighbourY = tile.y + y;

if(x== 0 ||y==0 ){//保证是检测横竖4个值,而不检测四个角的四个值

if(neighbourX >= 0 && neighbourX < obstacleMap.GetLength(0)&& neighbourY >= 0 && neighbourY < obstacleMap.GetLength(1)){//保证所检测的位置不超出地图边界

if(!mapFlags[neighbourX,neighbourY] && !obstacleMap[neighbourX,neighbourY]){

mapFlags [neighbourX,neighbourY] = true;

queue.Enqueue (new Coord(neighbourX,neighbourY));

accessibleTileCount++;

}

}

}

}

}

}

int targetAccessibleTileCount = (int)mapSize.x * mapSize.y - currentObstacleCount;

return targetAccessibleTileCount == accessibleTileCount;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容