You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MRVNextValueChooser.cs 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Collections.Generic;
  2. using uqac_ia_sudoku_csp.Interfaces;
  3. namespace uqac_ia_sudoku_csp.Solver.NextValueChoosers
  4. {
  5. public class MRVNextValueChooser : INextValueChooser
  6. {
  7. public void SelectVariable(Board board, out int x, out int y, IList<IConstraint> constraints)
  8. {
  9. var avail = 1;
  10. while (avail < board.Size)
  11. {
  12. for (var xx = 0; xx < board.Size; ++xx)
  13. {
  14. for (var yy = 0; yy < board.Size; ++yy)
  15. {
  16. if (board.GetNumber(xx, yy) == null)
  17. {
  18. var remaining = GetRemainingValue(board, xx, yy, constraints);
  19. if (remaining == avail)
  20. {
  21. x = xx;
  22. y = yy;
  23. return;
  24. }
  25. }
  26. }
  27. }
  28. ++avail;
  29. }
  30. x = -1;
  31. y = -1;
  32. }
  33. protected int GetRemainingValue(Board board, int x, int y, IList<IConstraint> constraints)
  34. {
  35. var c = 0;
  36. for (var i = 0; i < board.Size; ++i)
  37. {
  38. board.SetNumber(x, y, i);
  39. if (board.IsConsistent(x, y, constraints))
  40. {
  41. ++c;
  42. }
  43. }
  44. board.ClearNumber(x, y);
  45. return c;
  46. }
  47. }
  48. }