请求帮助排除c ++八皇后拼图代码 [英] Asking for help to troubleshoot a c++ Eight queens puzzle code

查看:94
本文介绍了请求帮助排除c ++八皇后拼图代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C ++代码编写了一个函数,用于八皇后问题。该程序应该打印出所有92种可能的解决方案。我只能跑到40岁。不知道问题在哪里。尝试调试,但仍然卡住。

I have written a function in C++ code for the eight queens problem. The program is supposed to print out all 92 possible solutions. I only can run up to 40. Don't know where the problem is. Try to debug but I'm still stuck.

#include "stdafx.h"
#include <cmath>
#include <iostream>
using namespace std;

bool ok(int board[8][8]){
    for(int c = 7; c > 0; c--){
        int r = 0;
        while(board[r][c] != 1 ){
            r++;
        } // while loop

        for(int i = 1; i <= c; i++){
            if(board[r][c-i] == 1)
                return false;
            else if (board[r-i][c-i] == 1)
                return false;
            else if (board[r+i][c-i] == 1)
                return false;
        } // for loop

    } // for loop
        return true;
} // ok

void print(int board[8][8], int count){
    cout << count << endl;
    for(int i = 0; i < 8; i++){
        for(int j = 0; j < 8; j++){
            cout << board[i][j];
        } // for loop 
        cout << endl;

    } // for loop

    cout << endl;
} // print board

int main (){

    int board[8][8]={0};
    int count = 0;
    for(int i0 = 0; i0 < 8; i0++)
       for(int i1=0; i1 < 8; i1++)
          for(int i2 = 0; i2 < 8; i2++)
         for(int i3 = 0; i3 < 8; i3++)
            for(int i4 = 0; i4 < 8; i4++)
           for(int i5 = 0; i5 < 8; i5++)
              for(int i6 = 0; i6 < 8; i6++)
                 for(int i7 = 0; i7 < 8; i7++){
                board[i0][0]=1;
                            board[i1][1]=1;
                            board[i2][2]=1;
                            board[i3][3]=1;
                            board[i4][4]=1;
                            board[i5][5]=1;
                            board[i6][6]=1;
                            board[i7][7]=1;

                            if(ok(board))print(board, ++count);

                            board[i0][0]=0;
                            board[i1][1]=0;
                            board[i2][2]=0;
                            board[i3][3]=0;         
                            board[i4][4]=0; 
                            board[i5][5]=0;
                            board[i6][6]=0;
                            board[i7][7]=0;

                                }
    return 0;
}


推荐答案

您的问题在 ok 函数。它有三个错误,都与你的矩阵的边界有关。第一个错误(如果任何原因导致您收到过多的解决方案)将在这里:

Your problem is in the ok function. It has three errors, all relating to the bounds of your matrix. The first error (which will if anything cause you to receive too many solutions), is here:

for(int c = 7; c > 0; c--){

检查列0.测试应为 c> = 0

其他两个错误,导致不可预测行为,在这里:

The other two errors, which cause unpredictable behavior, are here:

    for(int i = 1; i <= c; i++){
        if(board[r][c-i] == 1)
            return false;
        else if (board[r-i][c-i] == 1)
            return false;
        else if (board[r+i][c-i] == 1)
            return false;
    } // for loop

这会导致 / code>函数返回任意数量的假阴性。在我的情况下,编译和运行您的程序与这两个错误没有产生解决方案。它只是偶然的机会,它为您产生40解决方案。

This can cause the ok function to return an arbitrary number of false negatives. In my case, compiling and running your program with these two errors produced no solutions. It is only by chance that it produces 40 solutions for you.

问题再次与边界。 i 变量从1移动到 c ,因此 ci 按照意图从 c-1 下移到 0

The problem is again with bounds. The i variable is moving from 1 up to and including c, so c-i moves down from c-1 to 0, as intended.

但是,您不是检查 ri r + i 矩阵。考虑 r = 7 i = 4 的情况。然后, r + i = 11 ,运行超过行的末尾。类似地,如果 r = 0 i 是0以外的任何值, ri 将为负数,并且超出该行的开始。

However, you are not checking that r-i and r+i remain within the bounds of the matrix. Consider the case that r = 7 and i = 4. Then, r+i = 11, which runs past the end of the row. Similarly, if r = 0 and i is anything other than 0, r-i will be negative and run past the beginning of the row.

您需要添加其他检查,以确保在此测试中使用的行值循环在0到7的范围内。您可以利用C ++中的逻辑运算符的短路行为来执行此操作,例如:

You need to add additional checks to make sure that the row values used in the tests within this loop are within the range 0 to 7. You can take advantage of the short-circuiting behavior of the logical operators in C++ to do this, for example:

 else if (<test> && board[r-i][c-i] == 1)


仅当< test> 为true时,

才会检查 board [ri] [ci]

我离开添加更正这些后两个错误作为一个练习,因为这很可能是一个家庭作业分配(如果是,你应该在问题中添加[家庭作业]标签)。

I'm leaving adding the corrections to these second two errors as an exercise for you, since this is very likely to be a homework assignment (and if it is, you should add the [homework] tag to the question).

这篇关于请求帮助排除c ++八皇后拼图代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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