private void GetBestSolution(ref LabyrinthSolution solution, byte[,] pixels, int[] moves, Point currentPosition, Point pTarget) { for (int move = 0; move < MOVE_OFFSET.GetLength(0); move++) { Point futurePosition = GetPositionAfterMove(currentPosition, move); if (futurePosition == pTarget) { solution = new LabyrinthSolution(moves); } else if (CanMoveHere(pixels, futurePosition) && (solution.Moves.Length == 0 || (moves.Length + 1) < solution.Moves.Length)) { Misc.ArrayAppend(ref moves, move); pixels[futurePosition.X, futurePosition.Y] = Labyrinth.COLOR_BLUE; if (displayProgress && DateTime.Now - displayLastUpdate >= displayDelay) { bmpDisplay = GenerateBitmap(pixels); DisplayUpdate(); } GetBestSolution(ref solution, pixels, moves, futurePosition, pTarget); Misc.ArrayRemoveLast(ref moves); pixels[futurePosition.X, futurePosition.Y] = Labyrinth.COLOR_WHITE; } } }
private Bitmap GenerateBitmap(byte[,] pixels, LabyrinthSolution solution = null) { byte[,] outputPixels = pixels; if (solution != null && solution.Moves.Length > 0) { Point position = lab.pStart; for (int iMove = 0; iMove < solution.Moves.Length; iMove++) { position = GetPositionAfterMove(position, solution.Moves[iMove]); outputPixels[position.X, position.Y] = Labyrinth.COLOR_BLUE; } } Bitmap bmpOutput = new Bitmap(outputPixels.GetLength(0), outputPixels.GetLength(1), System.Drawing.Imaging.PixelFormat.Format4bppIndexed); Rectangle rect = new Rectangle(0, 0, bmpOutput.Width, bmpOutput.Height); System.Drawing.Imaging.BitmapData bmpData = bmpOutput.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmpOutput.PixelFormat); IntPtr ptr = bmpData.Scan0; int bytes = Math.Abs(bmpData.Stride) * bmpOutput.Height; System.Runtime.InteropServices.Marshal.Copy(GetBitmapByteArray(outputPixels, bmpData.Stride), 0, ptr, bytes); bmpOutput.UnlockBits(bmpData); return(bmpOutput); }
private void SolveTarget(int targetIndex) { Stopwatch swTarget = new Stopwatch(); swTarget.Restart(); byte[,] pixels = (byte[, ])lab.pixels.Clone(); for (int otherTarget = 0; otherTarget < lab.pTarget.Length; otherTarget++) { if (otherTarget == targetIndex) { continue; } pixels[lab.pTarget[otherTarget].X, lab.pTarget[otherTarget].Y] = Labyrinth.COLOR_BLACK; } LabyrinthSolution bestSolution = new LabyrinthSolution(new int[0]); GetBestSolution(ref bestSolution, pixels, new int[0], lab.pStart, lab.pTarget[targetIndex]); Bitmap bmpSolution = GenerateBitmap(pixels, bestSolution); swTarget.Stop(); bmpSolution.Save(strDirectory + "Target[" + targetIndex.ToString() + "]_Moves[" + bestSolution.Moves.Length.ToString() + "]_Time[" + swTarget.ElapsedMilliseconds.ToString() + "].bmp", System.Drawing.Imaging.ImageFormat.Bmp); bmpDisplay = bmpSolution; }