1.求此八皇后C程序的皇后后流程~~急~~要流程~~
2.c程序设计“八皇后”问题 包括图像
3.å
³äº8çåçC代ç
4.求八皇后问题C++程序设计
求此八皇后C程序的流程~~急~~要流程~~
八皇后问题:
问题提出:
8×8的棋盘上放置8个皇后,在同一横线、游戏c源游戏竖线、码皇易语言源码锁机源码教材对角线上会产生冲突,皇后后
求不产生冲突即8个皇后都安全的游戏c源游戏源码如何播放放置方法。
这里改变NCOUNT即可以求出n皇后的码皇国外源码博客n×n棋盘的放置方法
张可彦:
kyany@sina.com
*/
#include
"stdio.h"
#define
NCOUNT
8
int
nArray[NCOUNT][NCOUNT];
//
判断一个点是否是安全点
bool
IsSafe(int
i,int
j)
{
int
x=i,y=j;
while(1)
{
x
-=
1;
if(
x<0
)break;
y
-=
1;
if(
y<0)break;
if(
nArray[x][y]
==
1)return
false;
}
x=i;
y=j;
while(1)
{
x
+=
1;
if(
x>NCOUNT-1
)break;
y
+=
1;
if(
y
>NCOUNT-1
)break;
if(
nArray[x][y]
==
1)return
false;
}
x=i;
y=j;
while(1)
{
x
-=1;
if(
x<0
)break;
y
+=1;
if(
y>NCOUNT-1
)break;
if(
nArray[x][y]
==
1)return
false;
}
x=i;
y=j;
while(1)
{
x
+=1;
if(
x>NCOUNT-1
)break;
y-=1;
if(
y<0
)break;
if(
nArray[x][y]
==
1)return
false;
}
x=i;
y=j;
while(1)
{
x
-=1;
if(
x<0
)break;
if(
nArray[x][y]
==
1)return
false;
}
x=i;
y=j;
while(1)
{
x
+=1;
if(
x>NCOUNT-1
)break;
if(
nArray[x][y]
==
1)return
false;
}
x=i;
y=j;
while(1)
{
y
-=1;
if(
y<0
)break;
if(
nArray[x][y]
==
1)return
false;
}
x=i;
y=j;
while(1)
{
y
+=1;
if(
y>NCOUNT-1
)break;
if(
nArray[x][y]
==
1)return
false;
}
return
true;
}
void
main()
{
int
nVe=-1,nHo=0;
bool
bRetry
=
false;
int
nSol
=
0;
//
清除棋盘
for(int
i=0;i<NCOUNT;i++)
{
for(
int
j=0;j<NCOUNT;j++)
nArray[i][j]
=
0;
}
while(1)
{
nVe
+=
1;
if(
nVe>NCOUNT-1)
{ //
棋盘放满,打印当前棋盘上棋子位置
nSol++;
printf("Sol
%d:
",皇后后nSol);
for(int
i=0;i<NCOUNT;i++)
{
for(
int
j=0;j<NCOUNT;j++)
if(
nArray[i][j]==1)
printf("(%d,%d)
",i,j);
}
printf("\r\n");
//
回溯查找下一个可行方案
nVe
-=
2;
bRetry
=
true;
continue;
}
int
nFill
=
0;
if(
bRetry
)
{
//
回溯计算
bRetry
=
false;
for(
i=0;i<NCOUNT;i++)
{ //
得到棋子的位置
if(
nArray[nVe][i]
==
1)
{
nArray[nVe][i]
=
0;
nFill
=
i;
break;
}
}
if(
nFill
==
NCOUNT-1)
{ //
棋子在当前行已经是最后的位置
//
如果是第一行,算法结束
if(
nVe
==
0)return;
//
否则回溯
nVe
-=
2;
bRetry
=
true;
continue;
}
//
从当前位置之后查找一个安全点
nFill
+=
1;
}
bool
bFilled
=
false;
for(
i=nFill;i<NCOUNT;i++)
{ //
当前行查找一个安全点
if(
IsSafe(nVe,游戏c源游戏i))
{
bFilled
=
true;
nArray[nVe][i]
=
1;
break;
}
}
//
找不到安全点,回溯
if(
!bFilled
)
{
nVe
-=
2;
bRetry
=
true;
}
}
}
c程序设计“八皇后”问题 包括图像
#include<stdio.h>
#define NUM 8
int a[NUM+1];
void main()
{
int i,码皇k,flag,not_finish=1,count=0;
i=1;
a[1]=1;
printf("The possible configuration of 8 queens are:\n");
while(not_finish)
{
while(not_finish&&i<=NUM)
{
for(flag=1,k=1;flag&&k<i;k++)
if(a[k]==a[i])flag=0;
for(k=1;flag&&k<i;k++)
if((a[i]==a[k]-(k-i))||(a[i]==a[k]+(k-i))) flag=0;
if(!flag)
{
if(a[i]==a[i-1])
{
i--;
if(i>1&&a[i]==NUM)
a[i]=1;
else if(i==1&&a[i]==NUM)
not_finish=0;
else a[i]++;
}
else if(a[i]==NUM) a[i]=1;
else a[i]++;
}
else if(++i<=NUM)
if(a[i-1]==NUM) a[i]=1;
else a[i]=a[i-1]+1;
}
if(not_finish)
{
++count;
printf((count-1)%3?" [%2d]: ":" \n[%2d]: ",count);
for(k=1;k<=NUM;k++)
printf(" %d",a[k]);
if(a[NUM-1]<NUM) a[NUM-1]++;
else a[NUM-1]=1;
i=NUM-1;
}
}
}
有一个结果的棋盘图,但baidu的皇后后图像上传功能不好,就不传了。游戏c源游戏。码皇。皇后后租群源码
å ³äº8çåçC代ç
ç»ä½ ä¸ä¸ªæèªå·±åçå§ï¼å¥½å¥½ç 究ä¸ä¸
#include<stdio.h>
#define M //a stand for'-'
int n=4; //b stand for'/'
main() //c stand for'\'
{ int a[M]={ 0},游戏c源游戏b[M]={ 0},c[M]={ 0},g[M]={ 0};
int i=0,j=0,k=0,x,flag=0,z=1;
FILE * out;
out=fopen("out.txt","w");
while(i<n)
{ g[j]=i;
a[i]=1;
b[i]=1;
c[n-1-i]=1;
k=0;
j=1;
printf("1 a%d b%d c%d\n",i,i,n-1-i);
// system("pause");
while(j>0&&j<n)
{ while(k<n)
{ if(a[k]==1||b[k+j]==1||c[n-1-k+j]==1)
{ if(k==n-1){ flag=1;break;}
else { k++;continue;}
}
g[j]=k;
if(j==n-1)
{ printf("第%dç§ï¼",z++);
for(x=0;x<n;x++)
{ printf("%d ",g[x]);
fprintf(out,"%-3d",g[x]);
}
fprintf(out,"\n");
printf("\n");
//system("pause");
k++;
continue;}
else
{ a[k]=1;
b[k+j]=1;
c[n-1-k+j]=1;
printf("2 a%d b%d c%d\n",k,k+j,n-1-k+j);
// system("pause");
flag=0;
break;}
}
if(flag==1)
{ j--;
if(j==0)
{ a[i]=0;
b[i]=0;
c[n-1-i]=0;
printf("4 a%d b%d c%d\n",i,i,n-1-i);
i++;
break;
}
a[g[j]]=0;
b[g[j]+j]=0;
c[n-1-g[j]+j]=0;
printf("3 a%d b%d c%d\n",g[j],g[j]+j,n-1-g[j]+j);
// system("pause");
g[j]++;
k=g[j];
}
else//flag==0
{ j++;
k=0;}
}
}
fclose(out);
}
//fprintf(out,"\n");
//fprintf(out,"第%dç§ï¼",z++);
//fprintf(out,"%-3d",g[x]+1);
//system("pause");
求八皇后问题C++程序设计
#ifndef _QUEENBOARD_H_
#define _QUEENBOARD_H_
const int BOARDSIZE = 8;
using namespace std;
class Queenboard {
private:
bool board[BOARDSIZE][BOARDSIZE];
public:
Queenboard();
bool is_space_under_attack(int, int) const;
void occupy_space(int, int);
void clear_column(int);
friend ostream& operator<<(ostream& out, const Queenboard& cb);
};
Queenboard::Queenboard() {
// Initialize the board to contain zero queens.
for (int row = 0; row < BOARDSIZE; row++) {
for (int col = 0; col < BOARDSIZE; col++) {
board[row][col] = false;
}
}
}
ostream& operator<<(ostream& out, const Queenboard& cb) {
// output the board
for (int row = 0; row < BOARDSIZE; row++) {
out << "---------------------------------" << endl;
for (int col = 0; col < BOARDSIZE; col++) {
out << "|";
if ( cb.board[row][col]) {
out << " Q ";
}
else {
out << " ";
}
}
out << "|" << endl;
}
out << "---------------------------------" << endl;
return out;
}
void Queenboard::clear_column(int col) {
if (col >= BOARDSIZE || col < 0) {
throw out_of_range("Queenboard::clear_column()");
}
for (int row = 0; row < BOARDSIZE; row++) {
board[row][col] = false;
}
}
void Queenboard::occupy_space(int row, int col) {
if (col >= BOARDSIZE || col < 0 ||
row >= BOARDSIZE || row < 0) {
throw out_of_range("Queenboard::occupy_space()");
}
// places a queen on the board
board[row][col] = true;
}
bool Queenboard::is_space_under_attack(int row, int col) const {
if (col >= BOARDSIZE || col < 0 ||
row >= BOARDSIZE || row < 0) {
throw out_of_range("Queenboard::is_space_under_attack()");
}
// check to the left
int i = col - 1;
while (i >= 0) {
if (board[row][i]) return true;
i--;
}
// check diagonal up and left
int j = row - 1;
int k = col - 1;
while (j >= 0 && k >= 0) {
if (board[j][k]) return true;
j--; k--;
}
// check diagonal down and left
j = row + 1;
k = col - 1;
while (j < BOARDSIZE && k >= 0) {
if (board[j][k]) return true;
j++; k--;
}
return false;
}
#endif
#include <iostream>
#include <cstdlib>
#include <stdexcept>
#include "Queenboard.h"
using namespace std;
bool place_queens(Queenboard& qb, int col);
int main(int argc, char* argv[]) {
try {
Queenboard qb;
if (! place_queens(qb, 0)) {
cout << "No solution found.\n";
}
return EXIT_SUCCESS;
}
catch (exception& e) {
cerr << e.what() << "\n";
}
catch (...) {
cerr << "Unknown exception caught.\n";
}
return EXIT_FAILURE;
}
bool place_queens(Queenboard& qb, int col) {
bool inserted = false;
for (int row = 0; row < BOARDSIZE; row++) {
if (! qb.is_space_under_attack(row, col)) {
// insert a queen
qb.occupy_space(row, col);
inserted = true;
if (col == BOARDSIZE - 1) {
// solution found!
cout << qb << "\n";
return true;
}
else {
// place a queen in the next column
if (place_queens(qb, col + 1)) {
return true;
}
else {
inserted = false;
}
}
}
}
if (! inserted) {
// backtrack to previous column
qb.clear_column(col - 1);
return false;
}
}
2024-11-23 08:38
2024-11-23 08:23
2024-11-23 07:52
2024-11-23 07:48
2024-11-23 07:36
2024-11-23 07:35
2024-11-23 07:25
2024-11-23 06:28