Chinaunix首页 | 论坛 | 博客
  • 博客访问: 201378
  • 博文数量: 63
  • 博客积分: 2764
  • 博客等级: 少校
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-28 21:11
文章分类

全部博文(63)

文章存档

2011年(35)

2010年(28)

我的朋友

分类: C/C++

2010-09-07 20:35:02

今天看了C专家编程,里面有一章是令人头疼的编程挑战
1、输入一个字符串,输出字符串中的所有组合。
2、八皇后的问题。有多少中方案。
上网找了一些,调试好了程序。放这里供大家学习、优化。

首先是第一个:

/////////////////////////////////////////////////////////////////////////

//// Get the permutation of a string,

//// for example, input string abc, its permutation is

//// abc acb bac bca cba cab

///////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////

//Print the permutation of a string,

//Input: pStr - input string

//pBegin - points to the begin char of string

//which we want to permutate in this recursion

/////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <string.h>

void Permutation(char* pStr, char* pBegin)
{
        if(!pStr || !pBegin)
                return;

        //if pBegin points to the end of string,

        // this round of permutation is finished,

        // print the permuted string

        if(*pBegin == '\0')
        {
                printf("%s\n", pStr);
        }
        // otherwise, permute string

        else
        {
                char *pCh;
                for(pCh = pBegin; *pCh != '\0'; ++ pCh)
                {
                        // swap pCh and pBegin

                        char temp = *pCh;
                        *pCh = *pBegin;
                        *pBegin = temp;

                        Permutation(pStr, pBegin + 1);

                        // restore pCh and pBegin

                        temp = *pCh;
                        *pCh = *pBegin;
                        *pBegin = temp;
                }
        }
}

int main(void)
{
        char buf[1024];
        printf("getchar:%s\n",buf);
        fgets(buf,1024,stdin);
        printf("string:%s",buf);

        Permutation(buf, buf);

        return 0;
}

第二个:

/*
 * 八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题。该问题是十九世纪著名的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。高斯认为有76种方案。1854年在柏林的象棋杂志上不同的作者发表了40种不同的解,后来有人用图论的方法解出92种结果。计算机发明后,有多种方法可以解决此问题.
 *
 */


#include <stdio.h>
#include <stdlib.h>
#define N 8 /* 定义棋盘大小 */
int place(int k); /* 确定某一位置皇后放置与否,放置则返回1,反之返回0 */
void backtrack(int i);/* 主递归函数,搜索解空间中第i层子树 */
void chessboard(); /* 每找到一个解,打印当前棋盘状态 */
static int sum, /* 当前已找到解的个数 */
                   x[N]; /* 记录皇后的位置,x[i]表示皇后i放在棋盘的第i行的第x[i]列 */

int main(void)
{
        backtrack(0);
        getchar();
        return 0;
}
int place(int k)
{
        /* 测试皇后k在第k行第x[k]列时是否与前面已放置好的皇后相攻击。 x[j] == */
        /* x[k] 时,两皇后在同一列上;abs(k - j) == abs(x[j] - x[k]) 时,两皇 */
        /* 后在同一斜线上。两种情况两皇后都可相互攻击,故返回0表示不符合条件。*/
        int j;
        for (j = 0; j < k; j ++)
                if (abs(k - j) == abs(x[j] - x[k]) || (x[j] == x[k])) return 0;
        return 1;
}
void backtrack(int t)
{
        /* t == N 时,算法搜索至叶结点,得到一个新的N皇后互不攻击的放置方案 */
        int i;
        if (t == N) chessboard();
        else
                for (i = 0; i < N; i ++) {
                        x[t] = i;
                        if (place(t)) backtrack(t + 1);
                }
}
void chessboard()
{
        int i,j;
        printf("第%d种解法:\n", ++ sum);
        for (i = 0; i < N; i ++) {
                for (j = 0; j < N; j ++)
                        if (j == x[i]) printf("@ ");
                        else printf("* ");
                printf("\n");
        }
        printf("\n");
}


放在这里提供下载:
文件:string.tar
大小:40KB
下载:下载
C专家也放这里吧
阅读(775) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-09-10 20:16:24

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com