Welcome Guest.

1Answers

My C++ program can’t run. I used debug to find out the reason, but I don’t know how to change it.

Asked by: Brian Hunt 146 views IT February 27, 2021

#include

using namespace std;

int total,s[5][2];

bool a[8][8];

void out()

{

int t=0;

total++;

cout<<"total:"<

for(int i=0;i<8;i++)

{

for(int j=0;j<8;j++)

{

if(s[t][0]==j&&s[t][1]==i&&t<5){cout<<'*';t++;}

else cout<<'-';

}

cout<

}

cout<

}

void bj(int x,int y,bool e)

{

for(int i=1;;i++)

{

a[x+i][y+i]=e;

if(x+i==7||y+i==7)break;

}

for(int i=1;;i++)

{

a[x-i][y+i]=e;

if(x-i==0||y+i==7)break;

}

for(int i=1;;i++)

{

a[x-i][y-i]=e;

if(x-i==0||y-i==0)break;

}

for(int i=1;;i++)

{

a[x+i][y-i]=e;

if(x+i==7||y-i==0)break;

}

}

void fz(int t,int x,int y)

{

s[t][0]=x;

s[t][1]=y;

}

void hs(int t,int x,int y)

{

for(int i=y;i<8;i++)

{

for(int j=x;j<8;j++)

{

if(a[j][i]==0)

{

a[j][i]=1;

bj(j,i,1);

fz(t,j,i);

if(t==4)out();

else hs(t+1,j,i);

a[j][i]=0;

bj(j,i,0);

}

}

}

}

int main()

{

hs(0,0,0);

return 0;

}

原因在函数bj有问题,求修改思路和方法

1 Answers

  1. +4Votes  

    I don’t see the purpose of your program, but the judgment method in your bj is incorrect, which will cause the subscript to overflow

    The correct way is to judge the subscript first, and then exit the loop if it exceeds it

    That is, bj is changed to

    void bj(int x,int y,bool e)

    {

    for (int i=1;; i++)

    {

    if (x+i>7 || y+i>7) break;

    a[ x+i][y+i]=e;

    If(x+i==7||y+i==7)break;

    } }

    for(int i=1;; i++)

    {

    if(xi<0||y+i>7)break;

    a[xi][y+i]=e;

    if(xi==0||y+i==7)break;

    }}

    for(int i=1;; i++)

    {

    if(xi<0||yi<0)break;

    a[xi][yi]=e;

    // // if(xi==0||yi==0)break;

    }

    for (int i=1;; i++)

    {

    if(x+i>7||yi<0)break;

    a(x+ i][yi]=e;

    // if(x+i==7||yi==0)break;

    }

    }

    After looking at the results of the program, there are more than 30,000 (it is running, the results are indeed different)

    Donna Campbell- February 27, 2021 |