5皇后问题求解

YBKYO 2002-11-15 09:45:33
5皇后问题就是在8*8的国际象棋棋盘上,放5个皇后,使它们控制整个棋盘,即在任何一格放一个棋子,都会马上被吃掉。问如何放?
这是上大二时的作业,可我现在还不会,苦啊。
...全文
1319 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
潘李亮 2002-11-17
  • 打赏
  • 举报
回复
八皇后和五皇后是同一个道理
YBKYO 2002-11-17
  • 打赏
  • 举报
回复
才发现上面有一位叫FishCrazy(疯鱼),请问是不是原来kof中文网的疯鱼?
YBKYO 2002-11-17
  • 打赏
  • 举报
回复
upup
pdss 2002-11-17
  • 打赏
  • 举报
回复
到那里去看看,是偶整理的数据,全都关于程序设计算法的题目,而且还有很多澳赛题目
http://202.113.96.10/ini/index.htm
如果满意的话,give fen
rockhard 2002-11-17
  • 打赏
  • 举报
回复
#include "stdafx.h" 是VC生成的控制台程序里面就有的,你可以不用向导生成,空项目就不用这一句了。
rockhard 2002-11-17
  • 打赏
  • 举报
回复
答案可能太多,修改程序后让它输出六个解,其中两个我已验证是对的,其它的没有验证:
Answer:
The 1 queen x coordinate:0;y coordinate:0
The 2 queen x coordinate:0;y coordinate:1
The 3 queen x coordinate:1;y coordinate:5
The 4 queen x coordinate:4;y coordinate:0
The 5 queen x coordinate:5;y coordinate:4
Answer:
The 1 queen x coordinate:0;y coordinate:0
The 2 queen x coordinate:0;y coordinate:1
The 3 queen x coordinate:1;y coordinate:5
The 4 queen x coordinate:5;y coordinate:4
The 5 queen x coordinate:4;y coordinate:0
Answer:
The 1 queen x coordinate:0;y coordinate:0
The 2 queen x coordinate:0;y coordinate:1
The 3 queen x coordinate:3;y coordinate:5
The 4 queen x coordinate:4;y coordinate:3
The 5 queen x coordinate:5;y coordinate:4
Answer:
The 1 queen x coordinate:0;y coordinate:0
The 2 queen x coordinate:0;y coordinate:1
The 3 queen x coordinate:3;y coordinate:5
The 4 queen x coordinate:4;y coordinate:6
The 5 queen x coordinate:5;y coordinate:4
Answer:
The 1 queen x coordinate:0;y coordinate:0
The 2 queen x coordinate:0;y coordinate:1
The 3 queen x coordinate:3;y coordinate:5
The 4 queen x coordinate:5;y coordinate:4
The 5 queen x coordinate:4;y coordinate:3
Answer:
The 1 queen x coordinate:0;y coordinate:0
The 2 queen x coordinate:0;y coordinate:1
The 3 queen x coordinate:3;y coordinate:5
The 4 queen x coordinate:5;y coordinate:4
The 5 queen x coordinate:4;y coordinate:6
rockhard 2002-11-17
  • 打赏
  • 举报
回复
偶的穷举程序,调试通过(VC):


// 5queen.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>

struct ChessCoordinate
{
int x;
int y;
};

ChessCoordinate QueenCoordinate[5];//记录五个皇后的坐标位置


bool IsOk(void)//根据五皇后的位置判断是否满足条件。
{
int temp[8][8];
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
temp[i][j]=0;

for(i=0;i<5;i++)
{
for(int j=0;j<8;j++)
{
temp[QueenCoordinate[i].x][j]=1;//将皇后所在的整行置为已控制区
temp[j][QueenCoordinate[i].y]=1;//将皇后所在的整列置为已控制区

int t=j+QueenCoordinate[i].y-QueenCoordinate[i].x;
if(t>=0&&t<8)
temp[j][t]=1;//置斜率为-1的区域为控制区
t=QueenCoordinate[i].x+QueenCoordinate[i].y-j;
if(t>=0&&t<8)
temp[j][t]=1;//置斜率为1的区域为控制区
}
}
for(i=0;i<8;i++)
for(int j=0;j<8;j++)
if(!temp[i][j])
return false;
return true;
}

void PrintAnswer()
{
cout<<"\nAnswer:";
for(int i=0;i<5;i++)
{
cout<<"\nThe "<<i+1<<" queen x coordinate:"<<QueenCoordinate[i].x;
cout<<";y coordinate:"<<QueenCoordinate[i].y;
}
}

int main(int argc, char* argv[])
{
for(int i1x=0;i1x<8;i1x++)
for(int i1y=0;i1y<8;i1y++)
{
QueenCoordinate[0].x=i1x;
QueenCoordinate[0].y=i1y;
for(int i2x=0;i2x<8;i2x++)
for(int i2y=0;i2y<8;i2y++)
{
QueenCoordinate[1].x=i2x;
QueenCoordinate[1].y=i2y;
for(int i3x=0;i3x<8;i3x++)
for(int i3y=0;i3y<8;i3y++)
{
QueenCoordinate[2].x=i3x;
QueenCoordinate[2].y=i3y;
for(int i4x=0;i4x<8;i4x++)
for(int i4y=0;i4y<8;i4y++)
{
QueenCoordinate[3].x=i4x;
QueenCoordinate[3].y=i4y;
for(int i5x=0;i5x<8;i5x++)
for(int i5y=0;i5y<8;i5y++)
{
QueenCoordinate[4].x=i5x;
QueenCoordinate[4].y=i5y;
if(IsOk())
{
PrintAnswer();
return 0;//注释掉此行找出所有解
}
}
}
}
}
}
}

lovedata 2002-11-17
  • 打赏
  • 举报
回复
现 在 不 在 家, 你 留 个 地 址, 回 家 以 后 发 给 你 一 个 完 整 的 文 件, 是 老 师 让 做 的 作 业,n 皇 后。
FishCrazy 2002-11-17
  • 打赏
  • 举报
回复
回答YBKYO(YB):
虽然偶也玩KOF,但是水平很菜,还没去过KOF中文网……
让你失望了。
YBKYO 2002-11-15
  • 打赏
  • 举报
回复
8个皇后还用做?一行放一个不就完了吗?
langziji 2002-11-15
  • 打赏
  • 举报
回复
可以简单说一下国际象棋的无则吗?要不然我是帮不上忙的,如果你告诉我的话我可以给你用C写一个说不上最简单的算法给你。gowithmebaby@163.com
FishCrazy 2002-11-15
  • 打赏
  • 举报
回复
楼主大人,拜托国际象棋的棋盘是8*8的,5个皇后哪有那么大的本事啊,少说也得有8个啊!!
你的棋盘是不是小了点啊????
YBKYO 2002-11-15
  • 打赏
  • 举报
回复
给分当然是忘不了的....
YBKYO 2002-11-15
  • 打赏
  • 举报
回复
不解。
#include "stdafx.h"
#include "..\include\effectcmp.h"
这两个头文件在哪里?
ddmpqcw 2002-11-15
  • 打赏
  • 举报
回复
以前写过的一个类,可以解决N皇后问题!


#include "stdafx.h"
#include <iostream.h>
#include "..\include\effectcmp.h"
class bqueen{
public:
enum IS_DRAW{DRAW,NOTDRAW};
public:
bqueen(int n=8):top(0),_count(0),p(n){
_ln=new bool[n];
_rw=new bool[n];
_x1=new bool[n*2];
_x2=new bool[n*2];
pt=new POINT[n];
clear();
}

int count(){
return _count;
}
void byi(int i,IS_DRAW d){
if(i==p){
if(d==DRAW)
draw();
if (top==p)
_count++;
return;
}
for (int j=0;j<p;j++){
if(!ifln(j)&&!ifx1(i+j)&&!ifx2(j-i+p-1)){
setnextpt(i,j);
byi(i+1,d);
_ln[j]=_rw[i]=_x1[i+j]=_x2[j-i+p-1]=false;
top--;
}

}
}
void go(IS_DRAW d){
byi(0,d);
}

private:
void clear(){
for (int i=0;i<p;i++)
for (int j=0;j<p;j++)
_ln[j]=_rw[i]=_x1[j+i]=_x2[j-i+p-1]=false;
top=0;
}
bool ifln(int l){return _ln[l];}
bool ifrw(int r){return _rw[r];}
bool ifx1(int x){return _x1[x];}
bool ifx2(int x){return _x2[x];}
void printres(){
if (top<p)
return;
for (int i=0;i<p;i++)
cout<<"("<<pt[i].x<<","<<pt[i].y<<")"<<"\t";
cout<<endl;
}
void setnextpt(int x,int y){
pt[top].x=x;
pt[top].y=y;
top++;
_ln[y]=_rw[x]=_x1[x+y]=_x2[y-x+p-1]=true;
}
void cutjustnow(){
top--;
}
void drawln(int t){
while(t--)
cout<<" ";
}
void draw(){
for(int x=0;x<p;x++){
for(int y=0;y<p;y++){
drawln(pt[x].y);
cout<<"* ";
drawln(p-1-pt[x].y);
break;
}
cout<<endl;
}
cout<<"-----------------------------------------"<<endl;
}
private:
typedef struct tagPT{
int x;
int y;
}POINT;
bool *_ln,*_rw,*_x1,*_x2;
POINT *pt;
int top;
int _count;
int p;
};


void main(){
long dtim;
bqueen b(14);
BEGIN_RECORD
b.go(bqueen::NOTDRAW);
END_RECORD(dtim)
cout<<"all time:"<<dtim<<endl;
cout<<b.count()<<endl;
}


如果可以,记住给分!
YBKYO 2002-11-15
  • 打赏
  • 举报
回复
没听说过?题目我写在上面了。
fangrk 2002-11-15
  • 打赏
  • 举报
回复
只听说过8皇后,没听说过5皇后
YBKYO 2002-11-15
  • 打赏
  • 举报
回复
langziji(浪子)真是好人啊
关于皇后的走法可以看这个:http://person.zj.cninfo.net/~belllee/chess/h.htm
YYBear 2002-11-15
  • 打赏
  • 举报
回复
其实就是经典的8皇后问题啦,不过现在用5皇后,一样的算法阿
YYBear 2002-11-15
  • 打赏
  • 举报
回复
这是一个很经典的算法题啊,去图书馆查查算法书,不管老的新的,都应该有的!!

用回朔+穷举做的,先放一个在棋盘上,再在其他格上放一个,看看行不行,不行的话,退回上一步,再其他格上再试,这样一直试下去

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧