⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 八皇后.htm

📁 单片机游戏-八皇后,详细看说明了,很好玩的,点阵游戏
💻 HTM
📖 第 1 页 / 共 2 页
字号:
                  <DIV align=center> </DIV></TD>
                <TD height=27>
                  <DIV align=center> </DIV></TD></TR>
              <TR>
                <TD height=27>
                  <DIV align=center>-</DIV></TD>
                <TD height=27>
                  <DIV align=center>-</DIV></TD>
                <TD height=27>
                  <DIV align=center>-</DIV></TD>
                <TD height=27>
                  <DIV align=center>▲</DIV></TD>
                <TD height=27>
                  <DIV align=center>-</DIV></TD>
                <TD height=27>
                  <DIV align=center>-</DIV></TD>
                <TD height=27>
                  <DIV align=center>-</DIV></TD>
                <TD height=27>
                  <DIV align=center>-</DIV></TD></TR>
              <TR>
                <TD height=27>
                  <DIV align=center> </DIV></TD>
                <TD height=27>
                  <DIV align=center> </DIV></TD>
                <TD height=27>
                  <DIV align=center>/</DIV></TD>
                <TD height=27>
                  <DIV align=center>|</DIV></TD>
                <TD height=27>
                  <DIV align=center>\</DIV></TD>
                <TD height=27>
                  <DIV align=center> </DIV></TD>
                <TD height=27>
                  <DIV align=center> </DIV></TD>
                <TD height=27>
                  <DIV align=center> </DIV></TD></TR>
              <TR>
                <TD height=27>
                  <DIV align=center> </DIV></TD>
                <TD height=27>
                  <DIV align=center>/</DIV></TD>
                <TD height=27>
                  <DIV align=center> </DIV></TD>
                <TD height=27>
                  <DIV align=center>|<BR></DIV></TD>
                <TD height=27>
                  <DIV align=center> </DIV></TD>
                <TD height=27>
                  <DIV align=center>\</DIV></TD>
                <TD height=27>
                  <DIV align=center> </DIV></TD>
                <TD height=27>
                  <DIV align=center> </DIV></TD></TR>
              <TR>
                <TD height=27>
                  <DIV align=center>/</DIV></TD>
                <TD height=27>
                  <DIV align=center> </DIV></TD>
                <TD height=27>
                  <DIV align=center> </DIV></TD>
                <TD height=27>
                  <DIV align=center>|</DIV></TD>
                <TD height=27>
                  <DIV align=center> </DIV></TD>
                <TD height=27>
                  <DIV align=center> </DIV></TD>
                <TD height=27>
                  <DIV align=center>\</DIV></TD>
                <TD height=27>
                  <DIV align=center> </DIV></TD></TR></TBODY></TABLE></TD></TR>
        <TR>
          <TD height=24>
            <DIV align=center>2</DIV></TD></TR>
        <TR>
          <TD height=24>
            <DIV align=center>3</DIV></TD></TR>
        <TR>
          <TD height=24>
            <DIV align=center>4</DIV></TD></TR>
        <TR>
          <TD height=24>
            <DIV align=center>5</DIV></TD></TR>
        <TR>
          <TD height=24>
            <DIV align=center>6</DIV></TD></TR>
        <TR>
          <TD height=24>
            <DIV align=center>7</DIV></TD></TR>
        <TR>
          <TD height=24>
            <DIV align=center>8</DIV></TD></TR></TBODY></TABLE>
      <P align=left> 对于一组布局我们可以用一个一维数组来表示:X:ARRAY [1..8] OF 
      INTEGER;X[I]的下标I表示第I个皇后在棋盘的第I行,X[I]的内容表示在第I行的第X[I]列,例如:X[3]=5就表示第3个皇后在第3行的第5列。在这种方式下,要表示两个皇后A和B不在同一列或斜线上的条件可以描述为:X[A]&lt;&gt;X[B] 
      AND ABS(A-B)&lt;&gt;ABS(X[A]-X[B]){A和B分别表示两个皇后的行号}<BR><SPAN 
      class=style3>【解法】<FONT 
      color=#000000>有了上面的分析,相信大家可以找到不同的</FONT></SPAN><FONT 
      color=#000000>算法</FONT><SPAN class=style3><FONT 
      color=#000000>,这里我们提供三种,当然稍加改动就可推广到N皇后问题</FONT></SPAN><BR>PROGRAM 
      QN(INPUT,OUTPUT);{<SPAN class=style1>递归算法</SPAN>}<BR>CONST 
      N=8;<BR>VAR<BR> CONT,I:INTEGER;<BR> A:ARRAY[1..N] OF 
      BYTE;{存放正确的一组解}<BR> C:ARRAY[1..N] OF 
      BOOLEAN;{存放某一列放皇后的情况,用于判断是否有同列的情况}<BR> L:ARRAY[1-N..N-1] OF 
      BOOLEAN;{存放某一斜线上放皇后的情况,用于判断是否有同斜线的情况;斜线的方向为\}<BR> R:ARRAY[2..2*N] OF 
      BOOLEAN;{存放某一斜线上放皇后的情况,用于判断是否有同斜线的情况;斜线的方向为/}</P>
      <P align=left> PROCEDURE PR;<BR> VAR<BR>  I:INTEGER;<BR> BEGIN<BR>  FOR 
      I:=1 TO N DO WRITE(A[I]:4);<BR>  INC(CONT);<BR>  WRITELN(' 
      CONT=',CONT);<BR> END; </P>
      <P align=left> PROCEDURE TRY(I:INTEGER);<BR> VAR<BR> J:INTEGER; </P>
      <P align=left>  PROCEDURE 
      ERASE(I:INTEGER);<BR>  BEGIN<BR>   C[J]:=TRUE;<BR>   L[I-J]:=TRUE;<BR>   R[I+J]:=TRUE;<BR>  END;<BR> BEGIN<BR>  FOR 
      J:=1 TO N DO<BR>  BEGIN<BR>   IF C[J] AND L[I-J] AND R[I+J] 
      THEN<BR>   BEGIN<BR>    A[I]:=J;<BR>    C[J]:=FALSE;<BR>    L[I-J]:=FALSE;<BR>    R[I+J]:=FALSE;<BR>    IF 
      I&lt;N THEN TRY(I+1) ELSE 
      PR;<BR>    ERASE(I);<BR>   END;<BR>  END;<BR> END; </P>
      <P align=left>BEGIN<BR> FOR I:=1 TO N DO C[I]:=TRUE;<BR> FOR I:=1-N TO N-1 
      DO L[I]:=TRUE;<BR> FOR I:=2 TO 2*N DO 
      R[I]:=TRUE;<BR> CONT:=0;<BR> I:=1;<BR> TRY(I);<BR> WRITELN;<BR> WRITELN('PROGRAM 
      END.');<BR> READLN;<BR>END. 
      <P align=left>PROGRAM HUANGHOU(INPUT,OUTPUT);{<SPAN 
      class=style2>迭代回溯算法</SPAN>}<BR>CONST 
      N=8;<BR>VAR<BR> K:INTEGER;<BR> X:ARRAY[1..N] OF INTEGER; 
      <P align=left> FUNCTION 
      PLACE(K:INTEGER):BOOLEAN;<BR> VAR<BR>  I:INTEGER;<BR> BEGIN<BR>  I:=1;<BR>  WHILE 
      I&lt;K DO<BR>  BEGIN<BR>   IF (ABS(X[I]-X[K])=ABS(I-K)) OR (X[I]=X[K]) 
      THEN<BR>   BEGIN<BR>    PLACE:=FALSE;<BR>    EXIT;<BR>   END;<BR>   I:=I+1;<BR>  END;<BR>  PLACE:=TRUE;<BR> END;</P>
      <P align=left> PROCEDURE PRN;<BR> VAR<BR>  I:INTEGER;<BR> BEGIN<BR>  FOR 
      I:=1 TO N DO WRITE(X[I]:4);<BR>  WRITELN;<BR> END;</P>
      <P align=left> PROCEDURE 
      NQUEENS(N:INTEGER);<BR> BEGIN<BR>  X[1]:=0;<BR>  K:=1;<BR>  WHILE K&gt;0 
      DO<BR>  BEGIN<BR>   X[K]:=X[K]+1;<BR>   WHILE ((X[K]&lt;=N) AND (NOT 
      PLACE(K))) DO X[K]:=X[K]+1;<BR>   IF X[K]&lt;=N THEN<BR>   BEGIN<BR>    IF 
      K=N THEN 
      PRN<BR>    ELSE<BR>    BEGIN<BR>     K:=K+1;<BR>     X[K]:=0;<BR>    END;<BR>   END<BR>   ELSE 
      K:=K-1;<BR>  END;<BR> END;</P>
      <P align=left>BEGIN<BR> NQUEENS(N);<BR> READLN;<BR>END.</P>
      <P align=left>program bahh;{<SPAN 
      class=style2>递归回溯算法</SPAN>}<BR>var<BR>&nbsp;stack:array[1..20] of 
      byte;<BR>&nbsp;n,total:integer;<BR><BR>&nbsp;procedure 
      make(l:integer);<BR>&nbsp;var<BR>&nbsp; i,j:integer;<BR>&nbsp; 
      att:boolean;<BR>&nbsp;begin<BR>&nbsp; if l=n+1 then<BR>&nbsp; 
      begin<BR>&nbsp;&nbsp; inc(total);<BR>&nbsp;&nbsp; 
      writeln('No:',total);<BR>&nbsp;&nbsp; for j:=1 to n do writeln(' 
      ',stack[j],'*');<BR>&nbsp;&nbsp; writeln;<BR>&nbsp;&nbsp; exit;<BR>&nbsp; 
      end;<BR>&nbsp; for i:=1 to n do<BR>&nbsp; begin<BR>&nbsp;&nbsp; 
      att:=false;<BR>&nbsp;&nbsp; stack[l]:=i;<BR>&nbsp;&nbsp; for j:=1 to l-1 
      do<BR>&nbsp;&nbsp;&nbsp; if(abs(l-j)=abs(stack[j]-i))or(i=stack[j]) 
      then<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
      att:=true;<BR>&nbsp;&nbsp;&nbsp;&nbsp; j:=l-1;<BR>&nbsp;&nbsp;&nbsp; 
      end;<BR>&nbsp;&nbsp; if not att then make(l+1);<BR>&nbsp; 
      end;<BR>&nbsp;end;<BR>begin<BR>&nbsp;total:=0;<BR>&nbsp;fillchar(stack,sizeof(stack),0);<BR>&nbsp;write('n=');<BR>&nbsp;readln(n);<BR>&nbsp;make(1);<BR>&nbsp;writeln('Total=':9,total);<BR>&nbsp;readln;<BR>end.</P>
      <P align=left>PROGRAM BHH(INPUT,OUTPUT);{<SPAN class=style2>穷举算法<FONT 
      color=#000000><SPAN 
      style="FONT-WEIGHT: 400">:最好理解,但效率最低</SPAN></FONT></SPAN>}<BR>CONST 
      N=8;<BR>VAR<BR> I1,I2,I3,I4,I5,I6,I7,I8:INTEGER;<BR> X:ARRAY [1..N] OF 
      INTEGER;</P>
      <P align=left> PROCEDURE 
      PRINT;{输出正确的解}<BR> VAR<BR>  I:INTEGER;<BR> BEGIN<BR>  FOR I:=1 TO N DO 
      WRITE(X[I]);<BR>  WRITELN;<BR> END;</P>
      <P align=left> FUNCTION 
      CHECK():BOOLEAN;<BR> VAR<BR>  A,B:INTEGER;<BR> BEGIN<BR>  FOR A:=2 TO N 
      DO<BR>  BEGIN<BR>   FOR B:=1 TO A-1 DO<BR>   BEGIN<BR>    IF 
      (ABS(X[A]-X[B])=ABS(A-B)) OR (X[A]=X[B]) 
      THEN<BR>    BEGIN<BR>     CHECK:=FALSE;<BR>     EXIT;<BR>    END;<BR>   END;<BR>  END;<BR>  CHECK:=TRUE;<BR> END;</P>
      <P align=left>BEGIN<BR> FOR I1:=1 TO N DO<BR>  FOR I2:=1 TO N DO<BR>   FOR 
      I3:=1 TO N DO<BR>    FOR I4:=1 TO N DO<BR>     FOR I5:=1 TO N 
      DO<BR>      FOR I6:=1 TO N DO<BR>       FOR I7:=1 TO N DO<BR>        FOR 
      I8:=1 TO N 
      DO<BR>        BEGIN<BR>         X[1]:=I1;<BR>         X[2]:=I2;<BR>         X[3]:=I3;<BR>         X[4]:=I4;<BR>         X[5]:=I5;<BR>         X[6]:=I6;<BR>         X[7]:=I7;<BR>         X[8]:=I8;<BR>         IF 
      CHECK() THEN PRINT;<BR>        END;<BR> READLN;<BR>END.</P></DIV></TD></TR>
  <TR>
    <TD background=八皇后_files/main_center.gif height=45> </TD></TR>
  <TR>
    <TD background=八皇后_files/main_bottom.gif height=102>
      <DIV align=center>
      <DIV align=center>
      <TABLE id=table3 width=457 border=0>
        <TBODY>
        <TR>
          <TD width=57 rowSpan=3><IMG height=31 src="八皇后_files/noilogo.gif" 
            width=88 border=1></TD>
          <TD width=127>
            <DIV align=center>MSN:root@xm818.com</DIV></TD>
          <TD width=86>
            <DIV align=center>QQ:76455837</DIV></TD>
          <TD width=137>
            <DIV align=center><FONT color=#000000><A 
            href="mailto:root@xm818.com">mailto:root@xm818.com</A></FONT></DIV></TD></TR>
        <TR>
          <TD colSpan=3>
            <DIV align=center><FONT color=#ff0000><A 
            href="http://www.xm818.com/" target=_blank><FONT 
            color=#ff0000>厦门八一八</FONT></A> </FONT><FONT 
            color=#0000ff>二00五年元月一日</FONT> 
    </DIV></TD></TR></TBODY></TABLE></DIV></DIV></TD></TR></TBODY></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -