| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 470 人关注过本帖
标题:关于八皇后的问题
只看楼主 加入收藏
kingsbestman
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-12-6
收藏
 问题点数:0 回复次数:3 
关于八皇后的问题
#include <stdio.h>
void putQueen(int,int,int);
void printSolution(void);
int a[8][8]={0},n=1;
main()
{
    putQueen(-1,0,0);
    return 0;
}

void putQueen(int i,int j,int m)
{
    int ix,jx,chi,chj,ok;
    if(m!=-1)
    {
        if(m==0&&j==8)
            putQueen(0,0,-1);
        else
        {
            if(m==0)
            {
                a[i+1][j]=1;
                putQueen(i+1,0,m+1);
            }
            else
            {
                if(m==8)
                {
                    printSolution();
                    n++;
                    for(chi=i,chj=0;a[chi][chj]==0;chj++)
                    {}
                    a[chi][chj]=0;
                    putQueen(i-1,chj+1,m-1);
                }
                else
                {
                    if(j<=7)
                    {
                        for(ix=i+1,jx=j;jx<=7;jx++)
                        {
                            for(ok=0,chi=0,chj=-7;chj<=7;chj++)
                                if(ix+chi<=7&&ix+chi>=0&&jx+chj<=7&&jx+chj>=0)
                                    if(a[ix+chi][jx+chj])
                                        ok++;
                            for(chi=-7,chj=0;chi<=7;chi++)
                                if(ix+chi<=7&&ix+chi>=0&&jx+chj<=7&&jx+chj>=0)
                                    if(a[ix+chi][jx+chj])
                                        ok++;
                            for(chi=-7,chj=-7;chi<=7&&chj<=7;chi++,chj++)
                                if(ix+chi<=7&&ix+chi>=0&&jx+chj<=7&&jx+chj>=0)
                                    if(a[ix+chi][jx+chj])
                                        ok++;
                            for(chi=-7,chj=7;chi<=7&&chj>=-7;chi++,chj--)
                                if(ix+chi<=7&&ix+chi>=0&&jx+chj<=7&&jx+chj>=0)
                                    if(a[ix+chi][jx+chj])
                                        ok++;
                            if(!ok)
                            {
                                a[ix][jx]=1;
                                putQueen(ix,0,m+1);
                            }
                        }
                    }
                    else
                    {
                        ok=1;
                    }
                    if(ok)
                    {
                        for(chi=i,chj=0;a[chi][chj]==0;chj++)
                        {}
                        a[chi][chj]=0;
                        putQueen(i-1,chj+1,m-1);
                    }
                }
            }
        }
    }
}

void printSolution()
{
    int i,j;
    printf("Case %d:\n",n);
    for(i=0;i<=7;i++)
    {
        for(j=0;j<=7;j++)
            printf("%d ",a[i][j]);
        printf("\n");
    }   
}


RT
函数putQueen(上一次放棋的行数0开始,这次开始筛选的列数,放旗子的个数)
本来两个形参就可以,不想改了
可以输出前92个,却无法停止直至出错
93个以后 第一行全为0
求大大改进
搜索更多相关主题的帖子: include return 
2011-12-07 19:37
kingsbestman
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-12-6
收藏
得分:0 
再详细说明吧
中间连续4个FOR语句是判断可否放皇后的
只求找出92种以后还不停止的原因
停止条件是不是错了
2011-12-07 19:41
kingsbestman
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-12-6
收藏
得分:0 
#include <stdio.h>
void putQueen(int,int,int);//用于放置棋子
void printSolution(void);//用于打印摆放图
int a[8][8]={0},n=1;//a[][]用于存储棋子摆放,n代表是第几种方法
main()
{
    putQueen(-1,0,0);
    return 0;
}

void putQueen(int i,int j,int m)//i代表上次放置棋子的行下标,j代表本次开始筛选位置的列下标,m代表已放置棋子的个数
{
    int ix,jx,chi,chj,ok;//ix代表本次放置棋子的行下标,jx代表筛选过程列下标,chi chj用于八个方向位置的变换以及后退指示,ok代表是否可放
    if(m!=-1)//用于结束
    {
        if(m==0&&j==8)//放置结束条件
            putQueen(0,0,-1);
        else
        {
            if(m==0)//第一次放置棋子
            {
                a[i+1][j]=1;
                putQueen(i+1,0,m+1);
            }
            else
            {
                if(m==8)//放了八次棋子
                {
                    printSolution();
                    n++;
                    for(chi=i,chj=0;a[chi][chj]==0;chj++)
                    {}
                    a[chi][chj]=0;
                    putQueen(i-1,chj+1,m-1);
                }
                else
                {
                    if(j<=7)//符合筛选条件
                    {
                        for(ix=i+1,jx=j;jx<=7;jx++)//从j列下标开始筛选
                        {
                            for(ok=0,chi=0,chj=-7;chj<=7;chj++)
                                if(ix+chi<=7&&ix+chi>=0&&jx+chj<=7&&jx+chj>=0)
                                    if(a[ix+chi][jx+chj])
                                        ok++;
                            for(chi=-7,chj=0;chi<=7;chi++)
                                if(ix+chi<=7&&ix+chi>=0&&jx+chj<=7&&jx+chj>=0)
                                    if(a[ix+chi][jx+chj])
                                        ok++;
                            for(chi=-7,chj=-7;chi<=7&&chj<=7;chi++,chj++)
                                if(ix+chi<=7&&ix+chi>=0&&jx+chj<=7&&jx+chj>=0)
                                    if(a[ix+chi][jx+chj])
                                        ok++;
                            for(chi=-7,chj=7;chi<=7&&chj>=-7;chi++,chj--)
                                if(ix+chi<=7&&ix+chi>=0&&jx+chj<=7&&jx+chj>=0)
                                    if(a[ix+chi][jx+chj])
                                        ok++;
                            if(!ok)//符合放置条件
                            {
                                a[ix][jx]=1;
                                putQueen(ix,0,m+1);
                            }
                        }
                    }
                    else
                    {
                        ok=1;
                    }
                    if(ok)//都不符合放置条件需要退一步
                    {
                        for(chi=i,chj=0;a[chi][chj]==0;chj++)//找到上次放棋的位置
                        {}
                        a[chi][chj]=0;//取出棋子
                        putQueen(i-1,chj+1,m-1);
                    }
                }
            }
        }
    }
}

void printSolution()
{
    int i,j;
    printf("Case %d:\n",n);
    for(i=0;i<=7;i++)
    {
        for(j=0;j<=7;j++)
            printf("%d ",a[i][j]);
        printf("\n");
    }   
}
2011-12-07 19:51
kingsbestman
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-12-6
收藏
得分:0 
新来了加注释的
2011-12-07 19:51
快速回复:关于八皇后的问题
数据加载中...
 
   



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

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