| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 438 人关注过本帖
标题:求教:写八皇后时遇到的奇葩问题!
取消只看楼主 加入收藏
zyuce
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2012-11-25
结帖率:100%
收藏
 问题点数:0 回复次数:0 
求教:写八皇后时遇到的奇葩问题!
第一次我让整形变量num作为计数器,结果让我费解;第二次我让answer[0].n作为计数器,正常。
程序代码:
#include<stdio.h>
#define N 8
typedef struct
{
    int n;
    int row;
    int line;
}AnswerType;
AnswerType answer[N];
typedef enum
{
    notoccued = 0,
    occued = 1
}Ifoccued;
Ifoccued RowOccued[N];
Ifoccued LeftTop_RightDown[2*N-1];
Ifoccued LeftDown_RightTop[2*N-1];
void print();
int num;
void start(int LineIndex)
{
    for(int RowIndex=0;RowIndex<8;RowIndex++)
    {
        if(RowOccued[RowIndex]==notoccued&&LeftTop_RightDown[RowIndex-LineIndex+N-1]==notoccued
            &&LeftDown_RightTop[RowIndex+LineIndex]==notoccued)
        {
            RowOccued[RowIndex]=occued;
            LeftTop_RightDown[RowIndex-LineIndex+N-1]=occued;
            LeftDown_RightTop[RowIndex+LineIndex]=occued;
            answer[LineIndex].row=RowIndex;
            answer[LineIndex].line=LineIndex;
            if(LineIndex==7)
            { 
                num++;
                answer[0].n++;
                print();
                RowOccued[N]=notoccued;
                LeftTop_RightDown[2*N-1]=notoccued;
                LeftDown_RightTop[2*N-1]=notoccued;
            }else
            {
                start(LineIndex+1);
            }
        RowOccued[RowIndex]=notoccued;
        LeftTop_RightDown[RowIndex-LineIndex+N-1]=notoccued;
        LeftDown_RightTop[RowIndex+LineIndex]=notoccued;
        }
    }
}
void print()
{
    printf("第%d个结果为:",answer[0].n);
    for(int i=0;i<N;i++)
    {
        printf("[%d,%d] ",answer[i].line+1,answer[i].row+1);
    }
    printf("\n");
}
int main()
{
    num=0;
    answer[0].n=0;
    start(0);
    return 0;
}

这是上面代码的运行截图,没有问题 92个结果
[local]1[/local]

注意下面我把计数器换成了num
程序代码:
#include<stdio.h>
#define N 8
typedef struct
{
    int n;
    int row;
    int line;
}AnswerType;
AnswerType answer[N];
typedef enum
{
    notoccued = 0,
    occued = 1
}Ifoccued;
Ifoccued RowOccued[N];
Ifoccued LeftTop_RightDown[2*N-1];
Ifoccued LeftDown_RightTop[2*N-1];
void print();
int num;
void start(int LineIndex)
{
    for(int RowIndex=0;RowIndex<8;RowIndex++)
    {
        if(RowOccued[RowIndex]==notoccued&&LeftTop_RightDown[RowIndex-LineIndex+N-1]==notoccued
            &&LeftDown_RightTop[RowIndex+LineIndex]==notoccued)
        {
            RowOccued[RowIndex]=occued;
            LeftTop_RightDown[RowIndex-LineIndex+N-1]=occued;
            LeftDown_RightTop[RowIndex+LineIndex]=occued;
            answer[LineIndex].row=RowIndex;
            answer[LineIndex].line=LineIndex;
            if(LineIndex==7)
            { 
                num++;
                answer[0].n++;
                print();
                RowOccued[N]=notoccued;
                LeftTop_RightDown[2*N-1]=notoccued;
                LeftDown_RightTop[2*N-1]=notoccued;
            }else
            {
                start(LineIndex+1);
            }
        RowOccued[RowIndex]=notoccued;
        LeftTop_RightDown[RowIndex-LineIndex+N-1]=notoccued;
        LeftDown_RightTop[RowIndex+LineIndex]=notoccued;
        }
    }
}
void print()
{
    printf("第%d个结果为:",num);
    for(int i=0;i<N;i++)
    {
        printf("[%d,%d] ",answer[i].line+1,answer[i].row+1);
    }
    printf("\n");
}
int main()
{
    num=0;
    answer[0].n=0;
    start(0);
    return 0;
}

结果竟然是这样
搜索更多相关主题的帖子: 计数器 皇后 color 
2012-12-20 18:42
快速回复:求教:写八皇后时遇到的奇葩问题!
数据加载中...
 
   



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

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