mardi 28 janvier 2020

How do I get the previous element of a list using the same iterator in C++?

I am making a sudoku solver and I cannot manage to get backtracking working by refering to the last element of my list (which holds the positions of the blank spaces).

The code below shows my naive initial approach to this issue

{
    position CurrentPos;

    list<position>::iterator j;
    for (j = blankSpaces.begin();  j != blankSpaces.end();  j++)
    {
        CurrentPos.row = j->row;
        CurrentPos.col = j->col;

        for (int i = arr[CurrentPos.row][CurrentPos.col] + 1; i < 10; i++)
        {
            if (valid(arr, CurrentPos, i))
            {
                arr[CurrentPos.row][CurrentPos.col] = i;
                visitedStack.emplace_front(CurrentPos);
            }
            if (!(valid(arr, CurrentPos, i)) && (i == 9))
            {
                j--;
            }
        }
    }

}

Thanks in advanced

Aucun commentaire:

Enregistrer un commentaire