八皇后和全排列

  

经典的递归程序设计中的2到题目

1,八皇后问题

,国际象棋棋盘走法,用递归实现所有的可能性;

棋盘:

八皇后和全排列

(1),代码如下:

# include      typedef  unsigned  char 布尔;      # define  TRUE ,,,,,,, 1   # define  FALSE ,,,,,,, 0      # define  EIGHT ,, 8      void  showChess (int 国际象棋(*)[8]);,,//显示棋盘   boolean  isSafe (int 国际象棋(*)[8],int 行,int 坳);,//判断这个位置是否安全   void  eightQueen (int 国际象棋(*)[8],int 行),,,//八皇后的递归程序      void  eightQueen (int 国际象棋(*)[8],int 行){   ,,,int  colIndex;   ,,,,   ,,,如果(row 祝辞=,8){   ,,,,,,,showChess(象棋);   还有,,,}{   ,,,,,,,,(=colIndex  0;, colIndex  & lt;, 8;, colIndex + +) {   ,,,,,,,,,,,如果(isSafe(国际象棋,,行,,colIndex),==, TRUE) {   ,,,,,,,,,,,,,,,国际象棋(行)[colIndex],=, 1;   ,,,,,,,,,,,,,,,eightQueen(国际象棋,,行+ 1);   ,,,,,,,,,,,,,,,国际象棋(行)[colIndex],=, 0;   ,,,,,,,,,,,}   ,,,,,,,}   ,,,}   }      boolean  isSafe (int 国际象棋(*)[8],int 行,int 坳){   ,,,int  rowIndex;   ,,,int  colIndex;      ,,,(rowIndex =,第1行;,rowIndex 祝辞=,0;,rowIndex——) {   ,,,,,,,如果(国际象棋(rowIndex) (col),==, 1) {   ,,,,,,,,,,,return 假;   ,,,,,,,}   ,,,}   ,,,(rowIndex =,第1行,,colIndex =, col-1;, rowIndex 祝辞=,0,,,,colIndex 祝辞=,0;,rowIndex———, colIndex——) {   ,,,,,,,如果(国际象棋[rowIndex] [colIndex],==, 1) {   ,,,,,,,,,,,return 假;   ,,,,,,,}   ,,,}   ,,,(rowIndex =,第1行,,colIndex =, + 1上校;,rowIndex 祝辞=,0,,,,colIndex  & lt;, 8;, rowIndex———, colIndex + +) {   ,,,,,,,如果(国际象棋[rowIndex] [colIndex],==, 1) {   ,,,,,,,,,,,return 假;   ,,,,,,,}   ,,,}      ,,,return 真实;   }      void  showChess (int 国际象棋(*)[8]){   ,,,int 我;   ,,,int  j;   ,,,int  static 计算;      ,,,printf(“解:% d \ n”,, + +数量);   ,,,(小姐:=,0;,小姐:& lt;, 8;,我+ +){   ,,,,,,,,(=j  0;, j  & lt;, 8;, j + +) {   ,,,,,,,,,,,printf (" % 4 d ”,国际象棋[我][j]);   ,,,,,,,}   ,,,,,,,printf (" \ n ");   ,,,}   }      void 主要(void) {   ,,,int 国际象棋[8][8],=,{0};      ,,,eightQueen(国际象棋,0);   }

(2),运行结果:

八皇后和全排列“> <br/> </p> <p>因为4个方向,每一个方向都有23种解法! ! !</p> <p> <br/> </p> <p> 2,全排列问题</p> <p>,从n个数据中挑个选米数据,每个数据只能取一次,输出其全部组合的可能性;</p> <p>(1),代码如下:</p> <pre类= # include   # include      void  fullArray (char  * sourceStr, int  sourceLen,, int  *使用,int 我,char  * resStr, int 数);      void  fullArray (char  * sourceStr, int  sourceLen,, int  *使用,int 我,char  * resStr, int 计数){   ,,,int 指数;      ,,,如果,我的在=,count) {   ,,,,,,,printf (" % s \ n ",, resStr);   还有,,,}{   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

八皇后和全排列