LinkPathFinder.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. namespace DevilLink2.Core
  4. {
  5. public sealed class LinkPathFinder
  6. {
  7. private static readonly int[] Dr = { -1, 1, 0, 0 };
  8. private static readonly int[] Dc = { 0, 0, -1, 1 };
  9. private sealed class Node
  10. {
  11. public readonly GridPosition Position;
  12. public readonly int Direction, Turns;
  13. public readonly Node Parent;
  14. public Node(GridPosition position, int direction, int turns, Node parent)
  15. { Position = position; Direction = direction; Turns = turns; Parent = parent; }
  16. }
  17. /// <summary>Returns endpoints and key corners, including border coordinates.
  18. /// Invalid/empty endpoints return false. The board is never mutated.</summary>
  19. public bool TryFindPath(Board board, GridPosition start, GridPosition target,
  20. out IReadOnlyList<GridPosition> path)
  21. {
  22. if (board == null) throw new ArgumentNullException(nameof(board));
  23. path = Array.Empty<GridPosition>();
  24. if (start == target || !board.Contains(start.Row, start.Column) ||
  25. !board.Contains(target.Row, target.Column)) return false;
  26. var a = board.GetTile(start.Row, start.Column);
  27. var b = board.GetTile(target.Row, target.Column);
  28. if (a == null || b == null || a.Symbol != b.Symbol) return false;
  29. var visited = new bool[board.Rows + 2, board.Columns + 2, 4, 3];
  30. var queue = new Queue<Node>();
  31. var origin = new Node(start, -1, 0, null);
  32. for (int d = 0; d < 4; d++) Enqueue(origin, d, 0, board, target, visited, queue);
  33. while (queue.Count > 0)
  34. {
  35. var current = queue.Dequeue();
  36. if (current.Position == target) { path = BuildPath(current); return true; }
  37. for (int d = 0; d < 4; d++)
  38. {
  39. int turns = current.Turns + (d == current.Direction ? 0 : 1);
  40. if (turns <= 2) Enqueue(current, d, turns, board, target, visited, queue);
  41. }
  42. }
  43. return false;
  44. }
  45. private static void Enqueue(Node parent, int direction, int turns, Board board,
  46. GridPosition target, bool[,,,] visited, Queue<Node> queue)
  47. {
  48. var next = new GridPosition(parent.Position.Row + Dr[direction], parent.Position.Column + Dc[direction]);
  49. if (next.Row < -1 || next.Row > board.Rows || next.Column < -1 || next.Column > board.Columns) return;
  50. if (next != target && board.Contains(next.Row, next.Column) && !board.IsEmpty(next.Row, next.Column)) return;
  51. if (visited[next.Row + 1, next.Column + 1, direction, turns]) return;
  52. visited[next.Row + 1, next.Column + 1, direction, turns] = true;
  53. queue.Enqueue(new Node(next, direction, turns, parent));
  54. }
  55. private static IReadOnlyList<GridPosition> BuildPath(Node end)
  56. {
  57. var full = new List<GridPosition>();
  58. for (var n = end; n != null; n = n.Parent) full.Add(n.Position);
  59. full.Reverse();
  60. var corners = new List<GridPosition> { full[0] };
  61. for (int i = 1; i < full.Count - 1; i++)
  62. {
  63. var a = full[i - 1]; var b = full[i]; var c = full[i + 1];
  64. if ((b.Row - a.Row != c.Row - b.Row) || (b.Column - a.Column != c.Column - b.Column))
  65. corners.Add(b);
  66. }
  67. corners.Add(full[full.Count - 1]);
  68. return corners.AsReadOnly();
  69. }
  70. }
  71. }