using System.Collections.Generic; using uqac_ia_sudoku_csp.Interfaces; namespace uqac_ia_sudoku_csp.Solver.NextValueChoosers { public class MRVNextValueChooser : INextValueChooser { public void SelectVariable(Board board, out int x, out int y, IList constraints) { var avail = 1; while (avail < board.Size) { for (var xx = 0; xx < board.Size; ++xx) { for (var yy = 0; yy < board.Size; ++yy) { if (board.GetNumber(xx, yy) == null) { var remaining = GetRemainingValue(board, xx, yy, constraints); if (remaining == avail) { x = xx; y = yy; return; } } } } ++avail; } x = -1; y = -1; } protected int GetRemainingValue(Board board, int x, int y, IList constraints) { var c = 0; for (var i = 0; i < board.Size; ++i) { board.SetNumber(x, y, i); if (board.IsConsistent(x, y, constraints)) { ++c; } } board.ClearNumber(x, y); return c; } } }