public static string[] Solve(int food_limit, int[] spoon_limits, ref int countStep) { Queue<Move> nextQueue = new Queue<Move>(); Hashtable used = new Hashtable(); Move finish = null; int[] spoons = new int[spoon_limits.Length]; for (int i = 0; i < spoons.Length; i++) spoons[i] = 0; Move firstMove = new Move(0, spoons, food_limit, spoon_limits, "", null); nextQueue.Enqueue(firstMove); used[firstMove.signature] = true; while (true) { if (nextQueue.Count == 0) break; countStep++; Move m = (Move)(nextQueue.Dequeue()); if (m.IsFinished) { finish = m; break; } Move[] newMoves = m.GenNextMoves(); for (int i = 0; i < newMoves.Length; i++) { if (newMoves[i] == null) continue; if (newMoves[i].IsFinished) { finish = newMoves[i]; break; } if (used[newMoves[i].signature] != null) continue; used[newMoves[i].signature] = true; nextQueue.Enqueue(newMoves[i]); } if (finish != null) break; } if (finish == null) return null; List<string> steps = new List<string>(); while (finish != null) { //Console.WriteLine(finish.ToString()); steps.Insert(0, finish.move); finish = finish.parent; } steps.RemoveAt(0); return steps.ToArray(); }
public Move(int food,int[] spoons,int food_limit,int[] spoon_limits,string move,Move parent) { if (parent == null) level = 0; else level = parent.level + 1; this.food = food; this.spoons = spoons; this.food_limit = food_limit; this.spoon_limits = spoon_limits; this.move = move; this.parent = parent; IsFinished = (food == food_limit)?true:false; for (int i = 0; i < spoons.Length;i++ ) { if (spoons[i] > 0) IsFinished = false; } signature = GetSignature(); score = -level; }