示例#1
0
    public int makeStep()
    {
        int changedCol = -1;
        int colNewRow  = -1;

        Debug.Log("step");
        // if all columns were processed without conflicts the solution is there
        if (stk.Count == nQueens + 1)
        {
            qScript.addSolution();
            Debug.Log("FOUND SOLUTION!");
            stk.Pop();
            take(stk.Peek(), stk.Count - 1);

            changedCol = (stk.Count - 1);
            colNewRow  = -1;
        }
        // if the row for given column exceeds the size of the board
        else if (stk.Peek() == nQueens)
        {
            stk.Pop();
            if (stk.Count != 0)
            {
                take(stk.Peek(), stk.Count - 1);

                changedCol = (stk.Count - 1);
                colNewRow  = -1;
            }
        }
        // otherwise just go to the next row for current column
        else
        {
            int top = stk.Pop();
            stk.Push(++top);

            if (stk.Peek() < nQueens)
            {
                changedCol = (stk.Count - 1);
                colNewRow  = stk.Peek();

                if (check(stk.Peek(), stk.Count - 1))
                {
                    put(stk.Peek(), stk.Count - 1);
                    stk.Push(-1);

                    if (!isBlue [changedCol])
                    {
                        isBlue [changedCol] = true;
                        qScript.setColor(changedCol, Color.blue);
                    }
                }
                else
                {
                    isBlue [changedCol] = false;
                    qScript.setColor(changedCol, Color.red);
                }
            }
            else
            {
                changedCol = (stk.Count - 1);
                colNewRow  = -1;
                qScript.setColor(changedCol, Color.blue);
            }
        }

        if (changedCol >= 0)
        {
            currentQpos [changedCol] = colNewRow;
        }
        return(changedCol);
    }