static void Main(string[] args) { var b1 = Board.ReadInString("000010000007905400000804000001000200470030096000090000016050940004321600308000102"); var board1 = new Board(b1); board1.PrintOut(); Console.Out.WriteLine("going to solve now"); Console.Out.WriteLine("\n\n"); board1.Solve(); board1.IsValid(); //board1.Assign(); Console.Read(); }
static void Main(string[] args) { //var b1 = Board.ReadIn(@"sample.txt"); //var b1 = Board.ReadInString("008450371020097560503800492300705849280030056745609003837006904012970030956043100"); //var b1 = Board.ReadInString("..845.371.2..9756.5.38..4923..7.584928..3..567456.9..3837..69.4.1297..3.956.431.."); var b1 = Board.ReadInString(" 845 371 2 9756 5 38 4923 7 584928 3 567456 9 3837 69 4 1297 3 956 431 "); var board1 = new Board(b1); board1.PrintOut2(); Console.Out.WriteLine("going to solve now"); Console.Out.WriteLine("\n\n"); board1.Solve(); Console.Read(); }
static void Main(string[] args) { Console.WriteLine("Reading file."); string absolutePath = Path.Combine(GetApplicationDirectory(), SudokuFilePath); if (File.Exists(absolutePath)) { string fileContents = File.ReadAllText(absolutePath); Console.WriteLine("Generating board."); Board sudokuBoard = GetBoardFromString(fileContents); Console.WriteLine("Solving the board."); Stopwatch stopwatch = Stopwatch.StartNew(); Board solvedBoard = sudokuBoard.Solve(); stopwatch.Stop(); if (solvedBoard != null) { Console.WriteLine($"Solving succesfull, it took {stopwatch.ElapsedMilliseconds} ms."); Console.Write(solvedBoard.ToString()); } else { Console.WriteLine($"Solving failed, it took {stopwatch.ElapsedMilliseconds} ms."); } } else { Console.WriteLine($"Failed to read file at path \"{absolutePath}\"."); } Console.ReadKey(); }
public void TestPointingLineReduction() { var board = new Board("930050000200630095856002000003180570005020980080005000000800159508210004000560008"); board.Solve(); Assert.AreEqual(true, board.Solved); }
public void TestNakedTriples() { var board = new Board("294513..66..8423193..697254....56....4..8..6....47....73.164..59..735..14..928637"); board.Solve(); Assert.AreEqual(true, board.Solved); }
public void TestNakedQuads() { var board = new Board("000030086000020040090078520371856294900142375400397618200703859039205467700904132"); board.Solve(); Assert.AreEqual(true, board.Solved); }
public void TestNakedPairs() { var board = new Board("4.....938.32.941...953..24.37.6.9..4529..16736.47.3.9.957..83....39..4..24..3.7.9"); board.Solve(); Assert.AreEqual(true, board.Solved); }
public void TestHiddenTriples() { var board = new Board("000001030231090000065003100678924300103050006000136700009360570006019843300000000"); board.Solve(); Assert.AreEqual(true, board.Solved); }
public void TestHiddenSingle() { var board = new Board("200070038000006070300040600008020700100000006007030400004080009060400000910060002"); board.Solve(); Assert.AreEqual(true, board.Solved); }
public void TestHiddenPairs() { var board = new Board("000000000904607000076804100309701080008000300050308702007502610000403208000000000"); board.Solve(); Assert.AreEqual(true, board.Solved); }
public void TestEasiestSudoku() { var board = new Board("...1.5...14....67..8...24...63.7..1.9.......3.1..9.52...72...8..26....35...4.9..."); board.Solve(); Assert.AreEqual(true, board.Solved); }
public void TestBoxLineReduction() { var board = new Board("020943715904000600750000040500480000200000453400352000042000081005004260090208504"); board.Solve(); Assert.AreEqual(true, board.Solved); }
public void TestYWing() { var board = new Board("900240000050690231020050090090700320002935607070002900069020073510079062207086009"); board.Solve(); Assert.AreEqual(true, board.Solved); }
public override void Generate() { xs = new List<int>(); ys = new List<int>(); values = new List<int>(); int lastLookaheadUsed = 0; SolveState result = SolveState.MultipleSolutions; int width = sizex * sizey; int loops = 0; Board workingBoard = null; int possibilityCount = 0; List<List<int>> a = new List<List<int>>(); while (result != SolveState.Solved) { if (result == SolveState.MultipleSolutions) { if (workingBoard != null) { int rndnum = rnd.Next(possibilityCount); int i2 = a[rndnum][0]; int j2 = a[rndnum][1]; int k2 = a[rndnum][2]; bool hasOpp = width % 2 == 0 || (i2 != width / 2 || j2 != width / 2); xs.Add(i2); ys.Add(j2); values.Add(k2 + 1); if (hasOpp) { int rnd2 = rnd.Next(a[rndnum].Count - 3) + 3; int l2 = a[rndnum][rnd2]; xs.Add(width - 1 - i2); ys.Add(width - 1 - j2); values.Add(l2 + 1); } } else { int x = rnd.Next(width); int y = rnd.Next(width); int v = rnd.Next(width) + 1; xs.Add(x); ys.Add(y); values.Add(v); bool hasOpp = width % 2 == 0 || (x != width / 2 || y != width / 2); if (hasOpp) { xs.Add(width - 1 - x); ys.Add(width - 1 - y); values.Add(rnd.Next(width) + 1); } } Board board = new Board(sizey, sizex); // This line used to generate harder games. board.MaxLookahead = maxLookahead; board.Apply(xs, ys, values); result = board.Solve(); if (result == SolveState.MultipleSolutions) { workingBoard = board; a = new List<List<int>>(); possibilityCount = 0; for (int i = 0; i < width; i++) { for (int j = 0; j < width; j++) { if (workingBoard.Get(i, j) == 0) { if (workingBoard.Get(width - 1 - i, width - 1 - j) == 0) { for (int k = 0; k < width; k++) { if (workingBoard.CheckPossible(i, j, k + 1)) { List<int> aInner = new List<int>(); aInner.Add(i); aInner.Add(j); aInner.Add(k); for (int l = 0; l < width; l++) { if (workingBoard.CheckPossible(width - 1 - i, width - 1 - j, l + 1)) { aInner.Add(l); } } if (aInner.Count >= 4) { a.Add(aInner); possibilityCount++; } } } } } } } if (possibilityCount == 0) { a = new List<List<int>>(); possibilityCount = 0; for (int i = 0; i < width; i++) { for (int j = 0; j < width; j++) { if (workingBoard.Get(i, j) == 0) { for (int k = 0; k < width; k++) { if (workingBoard.CheckPossible(i, j, k + 1)) { List<int> aInner = new List<int>(); aInner.Add(i); aInner.Add(j); aInner.Add(k); for (int l = 0; l < width; l++) { if (workingBoard.CheckPossible(width - 1 - i, width - 1 - j, l + 1)) { aInner.Add(l); } } if (aInner.Count >= 4) { a.Add(aInner); possibilityCount++; } } } } } } if (possibilityCount == 0) { // Deadend, try again. Faster then walking backwards? Generate(); return; } } } lastLookaheadUsed = board.LastLookaheadUsed; } else if (result == SolveState.Unsolvable) { if (width % 2 == 0 || (xs[xs.Count - 1] != width / 2 || ys[ys.Count - 1] != width / 2)) { xs.RemoveAt(xs.Count - 1); ys.RemoveAt(ys.Count - 1); values.RemoveAt(values.Count - 1); } xs.RemoveAt(xs.Count - 1); ys.RemoveAt(ys.Count - 1); values.RemoveAt(values.Count - 1); result = SolveState.MultipleSolutions; } loops++; // if we go for a long time, try again. if (loops > width * width * width) { Generate(); return; } } if (ensureDiff && lastLookaheadUsed != maxLookahead) { Generate(); return; } }
static void Main(string[] args) { var board = new Board("016007803090800000870001260048000300650009082039000650060900020080002936924600510"); board.Solve(); Console.ReadLine(); }
public override void Generate() { xs = new List<int>(); ys = new List<int>(); values = new List<int>(); int width = sizex * sizey; int lastLookaheadUsed = 0; int loops = 0; SudokuDancingLinks dl = new SudokuDancingLinks(sizey, sizex); while (true) { int x = 0; int y = 0; bool match = true; while (match) { x = rnd.Next(width); y = rnd.Next(width); match = false; for (int i = 0; i < xs.Count; i++) { if (xs[i] == x && ys[i] == y) { match = true; break; } } } int v = rnd.Next(width) + 1; xs.Add(x); ys.Add(y); values.Add(v); bool hasOpp = width % 2 == 0 || (x != width / 2 || y != width / 2); if (hasOpp) { xs.Add(width - 1 - x); ys.Add(width - 1 - y); values.Add(rnd.Next(width) + 1); } dl.Clear(); for (int i = 0; i < xs.Count; i++) { dl.Grid[ys[i], xs[i]] = values[i]; } dl.Solve(); if (dl.Count <= 0) { if (width % 2 == 0 || (xs[xs.Count - 1] != width / 2 || ys[ys.Count - 1] != width / 2)) { xs.RemoveAt(xs.Count - 1); ys.RemoveAt(ys.Count - 1); values.RemoveAt(values.Count - 1); } xs.RemoveAt(xs.Count - 1); ys.RemoveAt(ys.Count - 1); values.RemoveAt(values.Count - 1); } else if (dl.Count == 1) { break; } loops++; // if we go for a long time, try again. if (loops > width * width * width) { Generate(); return; } } List<int> xs2 = new List<int>(xs); List<int> ys2 = new List<int>(ys); List<int> values2 = new List<int>(values); xs = new List<int>(); ys = new List<int>(); values = new List<int>(); while (xs2.Count > 0) { int choice = rnd.Next(xs2.Count); int x = xs2[choice]; int y = ys2[choice]; int v = values2[choice]; int x2 = -1; int y2 = -1; int v2 = -1; if (choice > 0 && xs2[choice - 1] == width - 1 - x && ys2[choice - 1] == width - 1 - y) { x2 = width - 1 - x; y2 = width - 1 - y; v2 = values2[choice - 1]; xs2.RemoveAt(choice - 1); xs2.RemoveAt(choice - 1); ys2.RemoveAt(choice - 1); ys2.RemoveAt(choice - 1); values2.RemoveAt(choice - 1); values2.RemoveAt(choice - 1); } else if (choice < xs2.Count - 1 && xs2[choice + 1] == width - 1 - x && ys2[choice + 1] == width - 1 - y) { x2 = width - 1 - x; y2 = width - 1 - y; v2 = values2[choice + 1]; xs2.RemoveAt(choice); xs2.RemoveAt(choice); ys2.RemoveAt(choice); ys2.RemoveAt(choice); values2.RemoveAt(choice); values2.RemoveAt(choice); } else { xs2.RemoveAt(choice); ys2.RemoveAt(choice); values2.RemoveAt(choice); } dl.Clear(); for (int i = 0; i < xs.Count; i++) { dl.Grid[ys[i], xs[i]] = values[i]; } for (int i = 0; i < xs2.Count; i++) { dl.Grid[ys2[i], xs2[i]] = values2[i]; } dl.Solve(); if (dl.Count != 1) { xs.Add(x); ys.Add(y); values.Add(v); if (x2 != -1) { xs.Add(x2); ys.Add(y2); values.Add(v2); } } } if (ensureDiff) { Board b = new Board(sizey, sizex); b.MaxLookahead = maxLookahead; b.Apply(xs, ys, values); SolveState result = b.Solve(); if (result != SolveState.Solved) { Generate(); return; } lastLookaheadUsed = b.LastLookaheadUsed; if (lastLookaheadUsed != maxLookahead) { Generate(); return; } } }