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.

DhNextCellChooser.cs 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using uqac_ia_sudoku_csp.Interfaces;
  4. namespace uqac_ia_sudoku_csp.Solver.NextValueChoosers
  5. {
  6. public class DhNextCellChooser : INextCellChooser
  7. {
  8. public void SelectVariable(Board board, out int x, out int y, IList<IConstraint> constraints)
  9. {
  10. var dict = new Dictionary<KeyValuePair<int, int>, int>();
  11. for (var yy = 0; yy < board.Size; ++yy)
  12. {
  13. for (var xx = 0; xx < board.Size; ++xx)
  14. {
  15. if (board.GetNumber(xx, yy) == null)
  16. {
  17. var k = new KeyValuePair<int, int>(xx, yy);
  18. if (!dict.ContainsKey(k))
  19. {
  20. dict.Add(k, 0);
  21. }
  22. ++dict[k];
  23. }
  24. }
  25. }
  26. if (!dict.Any())
  27. {
  28. x = -1;
  29. y = -1;
  30. }
  31. else
  32. {
  33. var k = dict.OrderByDescending(pair => pair.Value).First().Key;
  34. x = k.Key;
  35. y = k.Value;
  36. }
  37. }
  38. }
  39. }