score:0

Accepted answer

see my solution below. i put a blank in a cell when not solvable

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;

namespace sudoku
{
    public partial class form1 : form
    {
        sudoku sudoku = null;
        public form1()
        {
            initializecomponent();

            sudoku =  new sudoku(this);
        }

        private void solve_click(object sender, eventargs e)
        {
            sudoku.solve();
        }

        private void clear_click(object sender, eventargs e)
        {
            this.controls.remove(sudoku);
            sudoku.dispose();
            sudoku = new sudoku(this);
        }
    }
    public class sudoku : panel
    {
        public const int top_margin = 50;
        public const int left_margin = 50;

        static list<cell> cells = new list<cell>();

        static list<list<cell>> rows = null;
        static list<list<cell>> columns = null;
        static list<list<cell>> boxes = null;

        public sudoku(form form)
        {
            form.controls.add(this);
            this.left = left_margin;
            this.top = top_margin;

            this.width = (2 * left_margin) + (9 * cell.width);
            this.height = (2 * top_margin) + (9 * cell.height);

            rows = (new string('\0',9)).select(x => new list<cell>()).tolist();
            columns = (new string('\0', 9)).select(x => new list<cell>()).tolist();
            boxes = (new string('\0', 9)).select(x => new list<cell>()).tolist();


            for (int i = 0; i < 81; i++)
            {
                cell newcell = new cell(i);
                this.controls.add(newcell);
            }

        }
        public static void solve()
        {
            for (int i = 0; i < 81; i++)
            {
                if (cells[i].textbox.text != "")
                {
                    cells[i].value = int.parse(cells[i].textbox.text);
                }
            }
            recursivesolve(0);
        }
        static boolean recursivesolve(int index)
        {
            boolean solved = false;

            cell cell = cells[index];

            if (cell.value != null)
            {
                if (index == 80)
                {
                    solved = true;
                }
                else
                {
                    solved = recursivesolve(index + 1);
                }
            }
            else
            {
                for (int i = 1; i <= 9; i++)
                {
                    if (!rows[cell.row].where(x => x.value == i).any())
                    {
                        if (!columns[cell.column].where(x => x.value == i).any())
                        {
                            if (!boxes[cell.box].where(x => x.value == i).any())
                            {
                                cell.value = i;
                                cell.textbox.text = i.tostring();
                                cell.textbox.selectionalignment = horizontalalignment.center;
                                cell.textbox.selectionfont = new font("ariel", 30);
                                if (index == 80)
                                {
                                    solved = true;
                                }
                                else
                                {
                                    solved = recursivesolve(index + 1);
                                    if (solved)
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        cell.value = null;
                                        cell.textbox.text = "";
                                    }
                                }
                            }

                        }
                    }
                }
            }
            return solved;
        }

        public class cell : panel
        {
            public const int width = 100;
            public const int height = 100;

            public dictionary<int, int> boxdict = new dictionary<int, int>() {
                {0,0}, {1,0},{2,0},{3,1},{4,1},{5,1},{6,2},{7,2},{8,2},
                {9,0}, {10,0},{11,0},{ 12,1},{13,1},{14,1},{15,2},{16,2},{17,2},
                {18,0}, {19,0},{20,0},{21,1},{22,1},{23,1},{24,2},{25,2},{26,2},
                {27,3}, {28,3},{29,3},{30,4},{31,4},{32,4},{33,5},{34,5},{35,5},
                {36,3}, {37,3},{38,3},{39,4},{40,4},{41,4},{42,5},{43,5},{44,5},
                {45,3}, {46,3},{47,3},{48,4},{49,4},{50,4},{51,5},{52,5},{53,5},
                {54,6}, {55,6},{56,6},{57,7},{58,7},{59,7},{60,8},{61,8},{62,8},
                {63,6}, {64,6},{65,6},{66,7},{67,7},{68,7},{69,8},{70,8},{71,8},
                {72,6}, {73,6},{74,6},{75,7},{76,7},{77,7},{78,8},{79,8},{80,8},
            };

            public int index = 0;
            public int row = 0;
            public int column = 0;
            public int box = 0;
            public int? value = null;
            public richtextbox textbox = new richtextbox();

            public cell() { }

            public cell(int index)
            {
                cells.add(this);
                this.index = index;
                this.row = index / 9;
                this.column = index % 9;
                this.box = boxdict[index];

                this.left = sudoku.left_margin + (column * width);
                this.top = sudoku.top_margin + (row * height);
                this.width = width;
                this.height = height;

                this.controls.add(textbox);
                textbox.left = 0;
                textbox.top = 0;
                textbox.width = width;
                textbox.height = height;
                textbox.selectionalignment = horizontalalignment.center;
                textbox.selectionfont = new font("ariel", 30);
                //textbox.text = index.tostring();
                textbox.multiline = false;

                rows[row].add(this);
                columns[column].add(this);
                boxes[box].add(this);
            }
        }
        public void dispose()
        {
            cells = new list<cell>(); 
            rows = new list<list<cell>>();
            columns = new list<list<cell>>(); ;
            boxes = new list<list<cell>>();
        }
    }
}

Related Query

More Query from same tag