| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 425 人关注过本帖
标题:新手求帮助 ——为什么程序不能循环
只看楼主 加入收藏
jiehai
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-12-25
结帖率:0
收藏
已结贴  问题点数:20 回复次数:6 
新手求帮助 ——为什么程序不能循环
#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
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:20 
很壮观

重剑无锋,大巧不工
2011-12-25 09:11
jiehai
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-12-25
收藏
得分:0 
求救啊
2011-12-25 09:21
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:0 
你的代码真不知道该怎么下手。只能重写。八皇后的原理你理解多少?

重剑无锋,大巧不工
2011-12-25 09:42
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
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:0 
送一段示例给你
程序代码:
#include<stdio.h>
int count;
void print(int *a)
{
    int i, j;
    printf("\nNo.%d\n", ++count);
    for(i = 0; i < 8; i++, putchar('\n'))
    for(j = 0; j < 8; putchar((a[i] == j++) ? 'X' : '.'));
    putchar('\n');
    printf("Press any key to continue...");
    getch();
}
void P(int *a, int s)
{
    int i, t;
    for(t = s - 1, i = 0; i < t; i++)
    {
        if(a[i] + i == a[t] + t) return;
        if(a[i] - i == a[t] - t) return;
    }
    if(s >= 8){ print(a); return; }
    for(i = s; i < 8; i++)
    {
        t = a[s]; a[s] = a[i]; a[i] = t;
        P(a, s + 1);
        t = a[s]; a[s] = a[i]; a[i] = t;
    }
}
int main()
{
    int a[] = {0, 1, 2, 3, 4, 5, 6, 7};
    P(a, 0);
    return 0;
}

重剑无锋,大巧不工
2011-12-25 14:12
快速回复:新手求帮助 ——为什么程序不能循环
数据加载中...
 
   



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

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