标题:新手求帮助 ——为什么程序不能循环
取消只看楼主
jiehai
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-12-25
结帖率:0
已结贴  问题点数:20 回复次数:3 
新手求帮助 ——为什么程序不能循环
#include<stdio.h>
#include<conio.h>
int qp[8][8];
void printqp()//打印结果
{
    int i,j;
    for(i=0;i<8;i++)
    {
        for(j=0;j<8;j++)
            printf("%d",qp[i][j]);
        printf("\n");
    }
    printf("\n\n");
    //system("pause");
        getch();
}
void fh(int x,int y)//标记被禁止放皇后的位置为1
{
    int i,j;
    for(j=0;j<8;j++)
    if(j!=y) qp[x][j]=1;
    for(i=0;i<8;i++)
    if(i!=x) qp[i][y]=1;
    for(i=1;x-i>=0 && y-i>=0;i++) {qp[x-i][y-i]=1;}
    for(i=1;x+i<8 && j+i<8;i++) qp[x+i][y+i]=1;
    for(i=1;x-i>=0 && y+i<8;i++)  qp[x-i][y+i]=1;
    for(i=1;x+i<8 && y-i>=0;i++)  qp[x+i][y-i]=1;
}
void main()
{
    int r1,r2,r3,r4,r5,r6,r7,r8,i,j,count;
    count=0;
    for(r1=0;r1<8;r1++)
        for(r2=0;r2<8;r2++)
            for(r3=0;r3<8;r3++)
                for(r4=0;r4<8;r4++)
                    for(r5=0;r5<8;r5++)
                        for(r6=0;r6<8;r6++)
                            for(r7=0;r7<8;r7++)
                                for(r8=0;r8<8;r8++)//把皇后每行放一个,不论位置
                                {//qp[i][j]==0表示该位置可以摆放皇后,去qp[i][j]=2表示该位置已放皇后
                                    for(i=0;i<8;i++)
                                        for(j=0;j<8;j++)
                                            qp[i][j]=0;
                                    qp[0][r1]=2;
                                    fh(0,r1);
                                    if(qp[1][r2]==0)
                                    {
                                        qp[1][r2]=2;
                                        fh(1,r2);
                                        if(qp[2][r3]==0)
                                        {
                                            qp[2][r3]=2;
                                                fh(2,r3);
                                            if(qp[3][r4]==0)
                                            {
                                                qp[3][r4]=2;
                                                fh(3,r4);
                                                if(qp[4][r5]==0)
                                                {
                                                    qp[4][r5]=2;
                                                    fh(4,r5);
                                                    if(qp[5][r6]==0)
                                                    {
                                                        qp[5][r6]=2;
                                                        fh(5,r6);
                                                        if(qp[6][r7]==0)
                                                        {
                                                            qp[6][r7]=2;
                                                            fh(6,r7);
                                                            if(qp[7][r8]==0)
                                                            {
                                                                qp[7][r8]=2;
                                                                fh(7,r8);
                                                                count=count+1;
                                                                printf("%d\n",count);
                                                                printqp();
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
}
搜索更多相关主题的帖子: include system 
2011-12-25 09:03
jiehai
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-12-25
得分:0 
求救啊
2011-12-25 09:21
jiehai
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-12-25
得分:0 
同行同列以及对角不能摆放皇后
2011-12-25 09:50
jiehai
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-12-25
得分:0 
我是先吧棋盘全部初始为零,八皇后每行随机放一个, 然后禁止放皇后的位置为1,放皇后的位置为2  ,谢谢
2011-12-25 09:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-358569-1-1.html




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

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