示例#1
0
文件: App.xaml.cs 项目: delner/sudoku
            public static bool Solve(SudokuGrid grid, out List<string> solution)
            {
                int[,] g = new int[(grid.Puzzle.Rank * grid.Puzzle.Rank), (grid.Puzzle.Rank * grid.Puzzle.Rank)];

                for (int i = 0; i < (grid.Puzzle.Rank * grid.Puzzle.Rank); i++)
                {
                    for (int j = 0; j < (grid.Puzzle.Rank * grid.Puzzle.Rank); j++)
                    {
                        g[i, j] = grid.GetCell(i, j).Value.HasValue ? grid.GetCell(i, j).Value.Value : 0;
                    }
                }

                bool result = Solve(g, grid.Puzzle.Rank, 0, 0);

                if (result)
                {
                    // Set the the solution
                    solution = new List<string>();
                    for (int i = 0; i < (grid.Puzzle.Rank * grid.Puzzle.Rank); i++)
                    {
                        string line = string.Empty;
                        for (int j = 0; j < (grid.Puzzle.Rank * grid.Puzzle.Rank); j++)
                        {
                            if (!string.IsNullOrEmpty(line))
                            {
                                line += ("," + g[i, j].ToString());
                            }
                            else
                            {
                                line += g[i, j].ToString();
                            }
                        }
                        solution.Add(line);
                    }
                }
                else
                {
                    solution = grid.Puzzle.Format;
                }

                return result;
            }