Esempio n. 1
0
        private static void SolveSimpleGraph()
        {
            var southAmerica = new Dictionary<int, int[]>()
            {
                { Brazil, new[] { FrenchGuiana, Suriname, Guyana, Venezuala, Colombia, Peru, Bolivia, Paraguay, Uruguay, Argentina } },
                { FrenchGuiana, new[] { Brazil, Suriname } },
                { Suriname, new[] { Brazil, FrenchGuiana, Guyana } },
                { Guyana, new[] { Brazil, Suriname, Venezuala } },
                { Venezuala, new[] { Brazil, Guyana, Colombia } },
                { Colombia, new[] { Brazil, Venezuala, Peru, Ecuador } },
                { Ecuador, new[] { Colombia, Peru } },
                { Peru, new[] { Brazil, Colombia, Ecuador, Bolivia, Chile } },
                { Chile, new[] { Peru, Bolivia, Argentina } },
                { Bolivia, new[] { Chile, Peru, Brazil, Paraguay, Argentina } },
                { Paraguay, new[] { Bolivia, Brazil, Argentina } },
                { Argentina, new[] { Chile, Bolivia, Paraguay, Brazil, Uruguay } },
                { Uruguay, new[] { Brazil, Argentina } }
            };

            var southAmericaGraph = new Graph(13, from x in southAmerica.Keys
                                                  from y in southAmerica[x]
                                                  select Tuple.Create(x, y));

            var southAmericaSolver = new Solver(southAmericaGraph, 4);
            var solution = southAmericaSolver.Solve();
            foreach (var colour in solution)
            {
                Console.Write(colour + ", ");
            }
        }
Esempio n. 2
0
        public Solver(Graph graph, int colours)
        {
            this.colours = colours;
            this.graph = graph;
            this.possibilities = new BitSet[graph.Size];

            BitSet b = BitSet.Empty;
            for (int i = 0; i < colours; ++i)
            {
                b = b.Add(i);
            }

            for (int p = 0; p < this.possibilities.Length; ++p)
            {
                this.possibilities[p] = b;
            }
        }
Esempio n. 3
0
 private Solver(Graph graph, int colours, BitSet[] possibilities)
 {
     this.graph = graph;
     this.colours = colours;
     this.possibilities = possibilities;
 }
Esempio n. 4
0
        private void SolveCliqueGraph()
        {
            var offsets = new[,]
            {
                { new[] { 0, 4, 8, 12 }, new[] { 0, 1, 2, 3 } }, // rows
                { new[] { 0, 1, 2, 3 }, new[] { 0, 4, 8, 12 } }, // columns
                { new[] { 0, 2, 8, 10 }, new[] { 0, 1, 4, 5 } } // squares
            };

            var cliques =
                from r in Enumerable.Range(0, 3)
                from i in offsets[r, 0]
                select (from j in offsets[r, 1] select i + j);

            var edges = CliquesToEdges(cliques);
            var graph = new Graph(16, edges);
            var solver = new Solver(graph, 4);
            var solution = solver.Solve();
            foreach (var colour in solution)
            {
                Console.Write(colour + ", ");
            }
        }
Esempio n. 5
0
        private void SolveSudoku()
        {
            var offsets = new[,]
            {
                { new[] { 0, 9, 18, 27, 36, 45, 54, 63, 72 }, new[] { 0, 1, 2, 3, 4, 5, 7, 8 } }, // rows
                { new[] { 0, 1, 2, 3, 4, 5, 7, 8 }, new[] { 0, 9, 18, 27, 36, 45, 54, 63, 72 } }, // columns
                { new[] { 0, 3, 6, 27, 30, 33, 54, 57, 60 }, new[] { 0, 1, 2, 9, 10, 11, 18, 19, 20 } } // boxes
            };

            var cliques =
                from r in Enumerable.Range(0, 3)
                from i in offsets[r, 0]
                select (from j in offsets[r, 1] select i + j);

            var edges = CliquesToEdges(cliques);

            Graph graph = new Graph(81, edges);
            var solver = new Solver(graph, 9);
            string puzzle =
                "  8 274  " +
                "         " +
                " 6 38  52" +
                "      32 " +
                "1   7   4" +
                " 92      " +
                "78  62 1 " +
                "         " +
                "  384 5  ";

            int node = -1;
            foreach (char c in puzzle)
            {
                ++node;
                if (c == ' ')
                {
                    continue;
                }

                solver = solver.SetColour(node, c - '1');
            }

            var result = solver.Solve();
            int index = 0;
            foreach (var r in result)
            {
                Console.Write(r + 1 + " ");
                if (index % 9 == 8)
                {
                    Console.WriteLine();
                }

                ++index;
            }
        }