日期:2013-01-06  浏览次数:20518 次

改编自V星[视窗王子]应答程序,如下:<br>
<br>
using System;<br>
class Queen{<br>
    const int SIZE = 8;//皇后数<br>
    public static void Main()<br>
    {<br>
        int[] Queen = new int [SIZE];//每行皇后的位置<br>
        int y,x,i,j,d,t=0;<br>
        y = 0;<br>
        Queen[0] = -1;<br>
        while( true )<br>
        {<br>
            for (x=Queen[y]+1; x<SIZE; x++)<br>
            {<br>
                for (i=0;i<y;i++)<br>
                {<br>
                    j = Queen[i];<br>
                    d = y-i;<br>
                    //检查新皇后是否与以前的皇后能相互攻击<br>
                    if ((j==x)||(j==x-d)||(j==x+d))<br>
                        break;<br>
                }<br>
                if (i>=y)<br>
                    break;//不攻击<br>
            }<br>
            if (x == SIZE) //没有合适的位置<br>
            {<br>
                if (0==y)<br>
                {<br>
                    //回朔到了第一行<br>
                    Console.WriteLine("Done");<br>
                    break; //结束<br>
                }<br>
                //回朔<br>
                Queen[y]=-1;<br>
                y--;<br>
            }<br>
            else<br>