publicclass AlgPro4_nQueen {
privateintqueensNum = 8;
privateint[] column;
privateint[] rightUp;
privateint[] leftUp;
privateint[] queen;
privateintnum;
public AlgPro4_nQueen() {
column = newint[queensNum+1];
rightUp = newint[(queensNum<<1)+1];
leftUp = newint[(queensNum<<1)+1];
for(int i = 1; i <= queensNum; i++)
column[i] = 1;
for(int i = 1; i <= queensNum<<1; i++)
rightUp[i] = leftUp[i] = 1;
queen = newint[queensNum+1];
}
publicvoid backTrack(int i) {
if (i > queensNum) {
showAnswer();
} else {
for (int j = 1; j <= queensNum; j++) {
if (column[j] == 1&& rightUp[i + j] == 1 && leftUp[i - j + queensNum] == 1) {
queen[i] = j;
column[j] = rightUp[i + j] = leftUp[i - j + queensNum] = 0;
backTrack(i+ 1);
column[j] = rightUp[i + j] = leftUp[i - j + queensNum] = 1;
}
}
}
}
publicvoid showAnswer() {
num++;
System.out.println("\nSolution " + num + ":");
for (int y = 1; y <= queensNum; y++) {
for (int x = 1; x <= queensNum; x++) {
if (queen[y] == x) {
System.out.print(" ♀");
}else {
System.out.print(" .");
}
}
System.out.println();
}
}
publicstaticvoid main(String[] args) {
AlgPro4_nQueen nQueen = new AlgPro4_nQueen();
nQueen.backTrack(1);
}
}
求大牛将此Java程序改成C++,感激不尽。
答案:4 悬赏:0 手机版
解决时间 2021-12-28 22:28
- 提问者网友:孤凫
- 2021-12-28 17:05
最佳答案
- 五星知识达人网友:纵马山川剑自提
- 2021-12-28 18:44
include <stdio.h>
public class AlgPro4_nQueen {
private:
int queensNum = 8;
int *column;
int *rightUp;
int *leftUp;
int *queen;
int num;
public:
AlgPro4_nQueen() {
column = (int *)malloc(sizeof(int)*(queensNum+1));
rightUp = (int *)malloc(sizeof(int)*((queensNum<<1)+1));
leftUp = (int *)malloc(sizeof(int)*((queensNum<<1)+1));
for(int i = 1; i <= queensNum; i++)
column[i] = 1;
for(int i = 1; i <= queensNum<<1; i++)
rightUp[i] = leftUp[i] = 1;
queen = (int *)malloc(sizeof(int)*(queensNum+1));
}
void backTrack(int i) {
if (i > queensNum) {
showAnswer();
} else {
for (int j = 1; j <= queensNum; j++) {
if (column[j] == 1&& rightUp[i + j] == 1 && leftUp[i - j + queensNum] == 1) {
queen[i] = j;
column[j] = rightUp[i + j] = leftUp[i - j + queensNum] = 0;
backTrack(i+ 1);
column[j] = rightUp[i + j] = leftUp[i - j + queensNum] = 1;
}
}
}
}
void showAnswer() {
num++;
printf("\nSolution " + num + ":");
for (int y = 1; y <= queensNum; y++) {
for (int x = 1; x <= queensNum; x++) {
if (queen[y] == x) {
printf(" ♀");
}else {
printf(" .");
}
}
printf("\n");
}
}
}
void main() {
AlgPro4_nQueen nQueen = new AlgPro4_nQueen();
nQueen.backTrack(1);}
public class AlgPro4_nQueen {
private:
int queensNum = 8;
int *column;
int *rightUp;
int *leftUp;
int *queen;
int num;
public:
AlgPro4_nQueen() {
column = (int *)malloc(sizeof(int)*(queensNum+1));
rightUp = (int *)malloc(sizeof(int)*((queensNum<<1)+1));
leftUp = (int *)malloc(sizeof(int)*((queensNum<<1)+1));
for(int i = 1; i <= queensNum; i++)
column[i] = 1;
for(int i = 1; i <= queensNum<<1; i++)
rightUp[i] = leftUp[i] = 1;
queen = (int *)malloc(sizeof(int)*(queensNum+1));
}
void backTrack(int i) {
if (i > queensNum) {
showAnswer();
} else {
for (int j = 1; j <= queensNum; j++) {
if (column[j] == 1&& rightUp[i + j] == 1 && leftUp[i - j + queensNum] == 1) {
queen[i] = j;
column[j] = rightUp[i + j] = leftUp[i - j + queensNum] = 0;
backTrack(i+ 1);
column[j] = rightUp[i + j] = leftUp[i - j + queensNum] = 1;
}
}
}
}
void showAnswer() {
num++;
printf("\nSolution " + num + ":");
for (int y = 1; y <= queensNum; y++) {
for (int x = 1; x <= queensNum; x++) {
if (queen[y] == x) {
printf(" ♀");
}else {
printf(" .");
}
}
printf("\n");
}
}
}
void main() {
AlgPro4_nQueen nQueen = new AlgPro4_nQueen();
nQueen.backTrack(1);}
全部回答
- 1楼网友:不甚了了
- 2021-12-28 21:59
//我已经帮你改了,但是我运行了一下,发现结果有问题。
// 2333.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<math.h>
class AlgPro4_nQueen
{
private:
int queensNum ;
int column[8+1];
int rightUp[8<<1 +1];
int leftUp[8<<1 +1];
int queen[8+1];
int num;
public:
AlgPro4_nQueen(void);
public:
void backTrack(int i);
void showAnswer();
};
AlgPro4_nQueen::AlgPro4_nQueen()
{
for (int i=1;i<= 8;i++)column[i] = 1;
for (int i=1;i<= 8<<1;i++){
rightUp[i] = 1;
leftUp[i] = 1;
}
}
void AlgPro4_nQueen::showAnswer()
{
num++;
printf("\nSolution %d:",num);
for (int y = 1;y<=queensNum;y++){
for (int x= 1;x<=queensNum;x++){
if (queen[y] ==x) printf("♀");
else printf(" .");
}
printf("\n");
}
}
void AlgPro4_nQueen::backTrack(int i){
if (i>queensNum){
showAnswer();
}else{
for (int j = 1;j<=queensNum;j++){
if ((column[j] ==1) && (rightUp[i+j] ==1) && (leftUp[i-j+queensNum]==1)){
queen[i] =j;
column[j] = 0;
rightUp[i+j] = 0;
leftUp[i-j+queensNum]=0;
backTrack(i+1);
column[j]=1;
rightUp[i+j] =1;
leftUp[i-j+queensNum]=1;
}
}
}
}
int main()
{
AlgPro4_nQueen aq ;
aq.backTrack(1);
getchar();
}
- 2楼网友:独钓一江月
- 2021-12-28 21:10
//我已经帮你改了,但是我运行了一下,发现结果有问题
- 3楼网友:一把行者刀
- 2021-12-28 20:02
查求不多,只是声明变量的时候
private :
intqueensNum = 8;
这种形式
System.out.println()改写成 cout<<intqueensNum <<endl;
这样的形式
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯