8 Queens问题 [英] Problem with 8 Queens Problem

查看:83
本文介绍了8 Queens问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个问题上研究了一段时间了,良好"功能似乎存在问题,该功能应该在每次下新女王后检查董事会以确保其合法性.我相信这与函数的最后两个循环有关,但我不知道是什么.似乎总是允许皇后区彼此成对角线放置. (从右上角到左下角对角线.)请帮助!谢谢!

I have been working on this problem for awhile now and there seems to be a problem with the "good" function which is supposed to check the the board each time a new Queen is laid to make sure it is legal. I believe it has something to do with the final two loops of the function but I can not figure out what. It always seems to allow a Queens to be places diagonally of each other. (Diagonal from top right to bottom left.) PLEASE HELP! Thanks!

bool good()
{
    int counter = 0;
    for(int i = 0; i < MAX; i++)
    {
        for(int n = 0; n < MAX; n++)
        {
            if(board[i][n] == 1)
            {
                counter++;
                if(counter >= 2)
                {
                    return false;
                }
            }
        }
        counter = 0;
    }
    for(int n = 0; n < MAX; n++)
    {
        for(int i = 0; i < MAX; i++)
        {
            if(board[i][n] == 1)
            {
                counter++;
                if(counter >= 2)
                {
                    return false;
                }
            }
        }
        counter = 0;
    }
    int n = 0;
    for(int s = 0; s < MAX; s++)
    {
        for(int i = s; i < MAX; i++)
        {
                if(board[i][n] == 1)
                {
                    counter++;
                    if(counter >= 2)
                    {
                        return false;
                    }
                }
            n++;
        }
        n = 0;
        counter = 0;
    }
    int i = 0;
    for(int s = 0; s < MAX; s++)
    {
        for(int n = s; n < MAX; n++)
        {
                if(board[i][n] == 1)
                {
                    counter++;
                    if(counter >= 2)
                    {
                        return false;
                    }
                }
            i++;
        }
        i = 0;
        counter = 0;
    }
    n = 0;
    for(int s = 7; s <= 0; s--)
    {
        for(int i = s; i <= 0; i--)
        {
            if(board[i][n] == 1)
            {
                counter++;
                if(counter >= 2)
                {
                    return false;
                }
            }
            n++;
        }
        n = 0;
        counter = 0;
    }
    i = 0;
    for(int s = 7; s < MAX; s++)
    {
        for(int n = s; n <= 0; n--)
        {
            if(board[i][n] == 1)
            {
                counter++;
                if(counter >= 2)
                {
                    return false;
                }
            }
            i++;
        }
        i = 0;
        counter = 0;
    }
    return true;
}



用< pre>换行的代码



Code wrapped with <pre> tags.

推荐答案

^ ]


这篇关于8 Queens问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆