Dictionary values are not correctly updated during a while loop

89 Views Asked by At

I am currently attempting to write a Chronological Backtracking Algorithem for a sudoku solver. I have several positions stored as FlexiblePositions, which is represented as two integer in an array. Each value is stored in a dictionary with a list of numbers from 1 to 9.

The algorithem takes the location, fills it in the dictionary and gets the list of possible numbers (domain.) He is supposed to take a number, check it with the Checker Boolean method, and then remove it. If the domain is empty he moves in to the previous location.

However when he takes the domain, he doesnt seam to take the domain from the dictionary, only using one list for the entire run from the algorithm.

I don't know how I can cause the loop to take the domain which I request it too take.

int[,] ChronologicalBacktracking(int[,] s, List<int[]>FlexPositions)
{
    var domains = new Dictionary<int[], List<int>>();
    int[,] sudoku = (int[,])s.Clone();
    List<int> options = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    foreach (var i in FlexPositions)
    {
        domains.Add(i, options);
    }
    int pointer = 0;
    while(pointer<FlexPositions.Count())
    {
        int[] pos = FlexPositions[pointer];
        if (domains[pos].Count == 0)
        { 

Here the code should fill up the list of the pos before, then move to the previous one, but he for the if check above he still uses the same domain list.

            domains[pos] = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            pointer--;
            if (pointer>=0)
            {
                int[] prev = FlexPositions[pointer];
                sudoku[prev[0],prev[1]] = 0;
            }
        }
        else
        {

PrintList2 prints a list of all numbers in the list, which will diminish by a number for each iteration, despite pos being changed.

            pos = FlexPositions[pointer];
            PrintList2(domains[pos]);
            int number = domains[pos][domains[pos].Count-1];
            Console.WriteLine(pos[0] + "" + pos[1] + " " + number);
            //PrintSudoku(sudoku);

            if (Checker(sudoku, pos, number))
            {
                Console.WriteLine(pointer);
                sudoku[pos[0], pos[1]] = number;
                domains[pos].RemoveAt(domains[pos].Count-1);
                pointer++;

                if (pointer == FlexPositions.Count())
                {
                    return (sudoku);

                }
            }
            else
            {
                domains[pos].RemoveAt(domains[pos].Count-1);
            }


        }
        
    }
    return (sudoku);
}

I had similar issues with references in the past, so I think it is that. However I am not experienced with C# enough to fix this. Additionally I know this code seams optimally, or might have other issues, as I still need to improve on it once I got it working, however I cant seam to solve the issue with my dictionary list not being updated correctly.

Any help is appreciated.

0

There are 0 best solutions below