알고리즘 | 항상 짧은길 | 이동 비용 | 다수의 시작점과 끝점 | 속도 |
---|---|---|---|---|
BFS | O | X | O | 중간 |
Dijkstra | O | O | O | 느림 |
a* | X | O | O | 빠름 |
탬색 원리
코드 정리
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Node
{
public Vector2Int coordinates;
public bool isWalkable;
public bool isExplored;
public bool isPath;
public Node(Vector2Int coordinates, bool isWalkable)
{
this.coordinates = coordinates;
this.isWalkable = isWalkable;
}
}
void CreateGrid()
{
for(int x=0;x<gridSize.x;x++)
{
for (int y=0;y<gridSize.y;y++)
{
Vector2Int coordinates = new Vector2Int(x,y);
grid.Add(coordinates, new Node(coordinates,true));
}
}
}
void ExploreNeighbors()
{
List<Node> neighbors = new List<Node>();
foreach(Vector2Int direction in directions)
{
Vector2Int neighborCoordinates = currentSearchNode.coordinates + direction;
if (grid.ContainsKey(neighborCoordinates))
{
neighbors.Add(grid[neighborCoordinates]);
}
}
}