八皇后和全排列

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

成都创新互联一直通过网站建设和网站营销帮助企业获得更多客户资源。 以"深度挖掘,量身打造,注重实效"的一站式服务,以网站设计、成都网站设计、移动互联产品、成都营销网站建设服务为核心业务。十余年网站制作的经验,使用新网站建设技术,全新开发出的标准网站,不但价格便宜而且实用、灵活,特别适合中小公司网站制作。网站管理系统简单易用,维护方便,您可以完全操作网站资料,是中小公司快速网站建设的选择。

1、八皇后问题

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

棋盘:

八皇后和全排列

(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)、运行结果:

八皇后和全排列

因为4个方向,每一个方向都有23种解法!!!

2、全排列问题

  从n个数据中挑选m个数据,每个数据只能取一次,输出其全部组合的可能性;

(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)、运行结果:

八皇后和全排列

网站题目:八皇后和全排列
URL地址:https://www.cdcxhl.com/article2/ipgsic.html

成都网站建设公司_创新互联,为您提供网站改版电子商务软件开发定制开发面包屑导航域名注册

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站托管运营