public bool solver(Jolka jolka) { List <string> poss = new List <string>(); if (jolka.notusedZ.Count == 0 && jolka.usedZ.Count == jolka.words.Count && jolka.checkAllRestrFC()) { count_results++; if (!second) { Console.WriteLine(count_returns); Console.WriteLine(count_visited); Console.WriteLine(watch.ElapsedMilliseconds); watch.Stop(); //jolka.showGrid(); } second = !second; //return true; } jolka.addToPossible(); if (!(jolka.notusedZ.Count == 0)) { jolka.notusedZ[0].nextValue(); foreach (string x in jolka.notusedZ[0].possiblevalues) { poss.Add(x); } } for (int i = 0; i < poss.Count; i++) { count_visited++; jolka.notusedZ[0].value = poss[i]; jolka.usedZ.Add(jolka.notusedZ[0]); jolka.notusedZ.RemoveAt(0); if (!jolka.ifused(jolka.usedZ[jolka.usedZ.Count - 1].value) && jolka.checkAllRestr()) { jolka.used.Add(jolka.usedZ[jolka.usedZ.Count - 1].value); if (solver(jolka)) { return(true); } else { jolka.used.RemoveAt(jolka.used.Count - 1); if (jolka.usedZ.Count > 0) { jolka.usedZ[jolka.usedZ.Count - 1].value = ""; jolka.notusedZ.Insert(0, jolka.usedZ[jolka.usedZ.Count - 1]); jolka.usedZ.RemoveAt(jolka.usedZ.Count - 1); } count_returns++; } } else { if (jolka.usedZ.Count > 0) { jolka.usedZ[jolka.usedZ.Count - 1].value = ""; jolka.notusedZ.Insert(0, jolka.usedZ[jolka.usedZ.Count - 1]); jolka.usedZ.RemoveAt(jolka.usedZ.Count - 1); } count_returns++; } } return(false); }
public bool solver(Jolka jolka) { int next_zmienna = 0; for (int i = 0; i < jolka.zmienne.Count; i++) { if (jolka.zmienne[i].value.Equals("")) { next_zmienna = i; break; } } if (jolka.used.Count == jolka.words.Count && jolka.checkAllRestr()) { count_results++; if (!second) { Console.WriteLine(count_returns); Console.WriteLine(count_visited); Console.WriteLine(watch.ElapsedMilliseconds); watch.Stop(); //jolka.showGrid(); } second = !second; //return true; } jolka.zmienne[next_zmienna].nextValueBT(); for (int i = 0; i < jolka.zmienne[next_zmienna].values.Count; i++) { /* if (second && jolka.used.Count==0 ) * { * // Console.Write("Used: "); * foreach (string s in jolka.used) * { * Console.Write(s + ", "); * } * }*/ count_visited++; jolka.zmienne[next_zmienna].value = jolka.zmienne[next_zmienna].values[i]; if (!jolka.used.Contains(jolka.zmienne[next_zmienna].value) && jolka.checkAllRestr()) { jolka.used.Add(jolka.zmienne[next_zmienna].values[i]); if (solver(jolka)) { return(true); } else { jolka.used.RemoveAt(jolka.used.Count - 1); jolka.zmienne[next_zmienna].value = ""; count_returns++; } } else { jolka.zmienne[next_zmienna].value = ""; // Console.WriteLine("Ima here"); count_returns++; } } return(false); }
static void Main(string[] args) { /* Console.WriteLine("Sudoku"); * List<Sudoku> puzzles = readSudoku(@"E:\Semestr 6\SI\ai-lab2-2020-dane\Sudoku.csv"); * //Sudoku s = new Sudoku(); * // s.puzzle=new List<int>{2,9,5,7,4,3,8,6,1,4,3,1,8,6,5,9,0,0,8,7,6,1,9,2,5,4,3,3,8,7,4,5,9,2,1,6,6,1,2,3,8,7,4,9,5,5,4,9,2,1,6,7,3,8,7,6,3,5,2,4,1,8,9,9,2,8,6,7,1,3,5,4,1,5,4,9,3,8,6,0,0 }; * // s.makeGrid(); * SudokuSolver solver = new SudokuSolver(); * Stopwatch watch2 = Stopwatch.StartNew(); * solver.solveSudoku(puzzles[42]); // w celu zmiany wykonywanego sudoku, należy wpisać wybrany numer zagadki * Console.WriteLine("Czas całkowity sudoku: " + watch2.ElapsedMilliseconds); * //solver.solveSudoku(s); // sprawdzenie znajdowania dwóch rozwiązań * // s.showGrids(); * Console.WriteLine("Znalezione rozwiązania: "+solver.count_results); * Console.WriteLine("Wejść łącznie: "+solver.count_returns); * Console.WriteLine("Pwrotów łącznie: "+solver.count_visited);*/ /* foreach (Sudoku z in solver.result) * { * z.showGrids(); * }*/ //etap sudoku /*Console.WriteLine(); * Console.WriteLine(); * Console.WriteLine(); * Console.WriteLine("Jolki"); */ List <string> crosses = readCrossword(@"E:\Semestr 6\SI\ai-lab2-2020-dane\Jolka\puzzle4"); // do wyboru zagadki: należy zmienić nazwę pliku w obu linijkach List <string> words = readCrossword(@"E:\Semestr 6\SI\ai-lab2-2020-dane\Jolka\words4"); for (int i = 0; i < 10; i++) { Jolka j = new Jolka(words, crosses); JolkaSolver solverJ = new JolkaSolver(); Stopwatch watch = Stopwatch.StartNew(); solverJ.solver(j); Console.WriteLine(solverJ.count_returns); Console.WriteLine(solverJ.count_visited); Console.WriteLine(watch.ElapsedMilliseconds); watch.Stop(); Console.WriteLine(); } /* Console.WriteLine("Jolki2"); */ for (int i = 0; i < 10; i++) { Jolka z = new Jolka(words, crosses); JolkaFC solverF = new JolkaFC(); Stopwatch watch3 = Stopwatch.StartNew(); solverF.solver(z); //Console.WriteLine("Znalezione rozwiązania: " + solverF.count_results); Console.WriteLine(solverF.count_returns); Console.WriteLine(solverF.count_visited); Console.WriteLine(watch3.ElapsedMilliseconds); watch3.Stop(); Console.WriteLine(); } //j.showGrid(); }