//--------------------------------------------------------------------------------------- //그래프를 이용한 길찾기 - BFS(너비 우선 탐색) 방식을 이용하여 길찾기 public void MakeBFSLogic(E03_TileType[,] maze) { int mapSize = maze.GetLength(0); bool[,] found = new bool[mapSize, mapSize]; Queue <E03_Pos> q = new Queue <E03_Pos>(); int startY = 1; int startX = 1; q.Enqueue(new E03_Pos(startY, startX)); found[startY, startX] = true; E03_Pos[,] from = new E03_Pos[mapSize, mapSize]; from[startY, startX] = new E03_Pos(startY, startX); while (q.Count > 0) { E03_Pos pos = q.Dequeue(); int nowY = pos.Y; int nowX = pos.X; for (int i = 0; i < forwardX.Length; i++) { int nextPosY = nowY + forwardY[i]; int nextPosX = nowX + forwardX[i]; if (nextPosY < 0 || nextPosY >= mapSize || nextPosX < 0 || nextPosX >= mapSize) { continue; } if (found[nextPosY, nextPosX]) { continue; } if (maze[nextPosY, nextPosX] == E03_TileType.Wall) { continue; } q.Enqueue(new E03_Pos(nextPosY, nextPosX)); found[nextPosY, nextPosX] = true; from[nextPosY, nextPosX] = new E03_Pos(nowY, nowX); } } BFSRoad = MakeRoad(from, mapSize); }
List <E03_Pos> MakeRoad(E03_Pos[,] from, int mapSize) { int goalY = mapSize - 2; int goalX = mapSize - 2; List <E03_Pos> road = new List <E03_Pos>(); E03_Pos nowPos = new E03_Pos(goalY, goalX); while (true) { road.Add(nowPos); if (nowPos == from[nowPos.Y, nowPos.X]) { break; } nowPos = from[nowPos.Y, nowPos.X]; } road.Reverse(); return(road); }
//--------------------------------------------------------------------------------------- public void MakeAStarLogic2(E03_TileType[,] maze) { int mapSize = maze.GetLength(0); int goalY = mapSize - 2; int goalX = mapSize - 2; bool[,] closed = new bool[mapSize, mapSize]; int[,] open = new int[mapSize, mapSize]; for (int y = 0; y < mapSize; y++) { for (int x = 0; x < mapSize; x++) { open[y, x] = int.MaxValue; } } E03_Pos[,] from = new E03_Pos[mapSize, mapSize]; from[PosY, PosX] = new E03_Pos(PosY, PosX); D05_PriorityQueue <E03_PQNode> pq = new D05_PriorityQueue <E03_PQNode>(); open[PosY, PosX] = 10 * (Mathf.Abs(goalY - PosY) + Mathf.Abs(goalX - PosX)); pq.Push(new E03_PQNode() { F = open[PosY, PosX], G = 0, X = PosX, Y = PosY }); while (pq.Count > 0) { E03_PQNode node = pq.Pop(); int nowY = node.Y; int nowX = node.X; if (closed[nowY, nowX]) { continue; } if (nowY == goalY && nowX == goalX) { break; } for (int i = 0; i < forwardX2.Length; i++) { int nextY = nowY + forwardY2[i]; int nextX = nowX + forwardX2[i]; if (nextX < 0 || nextY < 0 || nextX >= mapSize || nextY >= mapSize) { continue; } if (maze[nextY, nextX] == E03_TileType.Wall) { continue; } if (closed[nextY, nextX]) { continue; } int g = node.G + moveCost2[i]; int h = 10 * (Mathf.Abs(goalY - nextY) + Mathf.Abs(goalX - nextX)); if (open[nextY, nextX] < g + h) { continue; } open[nextY, nextX] = g + h; pq.Push(new E03_PQNode() { F = g + h, G = g, X = nextX, Y = nextY }); from[nextY, nextX] = new E03_Pos(nowY, nowX); } } AStarRoad = MakeRoad(from, mapSize); }
//--------------------------------------------------------------------------------------- public void MakeAStarLogic(E03_TileType[,] maze) { int mapSize = maze.GetLength(0); int goalY = mapSize - 2; int goalX = mapSize - 2; int startY = PosY; int startX = PosX; bool[,] closed = new bool[mapSize, mapSize]; int[,] open = new int[mapSize, mapSize]; E03_Pos[,] from = new E03_Pos[mapSize, mapSize]; from[startY, startX] = new E03_Pos(startY, startX); for (int y = 0; y < open.GetLength(0); y++) { for (int x = 0; x < open.GetLength(1); x++) { open[y, x] = int.MaxValue; } } D05_PriorityQueue <E03_PQNode> pq = new D05_PriorityQueue <E03_PQNode>(); open[startY, startY] = Mathf.Abs(goalY - startY) + Math.Abs(goalX - startX) + 0; // 시작점이므로 현재까지 온 거리는 0이다. 사실 더해주지 않아도 무방하다. pq.Push(new E03_PQNode() { F = Mathf.Abs(goalY - startY) + Math.Abs(goalX - startX), G = 0, X = startX, Y = startY }); while (pq.Count > 0) { E03_PQNode node = pq.Pop(); if (closed[node.Y, node.X]) { continue; } closed[node.Y, node.X] = true; int nowY = node.Y; int nowX = node.X; if (nowY == goalY && nowX == goalX) { break; } for (int i = 0; i < forwardX.Length; i++) { int nextY = nowY + forwardY[i]; int nextX = nowX + forwardX[i]; if (nextX < 0 || nextY < 0 || nextX >= mapSize || nextY >= mapSize) { continue; } if (closed[nextY, nextX]) { continue; } if (maze[nextY, nextX] == E03_TileType.Wall) { continue; } int g = node.G + moveCost[i]; int h = Mathf.Abs(goalY - nowY) + Math.Abs(goalX - nowX); int distance = g + h; if (open[nextY, nextX] < distance) { continue; } pq.Push(new E03_PQNode() { F = distance, G = g, X = nextX, Y = nextY }); open[nextY, nextX] = distance; from[nextY, nextX] = new E03_Pos(nowY, nowX); } } AStarRoad = MakeRoad(from, mapSize); }