Exemplo n.º 1
0
    void ItsUniqueSudoku(int index = 0)
    {
        if (index >= panelsWithNum.Count)
        {
            return;
        }
        Panel currentPanel = panelsWithNum[index];
        int   currentNum   = currentPanel.GetCurrentNum();

        currentPanel.EraseNumber();
        emptyPanels.Add(currentPanel);
        for (int i = 1; i <= 9; ++i)
        {
            currentPanel.SetCandidate(i);
            if (CanBePlaced(currentPanel))
            {
                currentPanel.AddCandidate(i);
            }
        }
        List <int> panelCandidates = currentPanel.GetListOfCandidates();

        if (panelCandidates.Count > 1)
        {
            int numOfSolutions = 0;
            for (int i = 0; i < panelCandidates.Count; ++i)
            {
                currentPanel.SetNumber(panelCandidates[i]);
                if (SolveSudoku())
                {
                    numOfSolutions++;
                }
            }
            if (numOfSolutions > 1)
            {
                emptyPanels.Remove(currentPanel);
                currentPanel.SetNumber(currentNum);
            }
        }
        for (int i = 0; i < emptyPanels.Count; ++i)
        {
            emptyPanels[i].EraseNumber();
        }
        ItsUniqueSudoku(++index);
    }