八皇后和全排列

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

成都創(chuàng)新互聯(lián)一直通過網(wǎng)站建設(shè)和網(wǎng)站營銷幫助企業(yè)獲得更多客戶資源。 以"深度挖掘,量身打造,注重實效"的一站式服務(wù),以網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計、移動互聯(lián)產(chǎn)品、成都營銷網(wǎng)站建設(shè)服務(wù)為核心業(yè)務(wù)。十余年網(wǎng)站制作的經(jīng)驗,使用新網(wǎng)站建設(shè)技術(shù),全新開發(fā)出的標準網(wǎng)站,不但價格便宜而且實用、靈活,特別適合中小公司網(wǎng)站制作。網(wǎng)站管理系統(tǒng)簡單易用,維護方便,您可以完全操作網(wǎng)站資料,是中小公司快速網(wǎng)站建設(shè)的選擇。

1、八皇后問題

  國際象棋棋盤走法,用遞歸實現(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); //判斷這個位置是否安全
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)、運行結(jié)果:

八皇后和全排列

因為4個方向,每一個方向都有23種解法!!!

2、全排列問題

  從n個數(shù)據(jù)中挑選m個數(shù)據(jù),每個數(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("請問要幾個進行全排列? ");
    scanf("%d", &count);

    fullArray(sourceStr, strlen(sourceStr), used, 0, resStr, count);
}

(2)、運行結(jié)果:

八皇后和全排列

網(wǎng)站題目:八皇后和全排列
URL地址:http://muchs.cn/article2/ipgsic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、電子商務(wù)軟件開發(fā)、定制開發(fā)、面包屑導(dǎo)航域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營