void AddState(KnightSpearmanState state) { if (!stack.Contains(state)) { stack.Push(state); } }
public LinkedList <KnightSpearmanState> Solve(KnightSpearmanState initial) { closed = new List <KnightSpearmanState>(); AddState(initial); while (HasElements()) { KnightSpearmanState state = NextState(); if (state.IsSolution()) { return(FindPath(state)); } closed.Add(state); LinkedList <KnightSpearmanState> moves = state.GetPossibleMoves(); foreach (KnightSpearmanState move in moves) { if (!closed.Contains(move)) { AddState(move); } } } return(null); }
static void Main(string[] args) { KnightSpearmanState knightsSpearman = new KnightSpearmanState(); LinkedList <KnightSpearmanState> moves = null; DFS solver = new DFS(); moves = solver.Solve(knightsSpearman); int n = 1; foreach (KnightSpearmanState state in moves) { KnightSpearmanState nextState = (KnightSpearmanState)state; Console.WriteLine(n.ToString() + " "); if (nextState.IsSolution()) { Console.WriteLine("Solution"); } Console.WriteLine(nextState.ToString()); n++; } Console.ReadKey(); }
private LinkedList <KnightSpearmanState> FindPath(KnightSpearmanState solution) { LinkedList <KnightSpearmanState> path = new LinkedList <KnightSpearmanState>(); while (solution != null) { path.AddFirst(solution); solution = solution.Parent; } return(path); }
public KnightSpearmanState(KnightSpearmanState parent, Side knight1, Side spearman1, Side knight2, Side spearman2, Side knight3, Side spearman3) { this.Parent = parent; this.knight1 = knight1; this.spearman1 = spearman1; this.knight2 = knight2; this.spearman2 = spearman2; this.knight3 = knight3; this.spearman3 = spearman3; }
public override bool Equals(object obj) { if (obj == null || !(obj is KnightSpearmanState)) { return(false); } KnightSpearmanState knightsSpearman = (KnightSpearmanState)obj; return (knight1.KeepSide == knightsSpearman.knight1.KeepSide && spearman1.KeepSide == knightsSpearman.spearman1.KeepSide && knight2.KeepSide == knightsSpearman.knight2.KeepSide && spearman2.KeepSide == knightsSpearman.spearman2.KeepSide && knight3.KeepSide == knightsSpearman.knight3.KeepSide && spearman3.KeepSide == knightsSpearman.spearman3.KeepSide); }
public KnightSpearmanState(KnightSpearmanState parent) { this.parent = parent; }