public static void start(string sudokuPattern) { Stopwatch stopWatch_search = new Stopwatch(); Stopwatch stopWatch_print = new Stopwatch(); SudokuState startState = new SudokuState(sudokuPattern); SudokuSearch searcher = new SudokuSearch(startState); stopWatch_search.Start(); searcher.DoSearch(); stopWatch_search.Stop(); TimeSpan t_search = stopWatch_search.Elapsed; IState state = searcher.Solutions[0]; List <SudokuState> solutionPath = new List <SudokuState>(); while (state != null) { solutionPath.Add((SudokuState)state); state = state.Parent; } solutionPath.Reverse(); int[,] table_tmp1 = new int[9, 9]; int[,] table_tmp2 = new int[9, 9]; table_tmp1 = solutionPath[0].Table; stopWatch_print.Start(); foreach (SudokuState s in solutionPath) { table_tmp2 = table_tmp1; table_tmp1 = s.Table; s.Print(table_tmp2, table_tmp1); } stopWatch_print.Stop(); TimeSpan t_print = stopWatch_print.Elapsed; string SearchTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", t_search.Hours, t_search.Minutes, t_search.Seconds, t_search.Milliseconds / 10); Console.WriteLine("Czas przeszukiwania " + SearchTime); string PrintTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", t_print.Hours, t_print.Minutes, t_print.Seconds, t_print.Milliseconds / 10); Console.WriteLine("Czas wyswietlania " + PrintTime); }
static void Main(string[] args) { //string sudokuPattern = "000079065000003002005060093340050106000000000608020059950010600700600000820390000"; string sudokuPattern = "002008050000040070480072000008000031600080005570000600000960048090020000030800900"; // String sudokuPattern = "219685743400000001800000005600000007547218936900000004100000008700000009386549172"; SudokuState startState = new SudokuState(sudokuPattern); startState.Print(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); SudokuSearch searcher = new SudokuSearch(startState); searcher.DoSearch(); IState state = searcher.Solutions [0]; List <SudokuState> solutionPath = new List <SudokuState>(); while (state != null) { solutionPath.Add(( SudokuState )state); state = state.Parent; } stopwatch.Stop(); solutionPath.Reverse(); foreach (SudokuState s in solutionPath) { Console.Clear(); s.Print(); Console.ReadKey(); } Console.WriteLine(); Console.WriteLine("Czas obliczen: {0}", stopwatch.Elapsed); Console.ReadKey(); }
static void sudoku(bool UseAdvancedSearch) { string[] heuristicType = { "Simple", "Advanced" }; string[] hash = { "800030000930007000071520900005010620000050000046080300009076850060100032000040006", "000000600600700001401005700005900000072140000000000080326000010000006842040002930", "457682193600000007100000004200000006584716239300000008800000002700000005926835471", "000012034000056017000000000000000000480000051270000048000000000350061000760035000", "000700800000040030000009001600500000010030040005001007500200600030080090007000002", "100040002050000090008000300000509000700080003000706000007000500090000040600020001", "600040003010000070005000800000502000300090002000103000008000900070000050200030004", "000000000000003085001020000000507000004000100090000000500000073002010000000040009", "000040700080000000010000020000800006700000050400000200302070000000000000000006018" }; string SudokuPattern; int m = getInt("Choice Sudoku: "); SudokuPattern = hash[(m > hash.Length ? hash.Length - 1 : m)]; SudokuState startState = new SudokuState(SudokuPattern); SudokuSearch searcher = new SudokuSearch(startState, UseAdvancedSearch); searcher.DoSearch(); IState state = searcher.Solutions[0]; List <SudokuState> solutionPath = new List <SudokuState>(); while (state != null) { solutionPath.Add((SudokuState)state); state = state.Parent; } solutionPath.Reverse(); foreach (SudokuState s in solutionPath) { s.Print(); } string data = string.Format("{0} heuristic: \nSteps: {1}, \ncreated {2} board objects", heuristicType[UseAdvancedSearch ? 0 : 1], solutionPath.Count, SudokuState.counter); Console.Write(data); Console.Write("\nOpen: " + searcher.Open.Count); Console.Write("\nClose: " + searcher.Closed.Count); Console.Write("\n\nPress key to menu -> "); // Cleaning SudokuState.counter = 0; SudokuState.slowdown = false; }
static void Main(string[] args) { SudokuState theState = new SudokuState("800030000930007000071520900005010620000050000046080300009076850060100032000040006"); SudokuSearch theSearcher = new SudokuSearch(theState); // tworzymy szukacz na podstawie pierwszego stanu theSearcher.DoSearch(); // szukacz wykonuje algorytm A* znajduje droge od pierwszego stanu do rozwiazania List <SudokuState> states = new List <SudokuState>(); // tworzymy liste stanow SudokuState theResult = (SudokuState)theSearcher.Solutions[0]; // do zmiennej theResult przypisujemy stan z rozwiazaniem znalezionym przez szukacz while (theResult != null) // do kolejnych elementow listy states wpisujemy kolejne stany po drodze od stanu poczatkowego do rozwiazania { states.Add(theResult); theResult = (SudokuState)theResult.Parent; } for (int i = states.Count() - 1; i > 0; --i) // od tylu wypisujemy stany; od poczatku do rozwiazania { states.ElementAt(i).Write(); Console.WriteLine("Jeszcze " + i + " stanow do rozwiazania"); Thread.Sleep(300); Console.Clear(); // czyscimy konsole } states.ElementAt(0).Write(); // wypisujemy ostateczne rozwiazanie na sam koniec }
static void Main(string[] args) { bool puzzle = true; bool sudoku = false; if (sudoku) { List <string> sudokuPatterns = new List <string>(); sudokuPatterns.Add("800030000930007000071520900005010620000050000046080300009076850060100032000040006"); sudokuPatterns.Add("000000600600700001401005700005900000072140000000000080326000010000006842040002930"); sudokuPatterns.Add("457682193600000007100000004200000006584716239300000008800000002700000005926835471"); //sudokuPatterns.Add("000012034000056017000000000000000000480000051270000048000000000350061000760035000"); //not enough memory //sudokuPatterns.Add("000700800000040030000009001600500000010030040005001007500200600030080090007000002"); //not enough memory //sudokuPatterns.Add("100040002050000090008000300000509000700080003000706000007000500090000040600020001"); //not enough memory foreach (string pattern in sudokuPatterns) { //Sprawdzenie ilości pustych pól int emptyFieldsCount = 0; for (int i = 0; i < pattern.Length; ++i) { if (pattern[i] == '0') { emptyFieldsCount++; } } SudokuState startState = new SudokuState(pattern); //Stan startowy SudokuSearch searcher = new SudokuSearch(startState); System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); searcher.DoSearch(); watch.Stop(); IState state = searcher.Solutions[0]; List <SudokuState> solutionPath = new List <SudokuState>(); while (state != null) { solutionPath.Add((SudokuState)state); state = state.Parent; } solutionPath.Reverse(); foreach (SudokuState s in solutionPath) { s.Print(); Console.WriteLine(); } Console.WriteLine("Empty fields count: {0}", emptyFieldsCount); Console.WriteLine("States count in Open: {0}", searcher.Open.Count); Console.WriteLine("States count in Closed: {0}", searcher.Closed.Count); Console.WriteLine("Time: {0}s", watch.ElapsedMilliseconds / 1000.0); Console.ReadKey(); } } if (puzzle) { const int TESTS_NUMBER = 100; Console.WriteLine("Creating {0} puzzles.", TESTS_NUMBER); PuzzleState[] startStatesMisplaced = new PuzzleState[TESTS_NUMBER]; PuzzleState[] startStatesManhattan = new PuzzleState[TESTS_NUMBER]; Random random = new Random(); for (int i = 0; i < TESTS_NUMBER; ++i) //Tworzenie 100 stanów startowych puzzli { startStatesMisplaced[i] = new PuzzleState(random, true); startStatesManhattan[i] = new PuzzleState(startStatesMisplaced[i], false); } double misplacedTilesTimeSum = 0.0; double manhattanTimeSum = 0.0; long misplacedTilesOpenSum = 0; long misplacedTilesClosedSum = 0; long manhattanOpenSum = 0; long manhattanClosedSum = 0; double misplacedTilesPathLength = 0.0; double manhattanPathLength = 0.0; Console.WriteLine("Attempting to solve the puzzles using Misplaced Tiles heuristic:"); for (int i = 0; i < TESTS_NUMBER; ++i) { Console.WriteLine("Puzzle no. " + i); PuzzleSearch searcher = new PuzzleSearch(startStatesMisplaced[i]); searcher.DoSearch(); IState state = searcher.Solutions[0]; List <PuzzleState> solutionPath = new List <PuzzleState>(); while (state != null) { solutionPath.Add((PuzzleState)state); state = state.Parent; } solutionPath.Reverse(); foreach (PuzzleState s in solutionPath) { s.Print(); Console.WriteLine(); } System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); searcher.DoSearch(); watch.Stop(); misplacedTilesTimeSum += watch.ElapsedMilliseconds; misplacedTilesOpenSum += searcher.Open.Count; misplacedTilesClosedSum += searcher.Closed.Count; misplacedTilesPathLength += searcher.Solutions[0].G; Console.WriteLine("\tNumber of states in Open set: " + searcher.Open.Count); Console.WriteLine("\tNumber of states in Closed set: " + searcher.Closed.Count); Console.WriteLine("\tSolution path length: " + searcher.Solutions[0].G); Console.WriteLine("\tElapsed time: {0}s.", watch.ElapsedMilliseconds / 1000.0); } Console.WriteLine(); Console.WriteLine("Attempting to solve the puzzles using Manhattan heuristic: "); for (int i = 0; i < TESTS_NUMBER; ++i) { Console.WriteLine("Puzzle no. {0}.", i + 1); PuzzleSearch searcher = new PuzzleSearch(startStatesManhattan[i]); System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); searcher.DoSearch(); watch.Stop(); manhattanTimeSum += watch.ElapsedMilliseconds; manhattanOpenSum += searcher.Open.Count; manhattanClosedSum += searcher.Closed.Count; manhattanPathLength += searcher.Solutions[0].G; Console.WriteLine("\tNumber of states in Open set: " + searcher.Open.Count); Console.WriteLine("\tNumber of states in Closed set: " + searcher.Closed.Count); Console.WriteLine("\tSolution path length: " + searcher.Solutions[0].G); Console.WriteLine("\tElapsed time: {0}s.", watch.ElapsedMilliseconds / 1000.0); } Console.WriteLine(); Console.WriteLine("Misplaced Tiles heuristic: "); Console.WriteLine("\tAverage time: {0}s.", misplacedTilesTimeSum / 1000.0 / TESTS_NUMBER); Console.WriteLine("\tAverage Open states number: " + misplacedTilesOpenSum / TESTS_NUMBER); Console.WriteLine("\tAverage Closed states number: " + misplacedTilesClosedSum / TESTS_NUMBER); Console.WriteLine("\tAverage solution path length: " + misplacedTilesPathLength / TESTS_NUMBER); Console.WriteLine(); Console.WriteLine("Manhattan heuristic: "); Console.WriteLine("\tAverage time: {0}s.", manhattanTimeSum / 1000.0 / TESTS_NUMBER); Console.WriteLine("\tAverage Open states number: " + manhattanOpenSum / TESTS_NUMBER); Console.WriteLine("\tAverage Closed states number: " + manhattanClosedSum / TESTS_NUMBER); Console.WriteLine("\tAverage solution path length: " + manhattanPathLength / TESTS_NUMBER); Console.ReadKey(); } }