經(jīng)典的遞歸程序設(shè)計(jì)中的2到題目

1、八皇后問題
國際象棋棋盤走法,用遞歸實(shí)現(xiàn)所有的可能性;
棋盤:

(1)、代碼如下:
#include<stdio.h>
typedef unsigned char boolean;
#define TRUE 1
#define FALSE 0
#define EIGHT 8
void showChess(int (*chess)[EIGHT]); //顯示棋盤
boolean isSafe(int (*chess)[EIGHT], int row, int col); //判斷這個(gè)位置是否安全
void eightQueen(int (*chess)[EIGHT], int row); //八皇后的遞歸程序
void eightQueen(int (*chess)[EIGHT], int row){
int colIndex;
if(row >= EIGHT){
showChess(chess);
}else{
for(colIndex = 0; colIndex < EIGHT; colIndex++){
if(isSafe(chess, row, colIndex) == TRUE){
chess[row][colIndex] = 1;
eightQueen(chess, row+1);
chess[row][colIndex] = 0;
}
}
}
}
boolean isSafe(int (*chess)[EIGHT], int row, int col){
int rowIndex;
int colIndex;
for(rowIndex = row-1; rowIndex >= 0; rowIndex--){
if(chess[rowIndex][col] == 1){
return FALSE;
}
}
for(rowIndex = row-1, colIndex = col-1; rowIndex >= 0 && colIndex >= 0; rowIndex--, colIndex--){
if(chess[rowIndex][colIndex] == 1){
return FALSE;
}
}
for(rowIndex = row-1, colIndex = col+1; rowIndex >= 0 && colIndex < EIGHT; rowIndex--, colIndex++){
if(chess[rowIndex][colIndex] == 1){
return FALSE;
}
}
return TRUE;
}
void showChess(int (*chess)[EIGHT]){
int i;
int j;
int static count;
printf("解:%d\n", ++count);
for(i = 0; i < EIGHT; i++){
for(j = 0; j < EIGHT; j++){
printf("%4d ", chess[i][j]);
}
printf("\n");
}
}
void main(void){
int chess[EIGHT][EIGHT] = {0};
eightQueen(chess, 0);
}(2)、運(yùn)行結(jié)果:

因?yàn)?個(gè)方向,每一個(gè)方向都有23種解法!!!
2、全排列問題
從n個(gè)數(shù)據(jù)中挑選m個(gè)數(shù)據(jù),每個(gè)數(shù)據(jù)只能取一次,輸出其全部組合的可能性;
(1)、代碼如下:
#include<stdio.h>
#include<string.h>
void fullArray(char *sourceStr, int sourceLen, int *used, int i, char *resStr, int count);
void fullArray(char *sourceStr, int sourceLen, int *used, int i, char *resStr, int count){
int index;
if(i >= count){
printf("%s\n", resStr);
}else{
for(index = 0; index < sourceLen; index++){
if(used[index] == 0){
resStr[i] = sourceStr[index];
used[index] = 1;
fullArray(sourceStr, sourceLen, used, i+1, resStr, count);
used[index] = 0;
}
}
}
}
void main(void){
char sourceStr[80];
int used[80] = {0};
char resStr[80] = {0};
int count;
printf("請輸入字符串: ");
gets(sourceStr);
printf("請問要幾個(gè)進(jìn)行全排列? ");
scanf("%d", &count);
fullArray(sourceStr, strlen(sourceStr), used, 0, resStr, count);
}(2)、運(yùn)行結(jié)果:

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
標(biāo)題名稱:八皇后和全排列-創(chuàng)新互聯(lián)
文章出自:http://chinadenli.net/article2/dpgsoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、微信小程序、網(wǎng)站維護(hù)、Google、虛擬主機(jī)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容