经典的递归程序设计中的2到题目
1,八皇后问题
,国际象棋棋盘走法,用递归实现所有的可能性;
棋盘:
(1),代码如下:
# includetypedef 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),运行结果:
# include八皇后和全排列