关于约瑟夫问题

#include <iostream>
using namespace std;

int main()
{

bool checkout(int a[8][8],int row,int line);
void put(int a[8][8],int line,int row);
void eight_queen(const int a[8][8],int record);
void print_array(const int a[8][8]);
//建立8*8的棋盘数组,用数字1表示此行有皇后,初始化为数字-1,0表示该位置不能够防止皇后
int chessboard [8][8];
int record = 0;
//初始化
for(int i= 0;i < 8;i++)
{
for(int j = 0;j < 8;j++)
{
chessboard[i][j] = -1;
}
}
eight_queen(chessboard,record);
}


//判断该位置是否可以放皇后
bool checkout(const int a[8][8],int line, int row)
{
return (a[line][row] == -1)?true:false;
}


//放皇后
void put(int a[8][8],int line,int row)
{
//所在行位置均为零
for(int i = 0;i< 8;i++)
{
a[line][i]=0;
}

//所在列所有位置均设置为零
for(int j = 0;j< 8;j++)
{
a[j][row]=0;
}

//所在反对角线均为零
int x = line;
int y = row;
int x1 = line;
int y1 = row;
for(int k =0; k < 8; k++)
{
if(x > 7){x -= 8;}
if(y < 0){y += 8;}
a[x++][y--] = 0;
}

//所在对角线均为零

for(int k =0; k < 8; k++)
{
if(x1 > 7){x1 -= 8;}
if(y1 > 7){y1 -= 8;}
a[x1++][y1++] = 0;
}

a[line][row] = 1;
}


//输出结果
void print_array(const int a[8][8])
{
for(int i = 0;i < 8;i++)
{
for(int j = 0;j < 8;j++)
{
if(a[i][j]>=0){cout<< " "<<a[i][j]<<" ";}
else{cout<<a[i][j]<<" ";}
}
cout<<endl;
}
}

//递归算法
void eight_queen(const int a[8][8],int record)
{
int b[8][8];
for(int i= 0;i < 8;i++)
{
for(int j = 0;j < 8;j++)
{
b[i][j] = a[i][j];
}
}
if(record == 8)
{
print_array(b);
cout<<endl;
cout<<endl;
}
for(int i = 0;i < 8;i++)
{
if(checkout(b,record,i))
{
int c[8][8] ;
for(int m= 0;m < 8;m++)
{
for(int n = 0;n < 8;n++)
{
c[m][n] = b[m][n];
}
}
put(c,record,i);
eight_queen(c,record+1);
}
}
}
为什么输出结果不对啊,输出结果很诡异,不知道是什么情况
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值