private static Move Calculate44Border(int[] pieces, ref int countStep) { Move borderMove = null; BinaryHeap nextQueue = new BinaryHeap(); Hashtable used = new Hashtable(); Move firstMove = new Move(pieces, -1, null, 4, 4); nextQueue.Insert(firstMove.borderScore - firstMove.level / FACTOR, firstMove); used[firstMove.borderSignature] = true; while (true) { if (nextQueue.Count == 0) break; countStep++; //Console.WriteLine(countStep); Move m = (Move)(nextQueue.Remove()); if (m.IsBorderFinished || FoundBorderSolution(m)) { borderMove = m; break; } Move[] newMoves = new Move[] { m.DownMove(), m.UpMove(), m.RightMove(), m.LeftMove() }; for (int i = 0; i < newMoves.Length; i++) { if (newMoves[i] == null) continue; if (newMoves[i].IsBorderFinished || FoundBorderSolution(newMoves[i])) { borderMove = newMoves[i]; break; } if (used[newMoves[i].borderSignature] != null) continue; //if (m.borderScore > (newMoves[i].borderScore+1)) continue; used[newMoves[i].borderSignature] = true; nextQueue.Insert(newMoves[i].borderScore - newMoves[i].level / FACTOR, newMoves[i]); //Console.WriteLine(newMoves[i].ToBorderString()); //Console.WriteLine(newMoves[i].borderScore); //if (newMoves[i].IsInvalid) //Console.ReadKey(); } if (borderMove != null) break; } if (!borderMove.IsBorderFinished) { borderMove = GetBorderSolution(borderMove); } //Console.WriteLine("4x4 border = " + countStep); conn.Clone(); return borderMove; }
private static int[] Calculate(int dx, int dy, int[] pieces, ref int countStep) { Queue<Move> nextQueue = new Queue<Move>(); Hashtable used = new Hashtable(); Move finish = null; Move firstMove = new Move(pieces, -1, null, dx, dy); nextQueue.Enqueue(firstMove); //if ((score % 2) > 0) return null; used[firstMove.signature] = true; while (true) { if (nextQueue.Count == 0) break; countStep++; Move m = nextQueue.Dequeue(); if (m.IsFinished || Found33Solution(m)) { finish = m; break; } Move[] newMoves = new Move[] {m.DownMove(),m.UpMove(), m.RightMove(),m.LeftMove()}; for (int i = 0; i < newMoves.Length; i++) { if (newMoves[i] == null) continue; if (newMoves[i].IsFinished || Found33Solution(m)) { finish = newMoves[i]; break; } if (used[newMoves[i].signature] != null) continue; used[newMoves[i].signature] = true; nextQueue.Enqueue(newMoves[i]); //Console.WriteLine(newMoves[i].ToString()); //Console.ReadKey(); } if (finish != null) break; } if (finish == null) return null; if (!finish.IsFinished) { finish = Get33Solution(finish); } List<int> steps = new List<int>(); while (finish != null) { steps.Insert(0,finish.emptyPosition); finish = finish.parent; } steps.RemoveAt(0); return steps.ToArray(); }
public Move(int[] current,int click, Move parent,int dx,int dy) { if (parent == null) level = 0; else level = parent.level + 1; this.dx = dx; this.dy = dy; this.current = current; this.parent = parent; this.emptyPosition = click; if (emptyPosition == -1) { for (int i = 0; i < current.Length; i++) { if (current[i] == (current.Length-1)) { emptyPosition = i; } } } IsFinished = true; IsBorderFinished = true; for (int i = 0; i < current.Length; i++) { if (current[i] != i) { IsFinished = false; } if ((0 <= current[i] && current[i] <= 3) || current[i] == 4 || current[i] == 8 || current[i] == 12) { if (current[i] != i) { IsBorderFinished = false; } } } borderScore = GetBorderScore(); borderSignature = GetBorderSignature(); signature = GetSignature(); }
private static bool Found33Solution(Move m) { DbCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT COUNT(1) FROM tiles33 WHERE pos='" + m.signature + "'"; long num = (long)cmd.ExecuteScalar(); cmd.Dispose(); if (num > 0) return true; else return false; }
private static Move Get33Solution(Move borderMove) { Move finishMove = borderMove; int count = 0; while (true) { if (finishMove.IsBorderFinished) break; DbCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT before_empty FROM tiles33 WHERE pos='" + finishMove.signature + "'"; int click = (int)((long)cmd.ExecuteScalar()); if (click == (finishMove.emptyPosition + 1)) finishMove = finishMove.RightMove(); else if (click == (finishMove.emptyPosition - 1)) finishMove = finishMove.LeftMove(); else if (click == (finishMove.emptyPosition + 3)) finishMove = finishMove.DownMove(); else if (click == (finishMove.emptyPosition - 3)) finishMove = finishMove.UpMove(); cmd.Dispose(); count++; } //Console.WriteLine("Save = " + count + " steps"); return finishMove; }
private static string[] Calculate(int dx, int dy, int[] pieces, ref int countStep) { InitializeDatabase(); BinaryHeap nextQueue = new BinaryHeap(); Hashtable used = new Hashtable(); Move finish = null; Move firstMove = new Move(pieces, "", null, dx, dy); nextQueue.Insert(firstMove.score, firstMove); int clickD = dx - 1; int clickD2 = clickD * clickD; used[firstMove.signature] = true; while (true) { if (nextQueue.Count == 0) break; countStep++; Move m = (Move)(nextQueue.Remove()); if (m.IsFinished || FoundSolution(m,dx)) { finish = m; break; } Move[] newMoves = new Move[clickD2*2]; for (int k=0;k<newMoves.Length;k++) { if ((k%2) == 0) newMoves[k] = m.GenMove(k/2,Move.Direction.CLOCKWISE); else newMoves[k] = m.GenMove(k/2,Move.Direction.COUNTER_CLOCKWISE); } for (int i = 0; i < newMoves.Length; i++) { if (newMoves[i] == null) continue; if (newMoves[i].IsFinished || FoundSolution(m,dx)) { finish = newMoves[i]; break; } if (used[newMoves[i].signature] != null) continue; used[newMoves[i].signature] = true; nextQueue.Insert(newMoves[i].score,newMoves[i]); //Console.WriteLine(newMoves[i].ToString()); //Console.ReadKey(); } if (finish != null) break; } if (finish == null) return null; if (!finish.IsFinished) { finish = GetSolution(finish,dx); } List<string> steps = new List<string>(); while (finish != null) { steps.Insert(0, finish.move); finish = finish.parent; } steps.RemoveAt(0); return steps.ToArray(); }
public Move(int[] current, string move, Move parent, int dx, int dy) { if (parent == null) level = 0; else level = parent.level + 1; this.dx = dx; this.dy = dy; this.current = current; this.parent = parent; this.move = move; IsFinished = true; for (int i = 0; i < current.Length; i++) { if (current[i] != i) { IsFinished = false; } } signature = GetSignature(); score = GetScore(); }
private static Move GetSolution(Move m,int dimension) { Move finishMove = m; int count = 0; while (true) { if (finishMove.IsFinished) break; DbCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT move FROM rotation"+dimension+" WHERE pos='" + finishMove.signature + "'"; string move = ((string)cmd.ExecuteScalar()); string posString = move.Substring(0, 2); string directionString = move.Substring(2, 1); int p = int.Parse(posString); // reverse it Move.Direction d = Move.Direction.CLOCKWISE; if (directionString == "T") d = Move.Direction.CLOCKWISE; else if (directionString == "C") d = Move.Direction.COUNTER_CLOCKWISE; else throw new Exception("Invalid Direction"); finishMove = finishMove.GenMove(p, d); cmd.Dispose(); count++; } //Console.WriteLine("Save = " + count + " steps"); return finishMove; }