| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 320 人关注过本帖
标题:帮忙看看这个程序--n皇后问题 出现段错误,但找不到那个判断错了
只看楼主 加入收藏
xiaolinmax
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2013-9-8
结帖率:100%
收藏
 问题点数:0 回复次数:0 
帮忙看看这个程序--n皇后问题 出现段错误,但找不到那个判断错了
#include <stdio.h>

char chessboard[8][8];
int queenplace(int, int);

int n_queens(int locx, int locy, int queens)
{
    int i, j;                                                //循环计数变量
    int result = 0;

    if (queens == 8)                                           //递归结束条件
        return 1;
    else
        if (queenplace(locx, locy))                              //递归执行部分
        {
            chessboard[locx][locy] = 'Q';
            for (i = 0; i < 8; i++)
                for (j = 0; j < 8; i++){
                    result += n_queens(i, j, queens+1);
                    if(result > 0)
                        break;
                }
            if( result > 0)
                return 1;
            else{
                chessboard[locx][locy] = 'x';
                return 0;
            }
        }
        else
            return 0;
}

/*判断传入坐标是否可以放置皇后*/
int queenplace(int locx, int locy)
{
    int i, j;
    if (chessboard[locx][locy] != 'x')
        return 0;
    for (j = locy - 1; j >= 0; j-- )     //judge up
        if (chessboard[locx][j] != 'x')
        return 0;
    for (j = locy + 1; j < 8; j++)        //judge down
        if (chessboard[locx][j] != 'x')
        return 0;
    for (i = locx - 1; i >= 0; i--)        //judge left
        if (chessboard[i][locy] != 'x')
        return 0;
    for (i =  locx + 1; i < 8; i++)         //judge right
        if (chessboard[i][locy] != 'x')
        return 0;

    i = locx -1;
    j = locy -1;
    while (i >= 0 && j >= 0)
        if (chessboard[i--][j--] != 'x')    // judge left up
        return 0;

    i = locx + 1;
    j = locy - 1;
    while (i < 8 && j >= 0)                 //judge right up
        if (chessboard[i++][j--] != 'x')
        return 0;

    i = locx - 1;
    j = locy + 1;
    while (i >= 0 && j < 8)                //judge left down
        if (chessboard[i--][j++] != 'x')
        return 0;

    i = locx + 1;
    j = locy + 1;
    while (i < 8 && j < 8)                 //judge right down
        if (chessboard[i++][j++] != 'x')
            return 0;
    return 1;

}

int main()
{
    int i, j;
    for (i = 0; i < 8; i++)
        for (j = 0; j < 8; j++)
        chessboard[i][j] = 'x';
    n_queens(0,0,0);

    printf("the graph of 8 queens on the chessboard:\n");
    printf("      0   1   2   3   4   5   6   7\n");
    printf("    +---+---+---+---+---+---+---+---+\n");

    for (i = 0; i < 8; i++){
        printf("  %d |",i);
        for (j = 0; j < 8; j++)
            printf("-%c-|",chessboard[i][j]);
        printf("\n    +---+---+---+---+---+---+---+---+\n");

    }
    return 0;
}
2015-09-07 16:41
快速回复:帮忙看看这个程序--n皇后问题 出现段错误,但找不到那个判断错了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028627 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved