コード例 #1
0
    public void LookUpWords_9x9_AFewWords()
    {
        var grid    = new WordGrid(9, 9);
        var words   = new[] { "MATTHEW", "JOE", "MARY", "SARAH", "SALLLY" };
        var domains = words.ToDictionary(w => w, w => (IReadOnlyCollection <List <WordGrid.GridLocation> >)grid.GenerateDomain(w));

        var csp = new ConstraintSatisfactoryProblem <string, List <WordGrid.GridLocation> >(words, domains);

        csp.AddConstraint(new WordGrid.WordSearchConstraint(words));
        var solution = csp.BacktrackingSearch();

        foreach (var pair in solution)
        {
            _output.WriteLine($"{pair.Key} {string.Join(", ", pair.Value.Select(v => (v.Row, v.Column).ToString()))}");
        }

        _output.WriteLine(grid.ToString());

        foreach (var pair in solution)
        {
            grid.InitializeWithWord(pair.Key, pair.Value);
        }

        _output.WriteLine(grid.ToString());
    }
コード例 #2
0
    public static Dictionary <string, string> ColorTheMap()
    {
        var domains = Regions
                      .ToDictionary(r => r, r => (IReadOnlyCollection <string>)Colors);

        var constraints = new[]
        {
            new MapColoringConstraint(Regions[0], Regions[1]),

            new MapColoringConstraint(Regions[2], Regions[0]),
            new MapColoringConstraint(Regions[2], Regions[1]),
            new MapColoringConstraint(Regions[2], Regions[3]),
            new MapColoringConstraint(Regions[2], Regions[4]),
            new MapColoringConstraint(Regions[2], Regions[5]),

            new MapColoringConstraint(Regions[1], Regions[3]),

            new MapColoringConstraint(Regions[4], Regions[3]),
            new MapColoringConstraint(Regions[4], Regions[5]),

            new MapColoringConstraint(Regions[6], Regions[5]),
        };

        var csp = new ConstraintSatisfactoryProblem <string, string>(Regions, domains);

        foreach (var constraint in constraints)
        {
            csp.AddConstraint(constraint);
        }

        var coloredMap = csp.BacktrackingSearch(null);

        return(coloredMap);
    }