V
V
Vernal962016-05-17 14:27:45
Programming
Vernal96, 2016-05-17 14:27:45

Why does the program loop while iterating?

I am writing a program for solving Sudoku, I implement it using the backtracking method, but when executed, it starts to loop. Please tell me what I'm doing wrong, here is the code:
The main enumeration code:

int i, j, k;
            string text;
            int[] arr = new int[81];
            int[,,] arr1 = new int[81, 3, 9];
            //прием данных        
            i = 0;
            foreach (Control control in Controls)
            {
                TextBox tb = control as TextBox;
                if (tb != null)
                {
                    if (tb.Text != String.Empty)
                    {
                        arr[i] = Convert.ToInt32(tb.Text);
                        i++;
                    }
                    else
                    {
                        arr[i] = 0;
                        i++;
                    }
                }
            }
            //перенос данных в рабочий массив
            /*
            arr1[i,0] - здесь найденное решение 
            arr1[i,0,8] - здесь значение задается если цифра задана в условии
            arr1[i,1] - здесь кандидаты
            arr1[i,2] - здесь уже использованные кандидаты
            */
            for (i = 0; i < 81; i++)
            {
                arr1[i, 0, 0] = arr[i];
                if (arr[i] > 0)
                {
                    arr1[i, 0, 8] = arr[i];
                }
                if (arr[i] == 0)
                {
                    for (j = 0; j < 9; j++)
                    {
                        arr1[i, 1, j] = j + 1;
                    }
                }
            }


            //решение
            for (i = 0; i < 81; i++)
            {
                if (arr1[i, 0, 8] == 0) 
                {
                    for (j = 0; j < 9; j++)
                    {
                        arr1[i, 1, j] = j + 1;
                    }
                    constriction(i, arr1); //сокращение кандидатов
                    k = 0;
                    for (j = 0; j < 9; j++)
                    {
                        if (arr1[i, 1, j] > 0)
                        {
                            if (arr1[i, 1, j] == arr1[i, 2, j]) arr1[i, 1, j] = 0;
                            else k++;
                        }
                    }
                    if (k > 0)
                    {
                        for (j = 0; j < 9; j++)
                        {
                            if (arr1[i, 1, j] > 0 && arr1[i,2,j] == 0)
                            {
                                arr1[i, 0, 0] = arr1[i, 1, j];
                                arr1[i, 2, j] = arr1[i, 1, j];
                               break;
                            }
                        }
                    }
                    else
                    {
                        arr1[i, 0, 0] = 0;
                        for (j = 0; j < 9; j++)
                        {
                            arr1[i, 2, j] = 0;
                        }
                        i--;
                        while(arr1[i,0,8] > 0 && i > 0)
                        {
                            i--;
                        }
                        if (i != 0)
                        {
                            i--;
                        }
                    }
                }
            }

Candidate reduction function code:
int x, j, y;
            //сокращение в строке
            x = i;
            while (x % 9 != 0)
            {
                x--;
            }
            for (j = x; j < x + 9; j++)
            {
                if (j != i)
                {
                    if (arr1[j, 0, 0] > 0) arr1[i, 1, arr1[j, 0, 0] - 1] = 0;
                }
            }
            //сокращение в столбце
            y = i;
            while (y > 8)
            {
                y--;
            }
            for (j = y; j < y + 73; j += 9)
            {
                if (j != i)
                {
                    if (arr1[j, 0, 0] > 0) arr1[i, 1, arr1[j, 0, 0] - 1] = 0;
                }
            }
            //сокращение в сегменте
            x = i - x;
            y = i - y;
            if (x < 3) x = 1;
            if (x > 2 && x < 6) x = 2;
            if (x > 5) x = 3;
            if (y <= 18) y = 1;
            if (y > 18 && y < 54) y = 2;
            if (y >= 54) y = 3;
            x *= y;
            switch (x)
            {
                case 1:
                    {
                        x = 0;
                        break;
                    }
                case 2:
                    {
                        x = 3;
                        break;
                    }
                case 3:
                    {
                        x = 6;
                        break;
                    }
                case 4:
                    {
                        x = 27;
                        break;
                    }
                case 5:
                    {
                        x = 30;
                        break;
                    }
                case 6:
                    {
                        x = 33;
                        break;
                    }
                case 7:
                    {
                        x = 54;
                        break;
                    }
                case 8:
                    {
                        x = 57;
                        break;
                    }
                case 9:
                    {
                        x = 60;
                        break;
                    }
            }
            for (j = x; j < x + 19; j+= 9)
            {
                for (y = j; y < j + 3; y++)
                {
                    if (y != i)
                    {
                        if (arr1[y, 0, 0] > 0) arr1[i, 1, arr1[y, 0, 0] - 1] = 0;
                    }
                }
            }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
Zelimkhan Beltoev, 2016-05-17
@Beltoev

I don't think anyone will go into the details of such unreadable code, so I suggest you solve the problem yourself by discovering such a wonderful word as " Debugger ".
It's very simple: put breakpoints after each while and for loop (just in case). Run the program and every point where the debugger stops, just skip with the F5 key . As a result, as soon as the program "loops", you will know to what point you have not reached, therefore, the loop standing up to this breakpoint will be guilty.
Well, then put a breakpoint before this loop and the F10 keyUnderstand step by step why the loop condition is not met.
Looked closely at the code:

for (i = 0; i < 81; i++) <-----
            {
...
                        i--; <-----
                        while(arr1[i,0,8] > 0 && i > 0)
                        {
                            i--; <-----
                        }
                        if (i != 0)
                        {
                            i--; <-----
                        }

Got it? =)

A
Adamos, 2016-05-17
@Adamos

Sudoku is solved as follows:
1. we go through the entire table and for each cell we determine what numbers it can contain, according to the ones already available.
2. go through all the cells. If there can be only one digit, we enter this digit in the cells and exclude its variant from all the cells that this one “beats”. We set the flag "there were changes."
3. we check the options on all lines in search of the only place where the number can be. If we find such a place, we enter a number, "beat" the cells, set a flag.
4. if the flag "there were changes" is set, we return to step 2
5. if the field is not filled in completely, then there is no solution.
And no busting with a return is fucking necessary here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question